Skip to content

Commit

Permalink
feat: implement cancellation
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Martin <phmartin@redhat.com>
  • Loading branch information
feloy committed Nov 8, 2023
1 parent b8352c3 commit 4ea5a5e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/main/src/plugin/image-checker.ts
@@ -1,4 +1,5 @@
import type {
CancellationToken,
Disposable,
ImageCheckerProvider,
ImageCheckerProviderMetadata,
Expand Down Expand Up @@ -63,11 +64,11 @@ export class ImageCheckerImpl {
});
}

async check(providerId: string, image: ImageInfo): Promise<ImageChecks | undefined> {
async check(providerId: string, image: ImageInfo, token?: CancellationToken): Promise<ImageChecks | undefined> {
const provider = this._imageCheckerProviders.get(providerId);
if (provider === undefined) {
throw new Error('provider not found with id ' + providerId);
}
return provider.provider.check(image);
return provider.provider.check(image, token);
}
}
14 changes: 12 additions & 2 deletions packages/main/src/plugin/index.ts
Expand Up @@ -1953,8 +1953,18 @@ export class PluginSystem {

this.ipcHandle(
'image-checker:check',
async (_listener, id: string, image: ImageInfo): Promise<containerDesktopAPI.ImageChecks | undefined> => {
return imageChecker.check(id, image);
async (
_listener,
id: string,
image: ImageInfo,
tokenId?: number,
): Promise<containerDesktopAPI.ImageChecks | undefined> => {
let token;
if (tokenId) {
const tokenSource = cancellationTokenRegistry.getCancellationTokenSource(tokenId);
token = tokenSource?.token;
}
return imageChecker.check(id, image, token);
},
);

Expand Down
8 changes: 6 additions & 2 deletions packages/preload/src/index.ts
Expand Up @@ -1679,8 +1679,12 @@ function initExposure(): void {

contextBridge.exposeInMainWorld(
'imageCheck',
async (id: string, image: string): Promise<containerDesktopAPI.ImageChecks | undefined> => {
return ipcInvoke('image-checker:check', id, image);
async (
id: string,
image: string,
cancellationToken?: number,
): Promise<containerDesktopAPI.ImageChecks | undefined> => {
return ipcInvoke('image-checker:check', id, image, cancellationToken);
},
);
}
Expand Down

0 comments on commit 4ea5a5e

Please sign in to comment.