Skip to content

Commit

Permalink
fix: restoring most state, just need to isolate the plugin captp
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Sep 16, 2020
1 parent 93c8933 commit f92ee73
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
22 changes: 12 additions & 10 deletions packages/SwingSet/src/devices/plugin-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function buildRootDeviceNode(tools) {
const { SO, getDeviceState, setDeviceState, endowments } = tools;
const restart = getDeviceState();

let registeredReceiver;
let registeredReceiver = restart && restart.registeredReceiver;

const connectedMods = [];
const senders = [];
Expand All @@ -15,7 +15,8 @@ export function buildRootDeviceNode(tools) {
function saveState() {
setDeviceState(
harden({
connectedMods,
registeredReceiver,
connectedMods: [...connectedMods],
connectedState: [...connectedState],
}),
);
Expand All @@ -34,14 +35,11 @@ export function buildRootDeviceNode(tools) {
connectedMods.push(mod);
const receiver = obj => {
console.info('receiver', index, obj);
switch (obj.type) {
case 'PLUGIN_SAVE_STATE':
connectedState[index] = obj.data;
saveState();
break;
default:
SO(registeredReceiver).receive(index, obj);
}

// We need to run the kernel after the send-only.
endowments.queueThunkForKernel(() =>
SO(registeredReceiver).receive(index, obj),
);
};
// Create a bootstrap reference from the module.
const bootstrap = modNS.bootPlugin(
Expand Down Expand Up @@ -88,7 +86,11 @@ export function buildRootDeviceNode(tools) {
connect,
send,
registerReceiver(receiver) {
if (registeredReceiver) {
throw Error(`registerd receiver already set`);
}
registeredReceiver = receiver;
saveState();
},
});
}
4 changes: 2 additions & 2 deletions packages/SwingSet/src/devices/plugin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export function buildPlugin(pluginRequire) {
export function buildPlugin(pluginRequire, queueThunkForKernel) {
const srcPath = require.resolve('./plugin-src');

// srcPath and endowments are provided to buildRootDeviceNode() for use
// during configuration.
return {
srcPath,
endowments: { require: pluginRequire },
endowments: { require: pluginRequire, queueThunkForKernel },
};
}
2 changes: 1 addition & 1 deletion packages/SwingSet/src/kernel/liveSlots.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ function build(
// It has arguments, must be a method application.
res = HandledPromise.applyMethod(t, method, args);
} else {
// TODO: untested, but in principle sound.
// Just a getter.
// TODO: untested, but in principle sound.
res = HandledPromise.get(t, method);
}
res.then(notifySuccess, notifyFailure);
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-swingset/lib/ag-solo/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const bootPlugin = ({ getState, setState }) => {
async hello(name) {
state = { count: state.count + 1 };
setState(state);
await fs.writeFile(dataFile, `Said hello ${state.count} times`);
await fs.writeFile(dataFile, `Said hello ${state.count} times\n`);
return `Hello, ${name}`;
},
});
Expand Down
11 changes: 8 additions & 3 deletions packages/cosmic-swingset/lib/ag-solo/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ async function buildSwingset(
const mb = buildMailbox(mbs);
const cm = buildCommand(broadcast);
const timer = buildTimer();
const plugin = buildPlugin(require);
const withInputQueue = makeWithQueue();
const queueThunkForKernel = withInputQueue(async thunk => {
thunk();
// eslint-disable-next-line no-use-before-define
await processKernel();
});

const plugin = buildPlugin(require, queueThunkForKernel);

let config = loadSwingsetConfigFile(`${vatsDir}/solo-config.json`);
if (config === null) {
Expand Down Expand Up @@ -127,8 +134,6 @@ async function buildSwingset(
}
}

const withInputQueue = makeWithQueue();

// Use the input queue to make sure it doesn't overlap with
// other inbound messages.
const queuedDeliverInboundToMbx = withInputQueue(
Expand Down
6 changes: 6 additions & 0 deletions packages/eventual-send/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ export function makeHandledPromise() {
}
return t(...args);
}
if (!t) {
const ftype = typeof t;
throw new TypeError(
`target cannot contain [${method}], typeof is ${ftype}`,
);
}
if (!(method in t)) {
const names = Object.getOwnPropertyNames(t).sort();
throw new TypeError(`target[${method}] does not exist, has ${names}`);
Expand Down

0 comments on commit f92ee73

Please sign in to comment.