Skip to content

Commit

Permalink
refactor: clean up types some more
Browse files Browse the repository at this point in the history
Closes #1707, closes #1465
  • Loading branch information
michaelfig committed Sep 5, 2020
1 parent 401dcb2 commit 93399c8
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 37 deletions.
17 changes: 15 additions & 2 deletions packages/cosmic-swingset/lib/ag-solo/vats/lib-board.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
// @ts-check
/* global harden */

import { generateSparseInts } from '@agoric/sparse-ints';
import { assert, details } from '@agoric/assert';
import makeStore from '@agoric/store';

/**
* @typedef {Object} Board
* @property {(id: string) => any} getValue
* @property {(value: any) => string} getId
* @property {(value: any) => boolean} has
* @property {() => string[]} ids```
*/

/**
* Create a board to post things on.
* @param {number} [seed=0]
* @returns {Board}
*/
function makeBoard(seed = 0) {
const idToVal = makeStore('boardId');
const valToId = makeStore('value');
const sparseInts = generateSparseInts(seed);

/** @type {Board} */
const board = harden({
// Add if not already present
getId: value => {
Expand Down Expand Up @@ -37,5 +52,3 @@ function makeBoard(seed = 0) {
}

export { makeBoard };

/** @typedef {ReturnType<typeof makeBoard>} Board */
11 changes: 7 additions & 4 deletions packages/sparse-ints/src/sparseInts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

/* global harden */

// Generator function to produce a stream of positive integers that are
// sparsely scattered across the number space. This supports IDs that
// are guessable, but for example mistyping a correct ID is unlikely to
// mistakenly match another generated ID.
/**
* Generator function to produce a stream of positive integers that are
* sparsely scattered across the number space. This supports IDs that
* are guessable, but for example mistyping a correct ID is unlikely to
* mistakenly match another generated ID.
* @returns {Generator<number, number, number>}
*/
function* generateSparseInts(seed) {
// This is a linear-feedback shift register with computed startState.
// Thus, it is totally deterministic, but at least looks a little random
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/docs/seats.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The type of the ZCFSeat is:
/**
* @typedef {Object} ZCFSeat
* @property {() => void} exit
* @property {(msg: string=) => never} kickOut
* @property {(msg?: string) => never} kickOut
* @property {() => Notifier<Allocation>} getNotifier
* @property {() => boolean} hasExited
* @property {() => ProposalRecord} getProposal
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/contractFacet/offerSafety.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* allocationAmount greater than or equal to requiredAmount for every
* keyword of giveOrWant?
* @param {(brand: Brand) => AmountMath} getAmountMath
* @param {ProposalRecord["give"] | ProposalRecord["want"]} giveOrWant
* @param {AmountKeywordRecord} giveOrWant
* @param {AmountKeywordRecord} allocation
*/
const satisfiesInternal = (getAmountMath, giveOrWant = {}, allocation) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/contractFacet/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @property {(brand: Brand) => Issuer} getIssuerForBrand
* @property {GetAmountMath} getAmountMath
* @property {MakeZCFMint} makeZCFMint
* @property {(exit: ExitRule=) => ZcfSeatKit} makeEmptySeatKit
* @property {(exit?: ExitRule) => ZcfSeatKit} makeEmptySeatKit
*/

/**
Expand Down Expand Up @@ -96,7 +96,7 @@
* @callback MakeZCFMint
* @param {Keyword} keyword
* @param {AmountMathKind=} amountMathKind
* @returns {ZCFMint}
* @returns {Promise<ZCFMint>}
*/

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/zoe/src/contractSupport/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* @param {ContractFacet} zcf
* @param {SeatGainsLossesRecord} left
* @param {SeatGainsLossesRecord} right
* @param {String} [leftHasExitedMsg] A custom error message if
* @param {string} [leftHasExitedMsg] A custom error message if
* the left seat has been exited already
* @param {String} [rightHasExitedMsg] A custom error message if the
* @param {string} [rightHasExitedMsg] A custom error message if the
* right seat has been exited already
* @returns {void}
*
Expand Down Expand Up @@ -45,7 +45,7 @@
* @param {ContractFacet} zcf
* @param {ZCFSeat} leftSeat
* @param {ZCFSeat} rightSeat
* @param {String} [leftHasExitedMsg]
* @param {String} [rightHasExitedMsg]
* @param {string} [leftHasExitedMsg]
* @param {string} [rightHasExitedMsg]
* @returns {string}
*/
6 changes: 3 additions & 3 deletions packages/zoe/src/contractSupport/zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ export const swap = (

/**
* @typedef ExpectedRecord
* @property {Record<keyof ProposalRecord['give'],null>} [want]
* @property {Record<keyof ProposalRecord['want'],null>} [give]
* @property {Partial<Record<keyof ProposalRecord['exit'],null>>} [exit]
* @property {Record<Keyword, null>} [want]
* @property {Record<Keyword, null>} [give]
* @property {Record<keyof ProposalRecord['exit'], null>} [exit]
*/

/**
Expand Down
1 change: 0 additions & 1 deletion packages/zoe/src/contracts/auction/secondPriceAuction.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const start = zcf => {
// seller will use 'Asset' and 'Ask'. buyer will use 'Asset' and 'Bid'
assertIssuerKeywords(zcf, harden(['Asset', 'Ask']));

/** @type {Timer} */
E(timerAuthority)
.setWakeup(
closesAfter,
Expand Down
7 changes: 1 addition & 6 deletions packages/zoe/src/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
* @property {(zoeSeatAdmin: ZoeSeatAdmin) => boolean} hasZoeSeatAdmin
* @property {(zoeSeatAdmin: ZoeSeatAdmin) => void} removeZoeSeatAdmin
* @property {() => Instance} getInstance
* @property {() => PublicFacet} getPublicFacet
* @property {() => Object} getPublicFacet
* @property {() => IssuerKeywordRecord} getIssuers
* @property {() => BrandKeywordRecord} getBrands
* @property {() => Object} getTerms
Expand Down Expand Up @@ -235,8 +235,3 @@
* @property {() => Object} adminData
* provides some statistics about the vat in which the contract is running.
*/

/**
* @template T
* @typedef {import('@agoric/promise-kit').ERef<T>} ERef
*/
8 changes: 7 additions & 1 deletion packages/zoe/src/objArrayConversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { assert, details, q } from '@agoric/assert';

/**
* @typedef {bigint|boolean|null|number|string|symbol|undefined} Primitive
* @typedef {string|number|symbol} PropertyName
*/

/**
* @template T
* @template {string | number | symbol} U
* @template {PropertyName} U
* @param {T[]} array
* @param {U[]} keys
*/
Expand All @@ -24,6 +25,11 @@ export const arrayToObj = (array, keys) => {
return obj;
};

/**
* Assert all values from `part` appear in `whole`.
* @param {string[]} whole
* @param {string[]} part
*/
export const assertSubset = (whole, part) => {
part.forEach(key => {
assert.typeof(key, 'string');
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @property {Issuer} issuer
* @property {AmountMath} amountMath
*
* @typedef {Record<Keyword,Amount>} Allocation
* @typedef {AmountKeywordRecord} Allocation
* @typedef {Record<Keyword,AmountMath>} AmountMathKeywordRecord
*/

Expand Down
27 changes: 17 additions & 10 deletions packages/zoe/src/zoeService/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
*
* @typedef {{give: AmountKeywordRecord,
* want: AmountKeywordRecord,
* exit: ExitRule
* exit: ExitRule<any>
* }} ProposalRecord
*/

Expand All @@ -140,17 +140,24 @@
* @typedef {Object} Waker
* @property {() => void} wake
*
* @typedef {Object} Timer
* @property {(deadline: Deadline, wakerP: ERef<Waker>) => void} setWakeup
*
* @typedef {number} Deadline
*
* @typedef {{waived:null}} Waived
* @typedef {{onDemand:null}} OnDemand
*
* @typedef {{afterDeadline:{timer:Timer, deadline:Deadline}}} AfterDeadline
*
* @typedef {Partial<Waived>&Partial<OnDemand>&Partial<AfterDeadline>} ExitRule
*/

/**
* @template Deadline
* @typedef {Object} Timer
* @property {(deadline: Deadline, wakerP: ERef<Waker>) => void} setWakeup
*/

/**
* @template Deadline
* @typedef {{afterDeadline:{timer:Timer<Deadline>, deadline:Deadline}}} AfterDeadline
*/

/**
* @template Deadline
* @typedef {Waived | OnDemand | AfterDeadline<Deadline>} ExitRule
* The possible keys are 'waived', 'onDemand', and 'afterDeadline'.
* `timer` and `deadline` only are used for the `afterDeadline` key.
* The possible records are:
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/zcf/test-zcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const setupZCFTest = async (issuerKeywordRecord, terms) => {
// * @property {MakeInvitation} makeInvitation
// * @property {() => void} shutdown
// * @property {MakeZCFMint} makeZCFMint
// * @property {(exit: ExitRule=) => ZcfSeatKit} makeEmptySeatKit
// * @property {<Deadline>(exit?: ExitRule<Deadline>) => ZcfSeatKit} makeEmptySeatKit

test(`zcf.getZoeService`, async t => {
const { zoe, zcf } = await setupZCFTest();
Expand Down

0 comments on commit 93399c8

Please sign in to comment.