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 15, 2024
1 parent 3fa53fb commit e17cecc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
20 changes: 18 additions & 2 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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 @@ -289,7 +297,11 @@ const makeEndoBootstrap = 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,
makeFarContext(context),
);
return { external, internal: undefined };
};

Expand Down Expand Up @@ -332,7 +344,11 @@ const makeEndoBootstrap = 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,
makeFarContext(context),
);
return { external, internal: undefined };
};

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 e17cecc

Please sign in to comment.