Skip to content

Commit

Permalink
fix: Resources API - Send delta message when resources are added / de…
Browse files Browse the repository at this point in the history
…leted via the plugin interface. (#1797)

* Send delta when resources are added / deleted via plugin interface.
  • Loading branch information
panaaj authored Sep 21, 2024
1 parent d85611a commit 7323f02
Show file tree
Hide file tree
Showing 2 changed files with 34 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
35 changes: 33 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,21 @@ export class ResourcesApi {
}
}
if (provider) {
return this.resProvider[resType]?.get(provider)?.setResource(resId, data)
this.resProvider[resType]
?.get(provider)
?.setResource(resId, data)
.then((r) => {
this.app.handleMessage(
provider as string,
this.buildDeltaMsg(resType, resId, data),
SKVersion.v2
)
return r
})
.catch((e: Error) => {
debug(e)
return Promise.reject(new Error(`Error writing ${resType} ${resId}`))
})
} else {
return Promise.reject(new Error(`No provider for ${resType}`))
}
Expand All @@ -169,7 +186,21 @@ export class ResourcesApi {
provider = await this.getProviderForResourceId(resType, resId)
}
if (provider) {
return this.resProvider[resType]?.get(provider)?.deleteResource(resId)
this.resProvider[resType]
?.get(provider)
?.deleteResource(resId)
.then((r) => {
this.app.handleMessage(
provider as string,
this.buildDeltaMsg(resType, resId, null),
SKVersion.v2
)
return r
})
.catch((e: Error) => {
debug(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 7323f02

Please sign in to comment.