brianmhunt-mutex-promise
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -24,8 +24,8 @@ "use strict"; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } | ||
var PENDING = 0, | ||
@@ -45,3 +45,3 @@ RESOLVED = 1, | ||
var Promisee = (function () { | ||
var Promisee = function () { | ||
function Promisee(thenPromise, onResolvePromise1, onRejectPromise1) { | ||
@@ -88,5 +88,5 @@ _classCallCheck(this, Promisee); | ||
return Promisee; | ||
})(); | ||
}(); | ||
var MutexPromise = (function () { | ||
var MutexPromise = function () { | ||
function MutexPromise(fn) { | ||
@@ -118,2 +118,3 @@ _classCallCheck(this, MutexPromise); | ||
_createClass(MutexPromise, [{ | ||
@@ -126,12 +127,2 @@ key: 'then', | ||
// We should not create a `.then` "promise2" for an existing "promise1" | ||
// if the mutex has changed. | ||
if (this.mutexTo !== MutexPromise.mutexId) { | ||
this.emit("trespass", { | ||
promiseMutexTo: this.mutexTo, | ||
mutexId: MutexPromise.mutexId, | ||
during: "then" | ||
}); | ||
} | ||
if (this.state === PENDING) { | ||
@@ -145,2 +136,3 @@ promise2 = this._thenPending(onFul, onRej); | ||
promise2.weCatchFor.push(this); | ||
promise2.chainsFrom(this); | ||
@@ -188,2 +180,22 @@ // We're catching, meaning we catch anything above us. | ||
} | ||
}, { | ||
key: 'chainsFrom', | ||
value: function chainsFrom(previousPromise) { | ||
if (this.mutexTo !== MutexPromise.mutexId) { | ||
this.emit("trespass", { | ||
promiseMutexTo: this.mutexTo, | ||
mutexId: MutexPromise.mutexId, | ||
during: "construction" | ||
}); | ||
} else if (this.mutexTo !== previousPromise.mutexTo) { | ||
this.emit("trespass", { | ||
promiseMutexTo: this.mutexTo, | ||
mutexId: previousPromise.mutexTo, | ||
during: "chain" | ||
}); | ||
// Assign the previousPromise identifier so we can trace back to the | ||
// originating mutex from leafs. | ||
this.mutexTo = previousPromise.mutexId; | ||
} | ||
} | ||
@@ -224,2 +236,10 @@ // The "private" methods are below. | ||
function setResultForPromise2() { | ||
// Re-run our mutex checks in this event loop. | ||
if (promise2.mutexTo !== MutexPromise.mutexId) { | ||
promise2.emit("trespass", { | ||
promiseMutexTo: promise2.mutexTo, | ||
mutexId: MutexPromise.mutexId, | ||
during: "immediate-resolution" | ||
}); | ||
} | ||
var thenFn = promise1.state === RESOLVED ? onResolvePromise1 : onRejectPromise1; | ||
@@ -332,3 +352,3 @@ | ||
mutexId: MutexPromise.mutexId, | ||
during: "Resolution" | ||
during: "deferred-resolution" | ||
}); | ||
@@ -391,7 +411,7 @@ } | ||
// Capture Uncaught rejections and emit them. | ||
setTimeout((function () { | ||
setTimeout(function () { | ||
if (!this._isCaught) { | ||
this.emit('uncaught', this, reasonOrThenable); | ||
} | ||
}).bind(this), UNCAUGHT_TIMEOUT); | ||
}.bind(this), UNCAUGHT_TIMEOUT); | ||
} | ||
@@ -401,3 +421,3 @@ }]); | ||
return MutexPromise; | ||
})(); | ||
}(); | ||
@@ -407,15 +427,19 @@ // | ||
// | ||
// MutexPromise.race = function race(iter) { | ||
// FIXME | ||
// var rp = new MutexPromise(function (res, rej) { | ||
// var weCatchFor = this.weCatchFor | ||
// iter.forEach(function (p) { | ||
// p.then(res, rej) | ||
// weCatchFor.push(p) | ||
// }) | ||
// }) | ||
// rp.chainsFrom() | ||
// return rp | ||
// } | ||
MutexPromise.race = function race(iter) { | ||
return new MutexPromise(function (res, rej) { | ||
var weCatchFor = this.weCatchFor; | ||
iter.forEach(function (p) { | ||
p.then(res, rej); | ||
weCatchFor.push(p); | ||
}); | ||
}); | ||
}; | ||
// | ||
// | ||
MutexPromise.all = function all(iter) { | ||
@@ -448,2 +472,3 @@ var arr = []; | ||
all.weCatchFor.push(p); | ||
all.chainsFrom(p); | ||
}); | ||
@@ -460,2 +485,3 @@ | ||
rp.weCatchFor.push(valueOrThenableOrPromise); | ||
rp.chainsFrom(valueOrThenableOrPromise); | ||
} | ||
@@ -462,0 +488,0 @@ return rp; |
{ | ||
"name": "brianmhunt-mutex-promise", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Promises with uncaught handling and events", | ||
@@ -11,2 +11,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.6.5", | ||
"babel-preset-es2015": "^6.6.0", | ||
@@ -20,5 +21,5 @@ "chai": "^3.5.0", | ||
"scripts": { | ||
"build": "babel src/MutexPromise.js > dist/MutexPromise.js", | ||
"build": "./node_modules/babel-cli/bin/babel.js src/MutexPromise.js > dist/MutexPromise.js", | ||
"lint": "node ./node_modules/eslint/bin/eslint.js test src", | ||
"prepublish": "babel src/MutexPromise.js > dist/MutexPromise.js", | ||
"prepublish": "./node_modules/babel-cli/bin/babel.js src/MutexPromise.js > dist/MutexPromise.js", | ||
"test": "./node_modules/mocha/bin/mocha -u bdd", | ||
@@ -32,3 +33,3 @@ "start": "node ./node_modules/chokidar-cli --initial 'src/*.js' 'test/*.js' -c 'mocha -c -b -u bdd'" | ||
"keywords": [ | ||
"Promises" | ||
"promises-aplus" | ||
], | ||
@@ -35,0 +36,0 @@ "author": "Brian M Hunt", |
<div style='float: right'> | ||
<a href='https://travis-ci.org/brianmhunt/MutexPromise'> | ||
<img src='https://travis-ci.org/brianmhunt/MutexPromise.svg?branch=master' alt='Travis Status' title='Travis Status' align="right"> | ||
</a> | ||
<a href="https://promisesaplus.com/"> | ||
<img src="https://promisesaplus.com/assets/logo-small.png" alt="Promises/A+ logo" | ||
title="Promises/A+ 1.0 compliant" align="right" /> | ||
</a> | ||
</div> | ||
Mutex Promise | ||
============= | ||
A+ Compliant (and mostly follows ECMA-262) ES6 Promises with a few extra | ||
@@ -6,0 +18,0 @@ features that can help with debugging : |
@@ -102,12 +102,2 @@ "use strict" | ||
// We should not create a `.then` "promise2" for an existing "promise1" | ||
// if the mutex has changed. | ||
if (this.mutexTo !== MutexPromise.mutexId) { | ||
this.emit("trespass", { | ||
promiseMutexTo: this.mutexTo, | ||
mutexId: MutexPromise.mutexId, | ||
during: "then" | ||
}) | ||
} | ||
if (this.state === PENDING) { | ||
@@ -121,2 +111,3 @@ promise2 = this._thenPending(onFul, onRej) | ||
promise2.weCatchFor.push(this) | ||
promise2.chainsFrom(this) | ||
@@ -149,2 +140,22 @@ // We're catching, meaning we catch anything above us. | ||
chainsFrom(previousPromise) { | ||
if (this.mutexTo !== MutexPromise.mutexId) { | ||
this.emit("trespass", { | ||
promiseMutexTo: this.mutexTo, | ||
mutexId: MutexPromise.mutexId, | ||
during: "construction" | ||
}) | ||
} else if (this.mutexTo !== previousPromise.mutexTo) { | ||
this.emit("trespass", { | ||
promiseMutexTo: this.mutexTo, | ||
mutexId: previousPromise.mutexTo, | ||
during: "chain" | ||
}) | ||
// Assign the previousPromise identifier so we can trace back to the | ||
// originating mutex from leafs. | ||
this.mutexTo = previousPromise.mutexId | ||
} | ||
} | ||
// The "private" methods are below. | ||
@@ -178,2 +189,10 @@ // --- | ||
function setResultForPromise2() { | ||
// Re-run our mutex checks in this event loop. | ||
if (promise2.mutexTo !== MutexPromise.mutexId) { | ||
promise2.emit("trespass", { | ||
promiseMutexTo: promise2.mutexTo, | ||
mutexId: MutexPromise.mutexId, | ||
during: "immediate-resolution" | ||
}) | ||
} | ||
var thenFn = promise1.state === RESOLVED ? onResolvePromise1 | ||
@@ -270,3 +289,3 @@ : onRejectPromise1 | ||
mutexId: MutexPromise.mutexId, | ||
during: "Resolution" | ||
during: "deferred-resolution" | ||
}) | ||
@@ -338,11 +357,14 @@ } | ||
// | ||
MutexPromise.race = function race(iter) { | ||
return new MutexPromise(function (res, rej) { | ||
var weCatchFor = this.weCatchFor | ||
iter.forEach(function (p) { | ||
p.then(res, rej) | ||
weCatchFor.push(p) | ||
}) | ||
}) | ||
} | ||
// MutexPromise.race = function race(iter) { | ||
// FIXME | ||
// var rp = new MutexPromise(function (res, rej) { | ||
// var weCatchFor = this.weCatchFor | ||
// iter.forEach(function (p) { | ||
// p.then(res, rej) | ||
// weCatchFor.push(p) | ||
// }) | ||
// }) | ||
// rp.chainsFrom() | ||
// return rp | ||
// } | ||
@@ -375,2 +397,3 @@ // | ||
all.weCatchFor.push(p) | ||
all.chainsFrom(p) | ||
}) | ||
@@ -385,2 +408,3 @@ | ||
rp.weCatchFor.push(valueOrThenableOrPromise) | ||
rp.chainsFrom(valueOrThenableOrPromise) | ||
} | ||
@@ -387,0 +411,0 @@ return rp |
@@ -44,3 +44,3 @@ // | ||
// Out MutexPromise-specific tests | ||
describe("MutexPromise", function () { | ||
describe.only("MutexPromise", function () { | ||
beforeEach(function () { MP.setMutex(this.currentTest.title) }) | ||
@@ -166,7 +166,8 @@ afterEach(() => MP.eventHandlers = {}) | ||
// Triggers twice | ||
// 1. during .then | ||
// 2. during .resolve | ||
// Three 'tresspass' events: | ||
// 1. p0 resolves; | ||
// 2. p0 chains to .then; | ||
// 3. .then promise resolves. | ||
return p0 | ||
.then(() => assert.equal(spy.callCount, 2)) | ||
.then(() => assert.equal(spy.callCount, 3)) | ||
}) | ||
@@ -180,3 +181,3 @@ | ||
}) | ||
.then(() => assert.equal(spy.callCount, 2)) | ||
.then(() => assert.equal(spy.callCount, 3)) | ||
}) | ||
@@ -190,3 +191,3 @@ | ||
}) | ||
.then(() => assert.equal(spy.callCount, 2)) | ||
.then(() => assert.equal(spy.callCount, 3)) | ||
}) | ||
@@ -200,2 +201,20 @@ | ||
}) | ||
it("triggers trespass via .all", function () { | ||
var spy = handlerSpy('trespass') | ||
// MP.on('trespass', function (data) { | ||
// console.log(" ⚡️ ", data) | ||
// console.trace() | ||
// }) | ||
var p0 = new MP(function (res) { | ||
setTimeout(function () { | ||
res() | ||
MP.setMutex('rx') | ||
}, 1) | ||
}) | ||
// FIXME This is higher than it should be. | ||
return MP.all([0, p0, 1, 2]) | ||
.then(() => assert.equal(spy.callCount, 6)) | ||
}) | ||
}) | ||
@@ -202,0 +221,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42899
12
1060
140
7