Skip to content

Commit

Permalink
refactor: DRY allValues, zip
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Sep 16, 2022
1 parent c4e48b0 commit 667ad5c
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 @@ -5,31 +5,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 bundleRelativeGE = 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,13 +1,3 @@
// @ts-check

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 @@ -6,7 +6,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 @@ -347,9 +347,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 @@ -223,3 +223,13 @@ export const makeMeasureSeconds = currentTimeMillisec => {
};
return measureSeconds;
};

/** @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 667ad5c

Please sign in to comment.