From 1cecdcb7dd571146164aba1e8eda0b6ad91a975a Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Thu, 9 Sep 2021 18:24:00 -0600 Subject: [PATCH] feat(solo): add `keys`, `init`, and `delete` to `home.scratch` --- packages/solo/src/scratch.js | 43 ++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/packages/solo/src/scratch.js b/packages/solo/src/scratch.js index cf56cd93d29..96e93bde3fd 100644 --- a/packages/solo/src/scratch.js +++ b/packages/solo/src/scratch.js @@ -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; }, }); }