Skip to content

Commit

Permalink
feat(vowTools): asVow should not wrap a vow as a vow
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Jun 25, 2024
1 parent b6b5f5f commit 0cdcd5f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/vow/src/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { prepareWatchUtils } from './watch-utils.js';
import { makeAsVow } from './vow-utils.js';

/** @import {Zone} from '@agoric/base-zone' */
/** @import {IsRetryableReason, Vow} from './types.js' */
/** @import {IsRetryableReason} from './types.js' */

/**
* @param {Zone} zone
Expand Down
11 changes: 8 additions & 3 deletions packages/vow/src/vow-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,17 @@ export const makeAsVow = makeVowKit => {
* @returns {Vow<Awaited<T>>}
*/
const asVow = fn => {
const kit = makeVowKit();
let result;
try {
kit.resolver.resolve(fn());
result = fn();
} catch (e) {
kit.resolver.reject(e);
result = Promise.reject(e);
}
if (isVow(result)) {
return result;
}
const kit = makeVowKit();
kit.resolver.resolve(result);
return kit.vow;
};
return harden(asVow);
Expand Down
32 changes: 27 additions & 5 deletions packages/vow/test/asVow.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
// @ts-check
import test from 'ava';

import { E } from '@endo/far';
import { makeHeapZone } from '@agoric/base-zone/heap.js';

import { prepareVowTools } from '../src/tools.js';
import { isVow } from '../src/vow-utils.js';
import { getVowPayload, isVow } from '../src/vow-utils.js';

test('asVow takes a function that throws/returns synchronously and returns a vow', async t => {
const zone = makeHeapZone();
const { watch, when, asVow } = prepareVowTools(zone);
const { watch, when, asVow } = prepareVowTools(makeHeapZone());

const fnThatThrows = () => {
throw Error('fail');
};

const vowWithRejection = asVow(fnThatThrows);
t.true(isVow(vowWithRejection));
await t.throwsAsync(when(vowWithRejection), { message: 'fail' }, 'failure ');
await t.throwsAsync(
when(vowWithRejection),
{ message: 'fail' },
'error should propogate as promise rejection',
);

const isWatchAble = watch(asVow(fnThatThrows));
t.true(isVow(vowWithRejection));
await t.throwsAsync(when(isWatchAble), { message: 'fail' }, 'failure ');
await t.throwsAsync(when(isWatchAble), { message: 'fail' });

const fnThatReturns = () => {
return 'early return';
Expand All @@ -30,3 +34,21 @@ test('asVow takes a function that throws/returns synchronously and returns a vow
t.is(await when(vowWithReturn), 'early return');
t.is(await when(watch(vowWithReturn)), 'early return');
});

test('asVow does not resolve a vow to a vow', async t => {
const { watch, when, asVow } = prepareVowTools(makeHeapZone());

const testVow = watch(Promise.resolve('payload'));
const testVowAsVow = asVow(() => testVow);

const vowPayload = getVowPayload(testVowAsVow);
assert(vowPayload?.vowV0, 'testVowAsVow is a vow');
const unwrappedOnce = await E(vowPayload.vowV0).shorten();
t.false(
isVow(unwrappedOnce),
'vows passed to asVow are not rewrapped as vows',
);
t.is(unwrappedOnce, 'payload');

t.is(await when(testVow), await when(testVowAsVow), 'result is preserved');
});

0 comments on commit 0cdcd5f

Please sign in to comment.