-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor based on review comments
- Loading branch information
Showing
19 changed files
with
139 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* eslint-disable @jessie.js/safe-await-separator */ | ||
import { Fail } from '@agoric/assert'; | ||
import { kunser } from '@agoric/kmarshal'; | ||
import { makeQueue } from '@endo/stream'; | ||
import type { E } from '@endo/eventual-send'; | ||
|
||
import type { SwingsetController } from '../src/controller/controller.js'; | ||
|
||
const sink = () => {}; | ||
|
||
export const makeRunUtils = ( | ||
controller: SwingsetController, | ||
log = (..._) => {}, | ||
) => { | ||
let cranksRun = 0; | ||
|
||
const mutex = makeQueue(); | ||
|
||
mutex.put(controller.run()); | ||
|
||
const runThunk = async <T extends () => any>( | ||
thunk: T, | ||
): Promise<ReturnType<T>> => { | ||
try { | ||
// this promise for the last lock may fail | ||
await mutex.get(); | ||
} catch { | ||
// noop because the result will resolve for the previous runMethod return | ||
} | ||
|
||
const thunkResult = await thunk(); | ||
|
||
const result = controller.run().then(cranks => { | ||
cranksRun += cranks; | ||
log(`kernel ran ${cranks} cranks`); | ||
return thunkResult; | ||
}); | ||
mutex.put(result.then(sink, sink)); | ||
return result; | ||
}; | ||
|
||
const queueAndRun = async (deliveryThunk, voidResult = false) => { | ||
log('queueAndRun at', cranksRun); | ||
|
||
const kpid = await runThunk(deliveryThunk); | ||
|
||
if (voidResult) { | ||
return undefined; | ||
} | ||
const status = controller.kpStatus(kpid); | ||
switch (status) { | ||
case 'fulfilled': | ||
return kunser(controller.kpResolution(kpid)); | ||
case 'rejected': | ||
throw kunser(controller.kpResolution(kpid)); | ||
case 'unresolved': | ||
throw Error(`unresolved value for ${kpid}`); | ||
default: | ||
throw Fail`unknown status ${status}`; | ||
} | ||
}; | ||
|
||
type EVProxy = typeof E & { | ||
sendOnly: (presence: unknown) => Record<string, (...args: any) => void>; | ||
vat: (name: string) => Record<string, (...args: any) => Promise<any>>; | ||
}; | ||
// @ts-expect-error cast, approximate | ||
const EV: EVProxy = presence => | ||
new Proxy(harden({}), { | ||
get: (_t, method, _rx) => | ||
harden((...args) => | ||
queueAndRun(() => | ||
controller.queueToVatObject(presence, method, args), | ||
), | ||
), | ||
}); | ||
EV.vat = vatName => | ||
new Proxy(harden({}), { | ||
get: (_t, method, _rx) => | ||
harden((...args) => | ||
queueAndRun(() => controller.queueToVatRoot(vatName, method, args)), | ||
), | ||
}); | ||
// @ts-expect-error xxx | ||
EV.sendOnly = presence => | ||
new Proxy(harden({}), { | ||
get: (_t, method, _rx) => | ||
harden((...args) => | ||
queueAndRun( | ||
() => controller.queueToVatObject(presence, method, args), | ||
true, | ||
), | ||
), | ||
}); | ||
// @ts-expect-error xxx | ||
EV.get = presence => | ||
new Proxy(harden({}), { | ||
get: (_t, pathElement, _rx) => | ||
queueAndRun(() => | ||
controller.queueToVatRoot('bootstrap', 'awaitVatObject', [ | ||
presence, | ||
[pathElement], | ||
]), | ||
), | ||
}); | ||
return harden({ runThunk, EV }); | ||
}; | ||
export type RunUtils = ReturnType<typeof makeRunUtils>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.