Skip to content

Commit

Permalink
docs(patterns): consistent type constraints for makeExo
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 17, 2023
1 parent 9e026eb commit e656f14
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
40 changes: 22 additions & 18 deletions packages/store/src/patterns/exo-makers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ export const initEmpty = () => emptyRecord;
*/

/**
* @template A args to init
* @template S state from init
* @template {Record<string | symbol, CallableFunction>} T methods
* @template {(...args: any[]) => any} I init function
* @template {Record<string | symbol, CallableFunction>} M methods
* @param {string} tag
* @param {any} interfaceGuard
* @param {(...args: A[]) => S} init
* @param {T & ThisType<{ self: T, state: S }>} methods
* @param {FarClassOptions<Context<S,T>>} [options]
* @returns {(...args: A[]) => (T & import('@endo/eventual-send').RemotableBrand<{}, T>)}
* @param {I} init
* @param {M & ThisType<{ self: M, state: ReturnType<I> }>} methods
* @param {FarClassOptions<Context<ReturnType<I>, M>>} [options]
* @returns {(...args: Parameters<I>) => (M & import('@endo/eventual-send').RemotableBrand<{}, M>)}
*/
export const defineExoClass = (
tag,
Expand All @@ -64,7 +63,7 @@ export const defineExoClass = (
methods,
{ finish = undefined } = {},
) => {
/** @type {WeakMap<T,Context<S, T>>} */
/** @type {WeakMap<M,Context<ReturnType<I>, M>>} */
const contextMap = new WeakMap();
const prototype = defendPrototype(
tag,
Expand All @@ -73,14 +72,17 @@ export const defineExoClass = (
true,
interfaceGuard,
);
/**
* @param {Parameters<I>} args
*/
const makeInstance = (...args) => {
// Be careful not to freeze the state record
const state = seal(init(...args));
/** @type {T} */
/** @type {M} */
// @ts-expect-error could be instantiated with different subtype
const self = harden({ __proto__: prototype });
// Be careful not to freeze the state record
/** @type {Context<S,T>} */
/** @type {Context<ReturnType<I>,M>} */
const context = freeze({ state, self });
contextMap.set(self, context);
if (finish) {
Expand All @@ -94,15 +96,14 @@ export const defineExoClass = (
harden(defineExoClass);

/**
* @template A args to init
* @template S state from init
* @template {Record<string, Record<string | symbol, CallableFunction>>} F methods
* @template {(...args: any[]) => any} I init function
* @template {Record<string, Record<string | symbol, CallableFunction>>} F facet methods
* @param {string} tag
* @param {any} interfaceGuardKit
* @param {(...args: A[]) => S} init
* @param {F & ThisType<{ facets: F, state: S }> } methodsKit
* @param {FarClassOptions<KitContext<S,F>>} [options]
* @returns {(...args: A[]) => F}
* @param {I} init
* @param {F & ThisType<{ facets: F, state: ReturnType<I> }> } methodsKit
* @param {FarClassOptions<KitContext<ReturnType<I>,F>>} [options]
* @returns {(...args: Parameters<I>) => F}
*/
export const defineExoClassKit = (
tag,
Expand All @@ -119,6 +120,9 @@ export const defineExoClassKit = (
true,
interfaceGuardKit,
);
/**
* @param {Parameters<I>} args
*/
const makeInstanceKit = (...args) => {
// Be careful not to freeze the state record
const state = seal(init(...args));
Expand All @@ -145,7 +149,7 @@ export const defineExoClassKit = (
harden(defineExoClassKit);

/**
* @template {Record<string, Method>} T
* @template {Record<string | symbol, CallableFunction>} T
* @param {string} tag
* @param {InterfaceGuard | undefined} interfaceGuard CAVEAT: static typing does not yet support `callWhen` transformation
* @param {T} methods
Expand Down
1 change: 1 addition & 0 deletions packages/store/test/test-heap-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ test('test makeExo', t => {
t.throws(() => upCounter.incr(-3), {
message: 'In "incr" method of (upCounter): arg 0?: -3 - Must be >= 0',
});
// @ts-expect-error deliberately bad arg for testing
t.throws(() => upCounter.incr('foo'), {
message:
'In "incr" method of (upCounter): arg 0?: string "foo" - Must be a number',
Expand Down

0 comments on commit e656f14

Please sign in to comment.