Skip to content

Commit

Permalink
fix: improved Store API
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Dec 28, 2021
1 parent 9a58425 commit 0f75e65
Show file tree
Hide file tree
Showing 33 changed files with 525 additions and 357 deletions.
9 changes: 9 additions & 0 deletions packages/SwingSet/src/kernel/virtualObjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,15 @@ export function makeVirtualObjectManager(
backingMap.delete(key);
}
},
addAll: entries => {
for (const [key, value] of entries) {
if (result.has(key)) {
result.set(key, value);
} else {
result.init(key, value);
}
}
},
});
droppedCollectionRegistry.register(
result,
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/src/vats/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export function getPrefixes(addr) {
* @returns {Protocol} the local capability for connecting and listening
*/
export function makeNetworkProtocol(protocolHandler) {
/** @type {Store<Port, Set<Closable>>} */
/** @type {LegacyMap<Port, Set<Closable>>} */
// Legacy because we're storing a JS Set
const currentConnections = makeLegacyMap('port');

Expand Down
6 changes: 3 additions & 3 deletions packages/governance/src/binaryVoteCounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ const makeBinaryVoteCounter = (questionSpec, threshold, instance) => {
let spoiled = 0n;
const tally = [0n, 0n];

allBallots.values().forEach(({ chosen, shares }) => {
for (const { chosen, shares } of allBallots.values()) {
const choice = positions.findIndex(p => sameStructure(p, chosen));
if (choice < 0) {
spoiled += shares;
} else {
tally[choice] += shares;
}
});
}

/** @type { VoteStatistics } */
const stats = {
spoiled,
votes: allBallots.entries().length,
votes: allBallots.getSize(),
results: [
{ position: positions[0], total: tally[0] },
{ position: positions[1], total: tally[1] },
Expand Down
8 changes: 4 additions & 4 deletions packages/governance/src/electorateTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ const startCounter = async (

/** @param {Store<Handle<'Question'>, QuestionRecord>} questionStore */
const getOpenQuestions = async questionStore => {
const isOpenPQuestions = questionStore
.entries()
.map(([key, { publicFacet }]) => {
const isOpenPQuestions = [...questionStore.entries()].map(
([key, { publicFacet }]) => {
return [E(publicFacet).isOpen(), key];
});
},
);

const isOpenQuestions = await allComparable(harden(isOpenPQuestions));
return isOpenQuestions
Expand Down
11 changes: 5 additions & 6 deletions packages/governance/src/paramGovernance/paramManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check

import { Far } from '@agoric/marshal';
import { Far, mapIterable } from '@agoric/marshal';
import { assertIsRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { AmountMath } from '@agoric/ertp';
import { assertKeywordName } from '@agoric/zoe/src/cleanProposal.js';
Expand Down Expand Up @@ -254,9 +254,8 @@ const makeParamManagerBuilder = zoe => {
return param.getVisibleValue(proposed);
};

const getParamList = () => {
return harden(namesToParams.keys().map(k => getParam(k)));
};
const getParamList = () =>
harden(mapIterable(namesToParams.keys(), k => getParam(k)));

// should be exposed within contracts, and not externally, for invitations
const getInternalParamValue = name => {
Expand All @@ -266,9 +265,9 @@ const makeParamManagerBuilder = zoe => {
const getParams = () => {
/** @type {Record<Keyword,ParamDescription>} */
const descriptions = {};
namesToParams.entries().forEach(([name, param]) => {
for (const [name, param] of namesToParams.entries()) {
descriptions[name] = param.makeShortDescription();
});
}
return harden(descriptions);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/pegasus/src/pegasus.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ const makePegasus = (zcf, board, namesByAddress) => {
*/

/**
* @type {WeakStore<Connection, LocalDenomState>}
* @type {LegacyWeakMap<Connection, LocalDenomState>}
*/
// Legacy because the value contains a JS Set
const connectionToLocalDenomState = makeLegacyWeakMap('Connection');
Expand Down
15 changes: 9 additions & 6 deletions packages/store/src/legacy/legacyMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { assert, details as X, q } from '@agoric/assert';
* // Legacy because...
* ```
* comment explaining the problem inhibiting conversion to the new
* system. Some of these problems as if this writing:
* system. Some of these problems as of this writing:
* * A promiseKit used as a value, even though a promiseKit is not
* a passable. Solutions are to make it a passable, or to convert
* the container back to a conventional JavaScript Map.
Expand All @@ -29,15 +29,17 @@ import { assert, details as X, q } from '@agoric/assert';
* likely work fine.
*
* @deprecated switch to ScalarMap if possible, Map otherwise
* @template K,V
* @param {string} [keyName='key'] - the column name for the key
* @returns {LegacyMap<K,V>}
*/
export const makeLegacyMap = (keyName = 'key') => {
const m = new Map();
const assertKeyDoesNotExist = key =>
assert(!m.has(key), X`${q(keyName)} already registered: ${key}`);
const assertKeyExists = key =>
assert(m.has(key), X`${q(keyName)} not found: ${key}`);
const legacyMap = harden({
return harden({
has: key => {
// Check if a key exists. The key can be any JavaScript value,
// though the answer will always be false for keys that cannot be found
Expand All @@ -60,10 +62,11 @@ export const makeLegacyMap = (keyName = 'key') => {
assertKeyExists(key);
m.delete(key);
},
keys: () => [...m.keys()],
values: () => [...m.values()],
entries: () => [...m.entries()],
keys: () => m.keys(),
values: () => m.values(),
entries: () => m.entries(),
getSize: () => m.size,
clear: () => m.clear(),
});
return legacyMap;
};
harden(makeLegacyMap);
9 changes: 6 additions & 3 deletions packages/store/src/legacy/legacyWeakMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import '../types.js';
* See doccomment in the closely related `legacyMap.js` module.
*
* @deprecated switch to ScalarWeakMap if possible, WeakMap otherwise
* @template K,V
* @param {string} [keyName='key'] - the column name for the key
* @returns {LegacyWeakMap<K,V>}
*/
export const makeLegacyWeakMap = (keyName = 'key') => {
/** @type {WeakMap<K & Object,V>} */
const wm = new WeakMap();
const assertKeyDoesNotExist = key =>
assert(!wm.has(key), X`${q(keyName)} already registered: ${key}`);
const assertKeyExists = key =>
assert(wm.has(key), X`${q(keyName)} not found: ${key}`);
const legacyWeakMap = harden({
return harden({
has: key => {
// Check if a key exists. The key can be any JavaScript value,
// though the answer will always be false for keys that cannot be found
Expand All @@ -28,7 +31,8 @@ export const makeLegacyWeakMap = (keyName = 'key') => {
},
get: key => {
assertKeyExists(key);
return wm.get(key);
// How to tell typescript I believe the `get` will succeed.
return /** @type {V} */ (wm.get(key));
},
set: (key, value) => {
assertKeyExists(key);
Expand All @@ -39,6 +43,5 @@ export const makeLegacyWeakMap = (keyName = 'key') => {
wm.delete(key);
},
});
return legacyWeakMap;
};
harden(makeLegacyWeakMap);
Loading

0 comments on commit 0f75e65

Please sign in to comment.