Skip to content

Commit

Permalink
fix: clean up E.when and E.resolve (#1561)
Browse files Browse the repository at this point in the history
* fix: clean up E.when and E.resolve
  • Loading branch information
erights authored Aug 20, 2020
1 parent 4329a8e commit 634046c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
15 changes: 8 additions & 7 deletions packages/ERTP/src/issuer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { assert, details } from '@agoric/assert';
import makeStore from '@agoric/weak-store';
import { E } from '@agoric/eventual-send';
import { isPromise } from '@agoric/promise-kit';

import { makeAmountMath, MathKind } from './amountMath';
Expand All @@ -20,7 +21,7 @@ function makeIssuerKit(allegedName, amountMathKind = MathKind.NAT) {

const brand = harden({
isMyIssuer: allegedIssuerP => {
return Promise.resolve(allegedIssuerP).then(allegedIssuer => {
return E.when(allegedIssuerP, allegedIssuer => {
// eslint-disable-next-line no-use-before-define
return allegedIssuer === issuer;
});
Expand Down Expand Up @@ -185,19 +186,19 @@ function makeIssuerKit(allegedName, amountMathKind = MathKind.NAT) {
},

isLive: paymentP => {
return Promise.resolve(paymentP).then(payment => {
return E.when(paymentP, payment => {
return paymentLedger.has(payment);
});
},
getAmountOf: paymentP => {
return Promise.resolve(paymentP).then(payment => {
return E.when(paymentP, payment => {
assertKnownPayment(payment);
return paymentLedger.get(payment);
});
},

burn: (paymentP, optAmount = undefined) => {
return Promise.resolve(paymentP).then(payment => {
return E.when(paymentP, payment => {
assertKnownPayment(payment);
const paymentBalance = paymentLedger.get(payment);
assertAmountEqual(paymentBalance, optAmount);
Expand All @@ -207,7 +208,7 @@ function makeIssuerKit(allegedName, amountMathKind = MathKind.NAT) {
});
},
claim: (paymentP, optAmount = undefined) => {
return Promise.resolve(paymentP).then(srcPayment => {
return E.when(paymentP, srcPayment => {
assertKnownPayment(srcPayment);
const srcPaymentBalance = paymentLedger.get(srcPayment);
assertAmountEqual(srcPaymentBalance, optAmount);
Expand Down Expand Up @@ -242,7 +243,7 @@ function makeIssuerKit(allegedName, amountMathKind = MathKind.NAT) {
},
// payment to two payments, A and B
split: (paymentP, paymentAmountA) => {
return Promise.resolve(paymentP).then(srcPayment => {
return E.when(paymentP, srcPayment => {
paymentAmountA = amountMath.coerce(paymentAmountA);
assertKnownPayment(srcPayment);
const srcPaymentBalance = paymentLedger.get(srcPayment);
Expand All @@ -261,7 +262,7 @@ function makeIssuerKit(allegedName, amountMathKind = MathKind.NAT) {
});
},
splitMany: (paymentP, amounts) => {
return Promise.resolve(paymentP).then(srcPayment => {
return E.when(paymentP, srcPayment => {
assertKnownPayment(srcPayment);
amounts = amounts.map(amountMath.coerce);
// Commit point
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-swingset/lib/ag-solo/vats/ibc.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export function makeIBCProtocolHandler(E, callIBCDevice) {
localAddr,
remoteAddr,
);
const connP = E.when(conn);
const connP = E.resolve(conn);
channelKeyToConnP.init(channelKey, connP);
},
onReceive,
Expand Down
6 changes: 3 additions & 3 deletions packages/eventual-send/src/E.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// eslint-disable-next-line spaced-comment
/// <reference path="index.d.ts" />

const readOnlyProxy = {
const readOnlyProxyHandler = {
set(_target, _prop, _value) {
return false;
},
Expand All @@ -26,7 +26,7 @@ const readOnlyProxy = {
*/
function EProxyHandler(x, HandledPromise) {
return harden({
...readOnlyProxy,
...readOnlyProxyHandler,
get(_target, p, _receiver) {
if (`${p}` !== p) {
return undefined;
Expand Down Expand Up @@ -56,7 +56,7 @@ export default function makeE(HandledPromise) {

const makeEGetterProxy = x =>
new Proxy(Object.create(null), {
...readOnlyProxy,
...readOnlyProxyHandler,
has(_target, _prop) {
return true;
},
Expand Down
5 changes: 3 additions & 2 deletions packages/eventual-send/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ interface EProxy {
readonly G<T>(x: T): ESingleGet<Unpromise<T>>;

/**
* E.when(x) converts x to a promise.
* E.resolve(x) converts x to a handled promise. It is
* shorthand for HandledPromise.resolve(x)
*/
readonly when<T>(x: T): Promise<Unpromise<T>>;
readonly resolve<T>(x: T): Promise<Unpromise<T>>;

/**
* E.when(x, res, rej) is equivalent to
Expand Down
3 changes: 2 additions & 1 deletion packages/marshal/marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const remotableToInterface = new WeakMap();
* Simple semantics, just tell what interface (or undefined) a remotable has.
*
* @param {*} maybeRemotable the value to check
* @returns {InterfaceSpec|undefined} the interface specification, or undefined if not a Remotable
* @returns {InterfaceSpec|undefined} the interface specification, or undefined
* if not a Remotable
*/
export function getInterfaceOf(maybeRemotable) {
return remotableToInterface.get(maybeRemotable);
Expand Down
3 changes: 2 additions & 1 deletion packages/notifier/src/asyncIterableAdaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export const updateFromIterable = (updater, asyncIterable) => {
const iterator = asyncIterable[Symbol.asyncIterator]();
return new Promise(ack => {
const recur = () => {
E.when(iterator.next()).then(
E.when(
iterator.next(),
({ value, done }) => {
if (done) {
updater.finish && updater.finish(value);
Expand Down
10 changes: 6 additions & 4 deletions packages/notifier/test/test-notifier-adaptor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
import '@agoric/install-ses';
import { E } from '@agoric/eventual-send';
import test from 'tape-promise/tape';
import {
makeAsyncIterableFromNotifier,
Expand Down Expand Up @@ -42,7 +43,8 @@ const finiteStream = makeIterable(false);
const explodingStream = makeIterable(true);

const testEnding = (t, p, fails) => {
return Promise.resolve(p).then(
return E.when(
p,
result => {
t.equal(fails, false);
t.equal(result, refResult);
Expand Down Expand Up @@ -71,18 +73,18 @@ const testManualConsumer = (t, iterable, lossy) => {
const testLoop = i => {
return iterator.next().then(
({ value, done }) => {
i = skip(i, value, lossy);
if (done) {
t.equal(i, payloads.length);
return value;
}
i = skip(i, value, lossy);
t.assert(i < payloads.length);
// Need precise equality
t.assert(Object.is(value, payloads[i]));
return testLoop(i + 1);
},
reason => {
t.equal(i, payloads.length);
t.assert(i <= payloads.length);
throw reason;
},
);
Expand All @@ -101,7 +103,7 @@ const testAutoConsumer = async (t, iterable, lossy) => {
i += 1;
}
} finally {
t.equal(i, payloads.length);
t.assert(i <= payloads.length);
}
// The for-await-of loop cannot observe the final value of the iterator
// so this consumer cannot test what that was. Just return what testEnding
Expand Down

0 comments on commit 634046c

Please sign in to comment.