Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #171

Merged
merged 6 commits into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"extends": "ember",

"parser": "babel-eslint",

"ecmaFeatures": {
modules: true,
blockBindings: true,
arrowFunctions: true,
objectLiteralShorthandMethods: true,
objectLiteralShorthandProperties: true,
templateStrings: true
},

"rules": {
"indent": [ 2, "tab", { "SwitchCase": 1 } ],
"object-shorthand": [ 2, "always" ],
"prefer-const": 0,
"comma-dangle": 0,
"spaced-comment": 1,
"object-curly-spacing": [2, "always"],
"arrow-spacing": [ 1, { before: true, after: true } ],
"array-bracket-spacing": [ 2, "always" ],
"no-restricted-syntax": 0,
"no-warning-comments": [ 0, { "terms": [ "todo", "fixme", "xxx" ], "location": "start" } ],
"no-ternary": 0,
"no-nested-ternary": 2,
"brace-style": [ 2, "stroustrup" ],
"no-else-return": 0
}
}
16 changes: 16 additions & 0 deletions lib/es6-promise/-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import originalThen from './then';
import originalResolve from './promise/resolve';

export var PROMISE_ID = Math.random().toString(36).substring(16);

function noop() {}

var PENDING = void 0;
Expand Down Expand Up @@ -239,7 +241,21 @@ function initializePromise(promise, resolver) {
}
}

var id = 0;
function nextId() {
return id++;
}

function makePromise(promise) {
promise[PROMISE_ID] = id++;
promise._state = undefined;
promise._result = undefined;
promise._subscribers = [];
}

