Skip to content

Commit

Permalink
Merge pull request stefanpenner#121 from vicb/0608-setAsap
Browse files Browse the repository at this point in the history
Ability to override the internal asap implementation
  • Loading branch information
stefanpenner committed Jun 8, 2015
2 parents 2b62f79 + 0003ca2 commit d3959b5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "es6-promise",
"namespace": "Promise",
"version": "2.2.0",
"version": "2.3.0",
"description": "A polyfill for ES6-style Promises, tracking rsvp",
"authors": [
"Stefan Penner <[email protected]>"
Expand Down
24 changes: 14 additions & 10 deletions dist/es6-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
* @license Licensed under MIT license
* See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE
* @version 2.2.0
* @version 2.3.0
*/

(function() {
Expand Down Expand Up @@ -35,7 +35,7 @@
var lib$es6$promise$asap$$vertxNext;
var lib$es6$promise$asap$$customSchedulerFn;

function lib$es6$promise$asap$$asap(callback, arg) {
var lib$es6$promise$asap$$asap = function asap(callback, arg) {
lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len] = callback;
lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len + 1] = arg;
lib$es6$promise$asap$$len += 2;
Expand All @@ -51,11 +51,14 @@
}
}

var lib$es6$promise$asap$$default = lib$es6$promise$asap$$asap;
function lib$es6$promise$asap$$setScheduler(scheduleFn) {
lib$es6$promise$asap$$customSchedulerFn = scheduleFn;
}

function lib$es6$promise$asap$$setAsap(asapFn) {
lib$es6$promise$asap$$asap = asapFn;
}

var lib$es6$promise$asap$$browserWindow = (typeof window !== 'undefined') ? window : undefined;
var lib$es6$promise$asap$$browserGlobal = lib$es6$promise$asap$$browserWindow || {};
var lib$es6$promise$asap$$BrowserMutationObserver = lib$es6$promise$asap$$browserGlobal.MutationObserver || lib$es6$promise$asap$$browserGlobal.WebKitMutationObserver;
Expand Down Expand Up @@ -187,7 +190,7 @@
}

function lib$es6$promise$$internal$$handleForeignThenable(promise, thenable, then) {
lib$es6$promise$asap$$default(function(promise) {
lib$es6$promise$asap$$asap(function(promise) {
var sealed = false;
var error = lib$es6$promise$$internal$$tryThen(then, thenable, function(value) {
if (sealed) { return; }
Expand Down Expand Up @@ -268,7 +271,7 @@
promise._state = lib$es6$promise$$internal$$FULFILLED;

if (promise._subscribers.length !== 0) {
lib$es6$promise$asap$$default(lib$es6$promise$$internal$$publish, promise);
lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, promise);
}
}

Expand All @@ -277,7 +280,7 @@
promise._state = lib$es6$promise$$internal$$REJECTED;
promise._result = reason;

lib$es6$promise$asap$$default(lib$es6$promise$$internal$$publishRejection, promise);
lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publishRejection, promise);
}

function lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection) {
Expand All @@ -291,7 +294,7 @@
subscribers[length + lib$es6$promise$$internal$$REJECTED] = onRejection;

if (length === 0 && parent._state) {
lib$es6$promise$asap$$default(lib$es6$promise$$internal$$publish, parent);
lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, parent);
}
}

Expand Down Expand Up @@ -548,7 +551,7 @@
/**
Promise objects represent the eventual result of an asynchronous operation. The
primary way of interacting with a promise is through its `then` method, which
registers callbacks to receive either a promises eventual value or the reason
registers callbacks to receive either a promise's eventual value or the reason
why the promise cannot be fulfilled.
Terminology
Expand Down Expand Up @@ -672,7 +675,8 @@
lib$es6$promise$promise$$Promise.resolve = lib$es6$promise$promise$resolve$$default;
lib$es6$promise$promise$$Promise.reject = lib$es6$promise$promise$reject$$default;
lib$es6$promise$promise$$Promise._setScheduler = lib$es6$promise$asap$$setScheduler;
lib$es6$promise$promise$$Promise._asap = lib$es6$promise$asap$$default;
lib$es6$promise$promise$$Promise._setAsap = lib$es6$promise$asap$$setAsap;
lib$es6$promise$promise$$Promise._asap = lib$es6$promise$asap$$asap;

lib$es6$promise$promise$$Promise.prototype = {
constructor: lib$es6$promise$promise$$Promise,
Expand Down Expand Up @@ -883,7 +887,7 @@

if (state) {
var callback = arguments[state - 1];
lib$es6$promise$asap$$default(function(){
lib$es6$promise$asap$$asap(function(){
lib$es6$promise$$internal$$invokeCallback(state, child, callback, result);
});
} else {
Expand Down
4 changes: 2 additions & 2 deletions dist/es6-promise.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion lib/es6-promise/-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
isFunction
} from './utils';

import asap from './asap';
import {
asap
} from './asap';

function noop() {}

Expand Down
6 changes: 5 additions & 1 deletion lib/es6-promise/asap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var toString = {}.toString;
var vertxNext;
var customSchedulerFn;

export default function asap(callback, arg) {
export var asap = function asap(callback, arg) {
queue[len] = callback;
queue[len + 1] = arg;
len += 2;
Expand All @@ -23,6 +23,10 @@ export function setScheduler(scheduleFn) {
customSchedulerFn = scheduleFn;
}

export function setAsap(asapFn) {
asap = asapFn;
}

var browserWindow = (typeof window !== 'undefined') ? window : undefined;
var browserGlobal = browserWindow || {};
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
Expand Down
7 changes: 6 additions & 1 deletion lib/es6-promise/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
REJECTED
} from './-internal';

import asap, { setScheduler } from './asap';
import {
asap,
setAsap,
setScheduler
} from './asap';

import all from './promise/all';
import race from './promise/race';
Expand Down Expand Up @@ -156,6 +160,7 @@ Promise.race = race;
Promise.resolve = Resolve;
Promise.reject = Reject;
Promise._setScheduler = setScheduler;
Promise._setAsap = setAsap;
Promise._asap = asap;

Promise.prototype = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "es6-promise",
"namespace": "es6-promise",
"version": "2.2.0",
"version": "2.3.0",
"description": "A lightweight library that provides tools for organizing asynchronous code",
"main": "dist/es6-promise.js",
"directories": {
Expand Down
21 changes: 21 additions & 0 deletions test/scheduler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,25 @@ describe('scheduler', function() {
}, 'arg');
});
});

describe('Promise._setAsap', function() {
it('should allow overriding asap', function(done) {
var called = false;

Promise._setAsap(function(fn, arg) {
called = true;
// call the original implementation
Promise._asap(fn, arg);
// restore the original implementation
Promise._setAsap(Promise._asap);
});

Promise.resolve('value').then(function(v) {
resolvedWith = v;
assert.equal(v, 'value');
assert.equal(called, true);
done();
});
});
});
});

0 comments on commit d3959b5

Please sign in to comment.