Skip to content

Commit

Permalink
Send delta when resources are added / deleted via plugin interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
panaaj committed Sep 20, 2024
1 parent 57b7f8d commit e9bffb1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/resources-provider-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ module.exports = (server: ResourceProviderApp): Plugin => {
const apiGetResources = async (
resType: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
params?: any
params: any = {}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> => {
if (typeof params.position === 'undefined') {
Expand Down
31 changes: 29 additions & 2 deletions src/api/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ interface ResourceApplication
export class ResourcesApi {
private resProvider: { [key: string]: Map<string, ResourceProviderMethods> } =
{}
private app: ResourceApplication

constructor(app: ResourceApplication) {
this.app = app
this.initResourceRoutes(app)
}

Expand Down Expand Up @@ -106,6 +108,7 @@ export class ResourcesApi {
debug(`** listResources(${resType}, ${JSON.stringify(params)})`)

const provider = this.checkForProvider(resType, providerId)
debug(`** provider = ${provider}`)
if (!provider) {
return Promise.reject(new Error(`No provider for ${resType}`))
}
Expand Down Expand Up @@ -149,7 +152,19 @@ export class ResourcesApi {
}
}
if (provider) {
return this.resProvider[resType]?.get(provider)?.setResource(resId, data)
try {
const r = await this.resProvider[resType]
?.get(provider)
?.setResource(resId, data)
this.app.handleMessage(
provider as string,
this.buildDeltaMsg(resType, resId, data),
SKVersion.v2
)
return r
} catch (e) {
return Promise.reject(new Error(`Error deleting ${resType} ${resId}`))
}
} else {
return Promise.reject(new Error(`No provider for ${resType}`))
}
Expand All @@ -169,7 +184,19 @@ export class ResourcesApi {
provider = await this.getProviderForResourceId(resType, resId)
}
if (provider) {
return this.resProvider[resType]?.get(provider)?.deleteResource(resId)
try {
const r = await this.resProvider[resType]
?.get(provider)
?.deleteResource(resId)
this.app.handleMessage(
provider as string,
this.buildDeltaMsg(resType, resId, null),
SKVersion.v2
)
return r
} catch (e) {
return Promise.reject(new Error(`Error deleting ${resType} ${resId}`))
}
} else {
return Promise.reject(new Error(`No provider for ${resType}`))
}
Expand Down

0 comments on commit e9bffb1

Please sign in to comment.