Skip to content

Commit

Permalink
feat(solo): add keys, init, and delete to home.scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Sep 10, 2021
1 parent a6c7bf8 commit 1cecdcb
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions packages/solo/src/scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@ import { Far } from '@agoric/marshal';

export default function makeScratchPad() {
const map = new Map();

const keys = async () => {
const keyList = [...map.keys()];
return harden(keyList.sort());
};

return Far('scratchPad', {
get: async idP => {
const id = await idP;
return map.get(id);
delete: async keyP => {
const key = await keyP;
map.delete(key);
},
set: async (idP, objP) => {
const [id, obj] = await Promise.all([idP, objP]);
map.set(id, obj);
return id;
get: async keyP => {
const key = await keyP;
return map.get(key);
},
init: async (idP, objP) => {
const [id, obj] = await Promise.all([idP, objP]);
if (map.has(id)) {
throw Error(`Scratchpad already has id ${id}`);
// Initialize a key only if it doesn't already exist. Needed for atomicity
// between multiple invocations.
init: async (keyP, objP) => {
const [key, obj] = await Promise.all([keyP, objP]);
if (map.has(key)) {
throw Error(`Scratchpad already has key ${key}`);
}
map.set(id, obj);
return id;
map.set(key, obj);
return key;
},
list: async () => {
const ids = [...map.keys()];
return harden(ids.sort());
keys,
// Legacy alias for `keys`.
list: keys,
set: async (keyP, objP) => {
const [key, obj] = await Promise.all([keyP, objP]);
map.set(key, obj);
return key;
},
});
}

0 comments on commit 1cecdcb

Please sign in to comment.