export {
nextId,
makePromise,
getThen,
noop,
resolve,
Expand Down
5 changes: 5 additions & 0 deletions lib/es6-promise/enumerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ import then from './then';
import Promise from './promise';
import originalResolve from './promise/resolve';
import originalThen from './then';
import { makePromise, PROMISE_ID } from './-internal';

export default Enumerator;
function Enumerator(Constructor, input) {
this._instanceConstructor = Constructor;
this.promise = new Constructor(noop);

if (!this.promise[PROMISE_ID]) {
makePromise(this.promise);
}

if (Array.isArray(input)) {
this._input = input;
this.length = input.length;
Expand Down
8 changes: 4 additions & 4 deletions lib/es6-promise/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {

import {
noop,
nextId,
PROMISE_ID,
initializePromise
} from './-internal';

Expand All @@ -19,7 +21,6 @@ import Resolve from './promise/resolve';
import Reject from './promise/reject';
import then from './then';

var counter = 0;

function needsResolver() {
throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
Expand Down Expand Up @@ -134,9 +135,8 @@ export default Promise;
@constructor
*/
function Promise(resolver) {
this._id = counter++;
this._state = undefined;
this._result = undefined;
this[PROMISE_ID] = nextId();
this._result = this._state = undefined;
this._subscribers = [];

if (noop !== resolver) {
Expand Down
28 changes: 9 additions & 19 deletions lib/es6-promise/promise/race.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,16 @@ export default function race(entries) {
/*jshint validthis:true */
var Constructor = this;

var promise = new Constructor(noop);

if (!isArray(entries)) {
reject(promise, new TypeError('You must pass an array to race.'));
return promise;
return new Constructor(function(resolve, reject) {
reject(new TypeError('You must pass an array to race.'));
});
}

var length = entries.length;

function onFulfillment(value) {
resolve(promise, value);
}

function onRejection(reason) {
reject(promise, reason);
}

for (var i = 0; promise._state === PENDING && i < length; i++) {
subscribe(Constructor.resolve(entries[i]), undefined, onFulfillment, onRejection);
}

return promise;
return new Constructor(function(resolve, reject) {
var length = entries.length;
for (var i = 0; i < length; i++) {
Constructor.resolve(entries[i]).then(resolve, reject);
}
});
}
16 changes: 9 additions & 7 deletions lib/es6-promise/then.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ import {
subscribe,
FULFILLED,
REJECTED,
noop
noop,
makePromise,
PROMISE_ID
} from './-internal';

import { asap } from './asap';

export default function then(onFulfillment, onRejection) {
var parent = this;
var state = parent._state;

if (state === FULFILLED && !onFulfillment || state === REJECTED && !onRejection) {
return this;
var child = new this.constructor(noop);

if (child[PROMISE_ID] === undefined) {
makePromise(child);
}

var child = new this.constructor(noop);
var result = parent._result;
var state = parent._state;

if (state) {
var callback = arguments[state - 1];
asap(function(){
invokeCallback(state, child, callback, result);
invokeCallback(state, child, callback, parent._result);
});
} else {
subscribe(parent, child, onFulfillment, onRejection);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
"!dist/test"
],
"devDependencies": {
"babel-eslint": "^5.0.0",
"broccoli-es6-module-transpiler": "^0.5.0",
"broccoli-jshint": "^1.1.1",
"broccoli-merge-trees": "^1.1.1",
"broccoli-replace": "^0.2.0",
"broccoli-stew": "^1.2.0",
"broccoli-uglify-js": "^0.1.3",
"broccoli-watchify": "^0.2.0",
"ember-cli": "2.3.0-beta.1",
"ember-cli": "2.3.0",
"ember-publisher": "0.0.7",
"git-repo-version": "0.0.3",
"json3": "^3.3.2",
Expand Down
166 changes: 166 additions & 0 deletions test/extension-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,59 @@ describe('tampering', function() {
});
});

describe('alternative constructor', function() {
it('tampered resolved and then', function() {
var one = Promise.resolve(1);
var two = Promise.resolve(2);
var thenCalled = 0;
var resolveCalled = 0;
var invokedAlternativeConstructor = 0;

two.then = function() {
thenCalled++;
return Promise.prototype.then.apply(this, arguments);
};

function AlternativeConstructor(executor) {
invokedAlternativeConstructor++;
var followers = this.followers = [];
executor(function(value) {
followers.forEach(function(onFulfillment) {
onFulfillment(value)
});
}, function() {
throw TypeError('No Rejections supported');
});
}

AlternativeConstructor.resolve = function(x) {
resolveCalled++;
return new Promise(function(resolve) { resolve(x); });
};

AlternativeConstructor.prototype.then = function(onFulfillment, onRejection) {
this.followers.push(onFulfillment);
};

AlternativeConstructor.resolve = function(x) {
resolveCalled++;
return new Promise(function(resolve) { resolve(x); });
};

one.constructor = AlternativeConstructor

return one.then(function() {
return two;
}).then(function(value) {

assert.equal(invokedAlternativeConstructor, 1, 'expected AlternativeConstructor to be invoked once');
assert.equal(thenCalled, 1, 'expected then to be called once');
assert.equal(resolveCalled, 0, 'expected resolve to be called once');
assert.equal(value, 2, 'expected fulfillment value to be 2');
});
});
});

describe('Promise.all', function() {
it('tampered resolved and then', function() {
var two = Promise.resolve(2);
Expand All @@ -97,7 +150,120 @@ describe('tampering', function() {
assert.deepEqual(value, [2]);
});
});

it('alternative constructor and tampered then', function() {
var two = Promise.resolve(2);
var thenCalled = 0;
var resolveCalled = 0;

two.then = function() {
thenCalled++;
return Promise.prototype.then.apply(this, arguments);
};

function AlternativeConstructor(executor) {
var followers = this.followers = [];
executor(function(value) {
followers.forEach(function(onFulfillment) {
onFulfillment(value)
});
}, function() {
throw TypeError('No Rejections supported');
});
}

AlternativeConstructor.resolve = function(x) {
resolveCalled++;
return new Promise(function(resolve) { resolve(x); });
};

AlternativeConstructor.prototype.then = function(onFulfillment, onRejection) {
this.followers.push(onFulfillment);
};

return Promise.all.call(AlternativeConstructor, [two]).then(function(value) {
assert.equal(thenCalled, 1);
assert.equal(resolveCalled, 1);
assert.deepEqual(value, [2]);
});
});
});

describe('core-js species test', function() {
it('foreign thenable has correct internal slots', function() {
var p = Promise.resolve();

function NewConstructor(it) {
it(function(){}, function(){})
}

p.constructor = NewConstructor;

var f = p.then(function(){});
assert(f instanceof NewConstructor);
});
});
describe('Promise.race', function() {
it('tampered resolved and then', function() {
var two = Promise.resolve(2);
var thenCalled = 0;
var resolveCalled = 0;

two.then = function() {
thenCalled++;
return Promise.prototype.then.apply(this, arguments);
};

Promise.resolve = function(x) {
resolveCalled++;
return new Promise(function(resolve) { resolve(x); });
};

return Promise.race([two]).then(function(value) {
assert.equal(thenCalled, 1);
assert.equal(resolveCalled, 1);
assert.deepEqual(value, 2);
});
});

it('alternative constructor and tampered then', function() {
var two = Promise.resolve(2);
var thenCalled = 0;
var resolveCalled = 0;

two.then = function() {
thenCalled++;
return Promise.prototype.then.apply(this, arguments);
};

function AlternativeConstructor(executor) {
var followers = this.followers = [];
executor(function(value) {
followers.forEach(function(onFulfillment) {
onFulfillment(value)
});
}, function() {
throw TypeError('No Rejections supported');
});
}

AlternativeConstructor.resolve = function(x) {
resolveCalled++;
return new Promise(function(resolve) { resolve(x); });
};

AlternativeConstructor.prototype.then = function(onFulfillment, onRejection) {
this.followers.push(onFulfillment);
};

return Promise.race.call(AlternativeConstructor, [two]).then(function(value) {
assert.equal(thenCalled, 1);
assert.equal(resolveCalled, 1);
assert.deepEqual(value, 2);
});
});
});

});
});

Expand Down