Skip to content

Commit

Permalink
feat(daemon): Thread cancellation context into worklets
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored and rekmarks committed Feb 22, 2024
1 parent afdfa21 commit 501f96a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
22 changes: 20 additions & 2 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ const makeInspector = (type, number, record) =>
list: () => Object.keys(record),
});

const makeFarContext = context =>
Far('Context', {
cancel: context.cancel,
whenCancelled: () => context.cancelled,
whenDisposed: () => context.disposed,
addDisposalHook: context.onCancel,
});

/**
* @param {import('./types.js').DaemonicPowers} powers
* @param {Promise<number>} webletPortP
Expand Down Expand Up @@ -284,7 +292,12 @@ const makeDaemonCore = async (
// eslint-disable-next-line no-use-before-define
provideValueForFormulaIdentifier(guestFormulaIdentifier)
);
const external = E(workerDaemonFacet).makeUnconfined(specifier, guestP);
const external = E(workerDaemonFacet).makeUnconfined(
specifier,
guestP,
// TODO fix type
/** @type {any} */ (makeFarContext(context)),
);
return { external, internal: undefined };
};

Expand Down Expand Up @@ -327,7 +340,12 @@ const makeDaemonCore = async (
// eslint-disable-next-line no-use-before-define
provideValueForFormulaIdentifier(guestFormulaIdentifier)
);
const external = E(workerDaemonFacet).makeBundle(readableBundleP, guestP);
const external = E(workerDaemonFacet).makeBundle(
readableBundleP,
guestP,
// TODO fix type
/** @type {any} */ (makeFarContext(context)),
);
return { external, internal: undefined };
};

Expand Down
2 changes: 1 addition & 1 deletion packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export interface Context {
}

export interface FarContext {
cancel: (reason: string) => Promise<never>;
cancel: (reason: Error) => Promise<void>;
whenCancelled: () => Promise<never>;
whenDisposed: () => Promise<void>;
}
Expand Down
14 changes: 8 additions & 6 deletions packages/daemon/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,24 @@ export const makeWorkerFacet = ({ cancel }) => {

/**
* @param {string} specifier
* @param {unknown} powersP
* @param {Promise<unknown>} powersP
* @param {Promise<unknown>} contextP
*/
makeUnconfined: async (specifier, powersP) => {
makeUnconfined: async (specifier, powersP, contextP) => {
// Windows absolute path includes drive letter which is confused for
// protocol specifier. So, we reformat the specifier to include the
// file protocol.
const specifierUrl = normalizeFilePath(specifier);
const namespace = await import(specifierUrl);
return namespace.make(powersP);
return namespace.make(powersP, contextP);
},

/**
* @param {import('@endo/eventual-send').ERef<import('./types.js').EndoReadable>} readableP
* @param {unknown} powersP
* @param {Promise<unknown>} powersP
* @param {Promise<unknown>} contextP
*/
makeBundle: async (readableP, powersP) => {
makeBundle: async (readableP, powersP, contextP) => {
const bundleText = await E(readableP).text();
const bundle = JSON.parse(bundleText);

Expand All @@ -90,7 +92,7 @@ export const makeWorkerFacet = ({ cancel }) => {
const namespace = await importBundle(bundle, {
endowments,
});
return namespace.make(powersP);
return namespace.make(powersP, contextP);
},
});
};
Expand Down

0 comments on commit 501f96a

Please sign in to comment.