From 40e4e6e11a98645be00eff424685812e978d3adf Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Fri, 17 Jun 2016 20:21:48 +0100 Subject: [PATCH] getNewLibraryCopy method --- docs/docs/api-reference.md | 1 + docs/docs/api/promise.getnewlibrarycopy.md | 64 ++++++++++++++++++++++ src/promise.js | 2 + test/mocha/getNewLibraryCopy.js | 30 ++++++++++ 4 files changed, 97 insertions(+) create mode 100644 docs/docs/api/promise.getnewlibrarycopy.md create mode 100644 test/mocha/getNewLibraryCopy.js diff --git a/docs/docs/api-reference.md b/docs/docs/api-reference.md index ff62a7749..c8d13ed0c 100644 --- a/docs/docs/api-reference.md +++ b/docs/docs/api-reference.md @@ -74,6 +74,7 @@ redirect_from: "/docs/api/index.html" - [.catchReturn](api/catchreturn.html) - [.catchThrow](api/catchthrow.html) - [.reflect](api/reflect.html) + - [Promise.getNewLibraryCopy](api/promise.getnewlibrarycopy.html) - [Promise.noConflict](api/promise.noconflict.html) - [Promise.setScheduler](api/promise.setscheduler.html) - [Built-in error types](api/built-in-error-types.html) diff --git a/docs/docs/api/promise.getnewlibrarycopy.md b/docs/docs/api/promise.getnewlibrarycopy.md new file mode 100644 index 000000000..722f60c4f --- /dev/null +++ b/docs/docs/api/promise.getnewlibrarycopy.md @@ -0,0 +1,64 @@ +--- +layout: api +id: promise.getnewlibrarycopy +title: Promise.getNewLibraryCopy +--- + + +[← Back To API Reference](/docs/api-reference.html) +
+##Promise.getNewLibraryCopy + +```js +Promise.getNewLibraryCopy() -> Object +``` + +Returns a new independent copy of the Bluebird library. + +This method should be used before you use any of the methods which would otherwise alter the global `Bluebird` object - to avoid polluting global state. + +A basic example: + +```js +var Promise = require('bluebird'); +var Promise2 = Promise.getNewLibraryCopy(); + +Promise2.x = 123; + +console.log(Promise2 == Promise); // false +console.log(Promise2.x); // 123 +console.log(Promise.x); // undefined +``` + +`Promise` is independent to `Promise`. Any changes to `Promise2` do not affect the copy of Bluebird returned by `require('bluebird')`. + +In practice: + +```js +var Promise = require('bluebird').getNewLibraryCopy(); + +Promise.coroutine.addYieldHandler( function() { /* */ } ); // alters behavior of `Promise.coroutine()` + +// somewhere in another file or module in same app +var Promise = require('bluebird'); + +Promise.coroutine(function*() { + // this code is unaffected by the yieldHandler defined above + // because it was defined on an independent copy of Bluebird +}); +``` +
+ +
+ + diff --git a/src/promise.js b/src/promise.js index a1087fe00..47f8a7846 100644 --- a/src/promise.js +++ b/src/promise.js @@ -165,6 +165,8 @@ Promise.prototype.error = function (fn) { return this.caught(util.originatesFromRejection, fn); }; +Promise.getNewLibraryCopy = module.exports; + Promise.is = function (val) { return val instanceof Promise; }; diff --git a/test/mocha/getNewLibraryCopy.js b/test/mocha/getNewLibraryCopy.js new file mode 100644 index 000000000..16eb10d59 --- /dev/null +++ b/test/mocha/getNewLibraryCopy.js @@ -0,0 +1,30 @@ +"use strict"; + +var assert = require("assert"); +var testUtils = require("./helpers/util.js"); + +describe("Promise.getNewLibraryCopy", function() { + it("should return an independent copy of Bluebird library", function() { + var Promise2 = Promise.getNewLibraryCopy(); + Promise2.x = 123; + + assert.equal(typeof Promise2.prototype.then, "function"); + assert.notEqual(Promise2, Promise); + + assert.equal(Promise2.x, 123); + assert.notEqual(Promise.x, 123); + }); + it("should return copy of Bluebird library with its own getNewLibraryCopy method", function() { + var Promise2 = Promise.getNewLibraryCopy(); + var Promise3 = Promise2.getNewLibraryCopy(); + Promise3.x = 123; + + assert.equal(typeof Promise3.prototype.then, "function"); + assert.notEqual(Promise3, Promise); + assert.notEqual(Promise3, Promise2); + + assert.equal(Promise3.x, 123); + assert.notEqual(Promise.x, 123); + assert.notEqual(Promise2.x, 123); + }); +});