Skip to content

Commit

Permalink
refactor: DRY allValues, zip
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg authored and erights committed Nov 9, 2022
1 parent 7e5b0ad commit 5e32b38
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 37 deletions.
19 changes: 1 addition & 18 deletions packages/SwingSet/src/controller/initializeSwingset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,14 @@ import path from 'path';

import { resolve as resolveModuleSpecifier } from 'import-meta-resolve';
import { assert, details as X } from '@agoric/assert';
import { allValues } from '@agoric/internal';
import bundleSource from '@endo/bundle-source';

import '../types-ambient.js';
import { insistStorageAPI } from '../lib/storageAPI.js';
import { initializeKernel } from './initializeKernel.js';
import { kdebugEnable } from '../lib/kdebug.js';

/**
* @param {X[]} xs
* @param {Y[]} ys
* @returns {[X, Y][]}
* @template X, Y
*/
const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]);
const { keys, values, fromEntries } = Object;
/**
* @param {Record<string, Promise<V>>} obj
* @returns {Promise<Record<string, V>>}
* @template V
*/
const allValues = async obj => {
const vals = await Promise.all(values(obj));
return fromEntries(zip(keys(obj), vals));
};

const bundleRelative = rel =>
bundleSource(new URL(rel, import.meta.url).pathname);
const bundleRelativeCallable = rel =>
Expand Down
12 changes: 1 addition & 11 deletions packages/inter-protocol/src/collect.js
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
const { fromEntries, keys, values } = Object;

/** @type { <X, Y>(xs: X[], ys: Y[]) => [X, Y][]} */
export const zip = (xs, ys) => harden(xs.map((x, i) => [x, ys[+i]]));

/** @type { <K extends string, V>(obj: Record<K, ERef<V>>) => Promise<Record<K, V>> } */
export const allValues = async obj => {
const resolved = await Promise.all(values(obj));
// @ts-expect-error cast
return harden(fromEntries(zip(keys(obj), resolved)));
};
export { allValues, zip } from '@agoric/internal';
5 changes: 1 addition & 4 deletions packages/inter-protocol/src/proposals/committee-proposal.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// @ts-check
import { deeplyFulfilledObject } from '@agoric/internal';
import { deeplyFulfilledObject, zip } from '@agoric/internal';
import { E } from '@endo/far';
import { reserveThenDeposit } from './utils.js';

const { values } = Object;

/** @type { <X, Y>(xs: X[], ys: Y[]) => [X, Y][]} */
const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]);

/**
* @param {import('./econ-behaviors').EconomyBootstrapPowers} powers
* @param {{ options: { voterAddresses: Record<string, string> }}} param1
Expand Down
5 changes: 1 addition & 4 deletions packages/inter-protocol/src/proposals/startPSM.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { makeStorageNodeChild } from '@agoric/vats/src/lib-chainStorage.js';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { E } from '@endo/far';
import { Stable } from '@agoric/vats/src/tokens.js';
import { deeplyFulfilledObject } from '@agoric/internal';
import { deeplyFulfilledObject, zip } from '@agoric/internal';
import { makeScalarMapStore } from '@agoric/vat-data';

import { reserveThenDeposit, reserveThenGetNamePaths } from './utils.js';
Expand Down Expand Up @@ -360,9 +360,6 @@ export const PSM_GOV_MANIFEST = {
},
};

/** @type { <X, Y>(xs: X[], ys: Y[]) => [X, Y][]} */
const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]);

/**
* @param {import('./econ-behaviors').EconomyBootstrapPowers} powers
* @param {{ options: { voterAddresses: Record<string, string> }}} param1
Expand Down
10 changes: 10 additions & 0 deletions packages/internal/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,13 @@ export const fsStreamReady = stream =>
stream.on('ready', onReady);
stream.on('error', onError);
});

/** @type { <X, Y>(xs: X[], ys: Y[]) => [X, Y][]} */
export const zip = (xs, ys) => harden(xs.map((x, i) => [x, ys[+i]]));

/** @type { <K extends string, V>(obj: Record<K, V | PromiseLike<V>>) => Promise<Record<K, V>> } */
export const allValues = async obj => {
const resolved = await Promise.all(Object.values(obj));
// @ts-expect-error cast
return harden(fromEntries(zip(Object.keys(obj), resolved)));
};

0 comments on commit 5e32b38

Please sign in to comment.