Skip to content

Commit

Permalink
Rejection tracking (#2)
Browse files Browse the repository at this point in the history
* New: Unhandled rejection tracking

* First pass at rejection tests

* onUnhandledRejection working

* onRejectionHandled working

* Last tests for rejection tracking

* Clean up lint errors
  • Loading branch information
nzakas authored Jan 1, 2021
1 parent 0f127d8 commit 5976ddf
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 10 deletions.
123 changes: 123 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"mocha": "^5.2.0",
"nyc": "^15.1.0",
"rollup": "^1.20.3",
"rollup-plugin-babel-minify": "^9.0.0"
"rollup-plugin-babel-minify": "^9.0.0",
"sinon": "^9.2.2"
}
}
13 changes: 9 additions & 4 deletions src/pledge-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ export function rejectPledge(pledge, reason) {
pledge[PledgeSymbol.state] = "rejected";

// global rejection tracking
if (!pledge[PledgeSymbol.isHandled]) {
// TODO: perform HostPromiseRejectionTracker(promise, "reject").
if (pledge[PledgeSymbol.isHandled] === false) {
hostPledgeRejectionTracker(pledge, "reject");
}

return triggerPledgeReactions(reactions, reason);
Expand All @@ -257,7 +257,12 @@ export function triggerPledgeReactions(reactions, argument) {
}

//-----------------------------------------------------------------------------
// 25.6.1.9 HostPromiseRejectionTracker(promise, operation)
// 26.6.1.9 HostPromiseRejectionTracker ( promise, operation )
//-----------------------------------------------------------------------------

// TODO
// everything in this function is not defined in the spec

export function hostPledgeRejectionTracker(pledge, operation) {
const rejectionTracker = pledge.constructor[PledgeSymbol.rejectionTracker];
rejectionTracker.track(pledge, operation);
}
3 changes: 2 additions & 1 deletion src/pledge-symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export const PledgeSymbol = Object.freeze({
result: Symbol("PledgeResult"),
isHandled: Symbol("PledgeIsHandled"),
fulfillReactions: Symbol("PledgeFulfillReactions"),
rejectReactions: Symbol("PledgeRejectReactions")
rejectReactions: Symbol("PledgeRejectReactions"),
rejectionTracker: Symbol("PledgeRejectionTracker")
});
26 changes: 24 additions & 2 deletions src/pledge.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import {
isPledge,
createResolvingFunctions,
PledgeReaction,
PledgeCapability
PledgeCapability,
hostPledgeRejectionTracker
} from "./pledge-operations.js";
import { RejectionTracker } from "./rejection-tracker.js";

//-----------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -96,6 +98,16 @@ export class Pledge {
return this;
}

/* eslint-disable no-unused-vars */
static onUnhandledRejection(pledge, reason) {
// noop
}

static onRejectionHandled(pledge, reason) {
// noop
}
/* eslint-enable no-unused-vars */

static any(iterable) {

const C = this;
Expand Down Expand Up @@ -270,6 +282,7 @@ export class Pledge {
}
}

Pledge[PledgeSymbol.rejectionTracker] = new RejectionTracker();

//-----------------------------------------------------------------------------
// 25.6.5.4.1 PerformPromiseThen(promise, onFulfilled, onRejected,
Expand Down Expand Up @@ -308,8 +321,11 @@ function performPledgeThen(pledge, onFulfilled, onRejected, resultCapability) {
case "rejected":
{
const reason = pledge[PledgeSymbol.result];
if (pledge[PledgeSymbol.isHandled] === false) {
hostPledgeRejectionTracker(pledge, "handle");
}

const rejectJob = new PledgeReactionJob(rejectReaction, reason);
// TODO: if [[isHandled]] if false
hostEnqueuePledgeJob(rejectJob);
}
break;
Expand Down Expand Up @@ -374,6 +390,7 @@ function performPledgeAll(iteratorRecord, constructor, resultCapability, pledgeR
const remainingElementsCount = { value: 1 };
let index = 0;

/* eslint-disable-next-line no-constant-condition */
while (true) {
let next;

Expand Down Expand Up @@ -459,6 +476,7 @@ function performPledgeAny(iteratorRecord, constructor, resultCapability, pledgeR
const remainingElementsCount = { value: 1 };
let index = 0;

/* eslint-disable-next-line no-constant-condition */
while (true) {
let next;

Expand Down Expand Up @@ -514,6 +532,7 @@ function performPledgeAny(iteratorRecord, constructor, resultCapability, pledgeR
* the iterator in a normal fashion. Here's what it would look like if you were writing it
* like a human being instead of copying the spec.
*/
/* eslint-disable-next-line no-unused-vars */
function performPledgeAnySimple(iteratorRecord, constructor, resultCapability, pledgeResolve) {

assertIsConstructor(constructor);
Expand Down Expand Up @@ -610,6 +629,7 @@ function performPledgeRace(iteratorRecord, constructor, resultCapability, pledge
assertIsConstructor(constructor);
assertIsCallable(pledgeResolve);

/* eslint-disable-next-line no-constant-condition */
while (true) {

let next;
Expand Down Expand Up @@ -649,6 +669,7 @@ function performPledgeRace(iteratorRecord, constructor, resultCapability, pledge
* the iterator in a normal fashion. Here's what it would look like if you were writing it
* like a human being instead of copying the spec.
*/
/* eslint-disable-next-line no-unused-vars */
function performPledgeRaceSimple(iteratorRecord, constructor, resultCapability, pledgeResolve) {

assertIsConstructor(constructor);
Expand Down Expand Up @@ -687,6 +708,7 @@ function performPledgeAllSettled(iteratorRecord, constructor, resultCapability,
const remainingElementsCount = { value: 1 };
let index = 0;

/* eslint-disable-next-line no-constant-condition */
while (true) {
let next;

Expand Down
Loading

0 comments on commit 5976ddf

Please sign in to comment.