Skip to content

Commit

Permalink
Revert "fix: invoke the handler synchronously"
Browse files Browse the repository at this point in the history
This reverts commit 6d957e6.
  • Loading branch information
michaelfig committed Jul 13, 2019
1 parent 010efde commit b2e5d08
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 49 deletions.
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ export default function maybeExtendPromise(Promise) {
function handler(p, operation, ...args) {
const h = promiseToHandler.get(p) || forwardingHandler;
if (typeof h[operation] !== 'function') {
const handlerName =
h === forwardingHandler ? 'forwardingHandler' : 'unfulfilledHandler';
const handlerName = h === forwardingHandler ? 'forwardingHandler' : 'unfulfilledHandler';
throw TypeError(`${handlerName}.${operation} is not a function`);
}
return Promise.resolve(h[operation](p, ...args));
return h[operation](p, ...args);
}

Object.defineProperties(
Expand Down Expand Up @@ -180,13 +179,13 @@ export default function maybeExtendPromise(Promise) {

// Just like platform Promises, multiple calls to resolve don't fail.
if (!presenceToHandler.has(presence)) {
// Create table entries for the presence mapped to the fulfilledHandler.
presenceToPromise.set(presence, handledP);
presenceToHandler.set(presence, fulfilledHandler);
}

// We need to invoke fulfilledHandler immediately, so add it
// to the mapping.
promiseToHandler.set(handledP, fulfilledHandler);
// Remove the mapping, as our fulfilledHandler should be used instead.
promiseToHandler.delete(handledP);

// We committed to this presence, so resolve.
handledResolve(presence);
Expand Down
43 changes: 0 additions & 43 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,6 @@ if (typeof window !== 'undefined') {
});
}

test('immediate forwarding', async t => {
try {
const EPromise = maybeExtendPromise(Promise);

const queue = [];
const handler = {
POST(_o, fn, args) {
queue.push([fn, args]);
return 'foo';
},
};
let resolver;
const ep = EPromise.makeHandled(resolve => {
resolver = resolve;
}, handler);

// Make sure asynchronous posts go through.
const expected = [['myfn', ['abc', 123]]];
const firstPost = ep.post('myfn', ['abc', 123]).then(v => {
t.equal(v, 'foo', 'post return value is foo');
t.deepEqual(queue, expected, 'single post in queue');
});

t.deepEqual(queue, expected, 'unfulfilled post is synchronous');
await firstPost;

const target = {};
resolver(target, handler);
expected.push(['myotherfn', ['def', 456]]);
const secondPost = ep.post('myotherfn', ['def', 456]).then(v => {
t.equal(v, 'foo', 'second post return value is foo');
t.deepEqual(queue, expected, 'second post is queued');
});

t.deepEqual(queue, expected, 'fulfilled post is synchronous');
await secondPost;
} catch (e) {
t.assert(false, e);
} finally {
t.end();
}
});

test('maybeExtendPromise will not overwrite', async t => {
try {
const { makeHandled: secondMakeHandled } = maybeExtendPromise(Promise);
Expand Down

0 comments on commit b2e5d08

Please sign in to comment.