Skip to content

Commit

Permalink
fixup! fix(vow): correct the typing of unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jun 14, 2024
1 parent 74e2268 commit fd3a6f0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
9 changes: 8 additions & 1 deletion packages/internal/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-classes-per-file */
import type { RemotableBrand } from '@endo/eventual-send';
import type { ERef, RemotableBrand } from '@endo/eventual-send';
import type { Primitive } from '@endo/pass-style';
import type { Callable } from './utils.js';

Expand Down Expand Up @@ -56,3 +56,10 @@ export type DataOnly<T> =
export type Remote<Primary, Local = DataOnly<Primary>> =
| Primary
| RemotableBrand<Local, Primary>;

/**
* Potentially remote promises or settled references.
*/
export type FarRef<Primary, Local = DataOnly<Primary>> = ERef<
Remote<Primary, Local>
>;
28 changes: 15 additions & 13 deletions packages/vow/src/E.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const onSend = makeMessageBreakpointTester('ENDO_SEND_BREAKPOINTS');

/**
* @import { HandledPromiseConstructor, RemotableBrand } from '@endo/eventual-send'
* @import { Unwrap } from './types.js'
* @import { EUnwrap } from './types.js'
*/
/** @typedef {(...args: unknown[]) => any} Callable */

Expand Down Expand Up @@ -43,7 +43,7 @@ const baseFreezableProxyHandler = {
*
* @param {any} recipient Any value passed to E(x)
* @param {HandledPromiseConstructor} HandledPromise
* @param {<T>(x: T) => Promise<Unwrap<T>>} unwrap
* @param {<T>(x: T) => Promise<EUnwrap<T>>} unwrap
* @returns {ProxyHandler<any>} the Proxy handler
*/
const makeEProxyHandler = (recipient, HandledPromise, unwrap) =>
Expand Down Expand Up @@ -108,7 +108,7 @@ const makeEProxyHandler = (recipient, HandledPromise, unwrap) =>
*
* @param {any} recipient Any value passed to E.sendOnly(x)
* @param {HandledPromiseConstructor} HandledPromise
* @param {<T>(x: T) => Promise<Unwrap<T>>} unwrap
* @param {<T>(x: T) => Promise<EUnwrap<T>>} unwrap
* @returns {ProxyHandler<any>} the Proxy handler
*/
const makeESendOnlyProxyHandler = (recipient, HandledPromise, unwrap) =>
Expand Down Expand Up @@ -171,7 +171,7 @@ const makeESendOnlyProxyHandler = (recipient, HandledPromise, unwrap) =>
*
* @param {any} x Any value passed to E.get(x)
* @param {HandledPromiseConstructor} HandledPromise
* @param {<T>(x: T) => Promise<Unwrap<T>>} unwrap
* @param {<T>(x: T) => Promise<EUnwrap<T>>} unwrap
* @returns {ProxyHandler<any>} the Proxy handler
*/
const makeEGetProxyHandler = (x, HandledPromise, unwrap) =>
Expand All @@ -188,7 +188,7 @@ const resolve = x => HandledPromise.resolve(x);
* @template [A={}]
* @param {HandledPromiseConstructor} HandledPromise
* @param {object} [powers]
* @param {<T>(x: T) => Promise<Unwrap<T>>} [powers.unwrap]
* @param {<T>(x: T) => Promise<EUnwrap<T>>} [powers.unwrap]
* @param {A} [powers.additional]
*/
const makeE = (HandledPromise, powers = {}) => {
Expand Down Expand Up @@ -265,10 +265,10 @@ const makeE = (HandledPromise, powers = {}) => {
* unwrap(x).then(onfulfilled, onrejected)
*
* @template T
* @template [TResult1=Unwrap<T>]
* @template [TResult1=EUnwrap<T>]
* @template [TResult2=never]
* @param {ERef<T>} x value to convert to a handled promise
* @param {(value: Unwrap<T>) => ERef<TResult1>} [onfulfilled]
* @param {(value: EUnwrap<T>) => ERef<TResult1>} [onfulfilled]
* @param {(reason: any) => ERef<TResult2>} [onrejected]
* @returns {Promise<TResult1 | TResult2>}
* @readonly
Expand Down Expand Up @@ -309,11 +309,11 @@ export default makeE;
/**
* @template {Callable} T
* @typedef {(
* ReturnType<T> extends Unwrap<ReturnType<T>>
* ReturnType<T> extends EUnwrap<ReturnType<T>>
* ? (...args: Parameters<T>) => Promise<ReturnType<T>>
* : ReturnType<T> extends Promise<Unwrap<ReturnType<T>>>
* : ReturnType<T> extends Promise<EUnwrap<ReturnType<T>>>
* ? T
* : (...args: Parameters<T>) => Promise<Unwrap<ReturnType<T>>>
* : (...args: Parameters<T>) => Promise<EUnwrap<ReturnType<T>>>
* )} ECallable
*/

Expand Down Expand Up @@ -390,6 +390,8 @@ export default makeE;
* @typedef {(
* T extends Callable
* ? (...args: Parameters<T>) => ReturnType<T> // a root callable, no methods
* : FilteredKeys<T, Callable> extends never
* ? never
* : Pick<T, FilteredKeys<T, Callable>> // any callable methods
* )} PickCallable
*/
Expand All @@ -399,9 +401,9 @@ export default makeE;
*
* @template T
* @typedef {(
* T extends RemotableBrand<infer _, infer R> // if a given T will settle to some remote interface R
* EUnwrap<T> extends RemotableBrand<infer _, infer R> // if a given T will settle to some remote interface R
* ? PickCallable<R> // then return the callable properties of R
* : PickCallable<Unwrap<T>> // otherwise return the callable properties of the settled T
* : PickCallable<EUnwrap<T>> // otherwise return the callable properties of the settled T
* )} RemoteFunctions
*/

Expand All @@ -410,7 +412,7 @@ export default makeE;
* @typedef {(
* T extends RemotableBrand<infer L, infer _>
* ? L
* : Unwrap<T>
* : EUnwrap<T>
* )} LocalRecord
*/

Expand Down
12 changes: 4 additions & 8 deletions packages/vow/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
export {};

/**
* @import {RemotableBrand} from '@endo/eventual-send'
* @import {CopyTagged} from '@endo/pass-style'
* @import {RemotableObject} from '@endo/pass-style';
* @import {IsPrimitive, Remote} from '@agoric/internal';
* @import {Remote} from '@agoric/internal';
* @import {prepareVowTools} from './tools.js'
*/

Expand Down Expand Up @@ -35,13 +34,10 @@ export {};
* This is used within E, so we must narrow the type to its remote form.
* @template T
* @typedef {(
* T extends Vow<infer U> ? Unwrap<U> :
* T extends Promise<infer U> ? Unwrap<U> :
* T extends PromiseLike<infer U> ? Unwrap<U> :
* IsPrimitive<T> extends true ? T :
* T extends RemotableBrand<infer Local, infer _> ? Local & T :
* T extends Vow<infer U> ? EUnwrap<U> :
* T extends PromiseLike<infer U> ? EUnwrap<U> :
* T
* )} Unwrap
* )} EUnwrap
*/

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/vow/src/when.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check
import { getVowPayload, basicE } from './vow-utils.js';

/** @import { IsRetryableReason, Unwrap } from './types.js' */
/** @import { IsRetryableReason, EUnwrap } from './types.js' */

/**
* @param {IsRetryableReason} [isRetryableReason]
Expand All @@ -13,10 +13,10 @@ export const makeWhen = (
* Shorten `specimenP` until we achieve a final result.
*
* @template T
* @template [TResult1=Unwrap<T>]
* @template [TResult1=EUnwrap<T>]
* @template [TResult2=never]
* @param {T} specimenP value to unwrap
* @param {(value: Unwrap<T>) => TResult1 | PromiseLike<TResult1>} [onFulfilled]
* @param {(value: EUnwrap<T>) => TResult1 | PromiseLike<TResult1>} [onFulfilled]
* @param {(reason: any) => TResult2 | PromiseLike<TResult2>} [onRejected]
* @returns {Promise<TResult1 | TResult2>}
*/
Expand Down Expand Up @@ -50,7 +50,7 @@ export const makeWhen = (
payload = getVowPayload(result);
}

const unwrapped = /** @type {Unwrap<T>} */ (result);
const unwrapped = /** @type {EUnwrap<T>} */ (result);

// We've extracted the final result.
if (onFulfilled == null && onRejected == null) {
Expand Down

0 comments on commit fd3a6f0

Please sign in to comment.