Skip to content

Commit

Permalink
fix: consolidate legacy adaptation. Add done adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Jul 25, 2020
1 parent d66b933 commit de99eb1
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/notifier/src/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ import { producePromise } from '@agoric/produce-promise';
* @property {Updater<T>} updater the (closely-held) notifier producer
*/

/**
* Whether to enable deprecated legacy features to support legacy clients
* during the transition. TODO once all clients are updated to the new API,
* remove this flag and all code enabled by this flag.
*/
const supportLegacy = true;

/**
* Produces a pair of objects, which allow a service to produce a stream of
* update promises.
Expand All @@ -78,8 +85,18 @@ export const produceNotifier = (...args) => {
let currentUpdateCount = 1; // avoid falsy numbers
let currentResponse;

if (args.length === 0) {
// Transitional kludge. TODO remove
const hasState = () => currentResponse !== undefined;

const final = () => currentUpdateCount === undefined;

const extraProperties = () =>
supportLegacy
? {
updateHandle: currentUpdateCount,
done: final(),
}
: {};
if (supportLegacy && args.length === 0) {
// To accommodate old clients, for now we treat no initial state
// argument as if the argument defaults to undefined, as it used to.
args = [undefined];
Expand All @@ -90,15 +107,11 @@ export const produceNotifier = (...args) => {
currentResponse = harden({
value: args[0],
updateCount: currentUpdateCount,
updateHandle: currentUpdateCount, // deprecated. TODO remove
...extraProperties(),
});
}
// else start as !hasState() && !final()

const hasState = () => currentResponse !== undefined;

const final = () => currentUpdateCount === undefined;

// NaN matches nothing
function getUpdateSince(updateCount = NaN) {
if (
Expand All @@ -123,7 +136,7 @@ export const produceNotifier = (...args) => {
currentResponse = harden({
value: state,
updateCount: currentUpdateCount,
updateHandle: currentUpdateCount, // deprecated. TODO remove
...extraProperties(),
});
nextPromiseKit.resolve(currentResponse);
nextPromiseKit = producePromise();
Expand All @@ -138,8 +151,8 @@ export const produceNotifier = (...args) => {
currentUpdateCount = undefined;
currentResponse = harden({
value: finalState,
updateCount: undefined,
updateHandle: undefined, // deprecated. TODO remove
updateCount: currentUpdateCount,
...extraProperties(),
});
nextPromiseKit.resolve(currentResponse);
nextPromiseKit = undefined;
Expand All @@ -162,8 +175,8 @@ export const produceNotifier = (...args) => {
const updater = harden({
updateState,
fulfill,
resolve: fulfill, // deprecated. TODO remove
reject,
...(supportLegacy ? { resolve: fulfill } : {}),
});
return harden({ notifier, updater });
};

0 comments on commit de99eb1

Please sign in to comment.