Skip to content

Commit

Permalink
Merge pull request #85 from TimvdLippe/closure-compilation
Browse files Browse the repository at this point in the history
Fix Closure compilation
  • Loading branch information
taylorhakes committed Aug 5, 2018
2 parents 487e452 + acd5225 commit f6d61c5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
28 changes: 22 additions & 6 deletions dist/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
(factory());
}(this, (function () { 'use strict';

var promiseFinally = function(callback) {
/**
* @this {Promise}
*/
function finallyConstructor(callback) {
var constructor = this.constructor;
return this.then(
function(value) {
Expand All @@ -18,7 +21,7 @@ var promiseFinally = function(callback) {
});
}
);
};
}

// Store setTimeout reference so promise-polyfill will be unaffected by
// other code modifying setTimeout (like sinon.useFakeTimers())
Expand All @@ -33,13 +36,21 @@ function bind(fn, thisArg) {
};
}

/**
* @constructor
* @param {Function} fn
*/
function Promise(fn) {
if (!(this instanceof Promise))
throw new TypeError('Promises must be constructed via new');
if (typeof fn !== 'function') throw new TypeError('not a function');
/** @type {!number} */
this._state = 0;
/** @type {!boolean} */
this._handled = false;
/** @type {Promise|undefined} */
this._value = undefined;
/** @type {!Array<!Function>} */
this._deferreds = [];

doResolve(fn, this);
Expand Down Expand Up @@ -120,6 +131,9 @@ function finale(self) {
self._deferreds = null;
}

/**
* @constructor
*/
function Handler(onFulfilled, onRejected, promise) {
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
this.onRejected = typeof onRejected === 'function' ? onRejected : null;
Expand Down Expand Up @@ -159,13 +173,14 @@ Promise.prototype['catch'] = function(onRejected) {
};

Promise.prototype.then = function(onFulfilled, onRejected) {
// @ts-ignore
var prom = new this.constructor(noop);

handle(this, new Handler(onFulfilled, onRejected, prom));
return prom;
};

Promise.prototype['finally'] = promiseFinally;
Promise.prototype['finally'] = finallyConstructor;

Promise.all = function(arr) {
return new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -245,6 +260,7 @@ Promise._unhandledRejectionFn = function _unhandledRejectionFn(err) {
}
};

/** @suppress {undefinedVars} */
var globalNS = (function() {
// the only reliable means to get the global object is
// `Function('return this')()`
Expand All @@ -261,10 +277,10 @@ var globalNS = (function() {
throw new Error('unable to locate global object');
})();

if (!globalNS.Promise) {
globalNS.Promise = Promise;
if (!('Promise' in globalNS)) {
globalNS['Promise'] = Promise;
} else if (!globalNS.Promise.prototype['finally']) {
globalNS.Promise.prototype['finally'] = promiseFinally;
globalNS.Promise.prototype['finally'] = finallyConstructor;
}

})));
2 changes: 1 addition & 1 deletion dist/polyfill.min.js

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

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"scripts": {
"precommit": "lint-staged",
"pretest": "npm run build:cjs",
"test": "eslint src && mocha && karma start --single-run",
"closure": "google-closure-compiler --js=src/*.js --checks_only --module_resolution=node --compilation_level=ADVANCED",
"typescript": "tsc --checkJS --allowJS --noEmit src/index.js",
"lint": "eslint src && npm run closure && npm run typescript",
"test": "npm run lint && mocha && karma start --single-run",
"prebuild": "rimraf lib dist",
"build": "run-p build:**",
"build:cjs": "rollup -i src/index.js -o lib/index.js -f cjs",
Expand All @@ -37,6 +40,7 @@
"devDependencies": {
"cross-env": "^5.1.1",
"eslint": "^4.11.0",
"google-closure-compiler": "^20180610.0.1",
"husky": "^0.14.3",
"karma": "^0.13.19",
"karma-browserify": "^4.4.2",
Expand All @@ -50,7 +54,8 @@
"rimraf": "^2.6.2",
"rollup": "^0.52.0",
"rollup-plugin-uglify": "^2.0.1",
"sinon": "^1.17.2"
"sinon": "^1.17.2",
"typescript": "^2.9.2"
},
"keywords": [
"promise",
Expand Down
7 changes: 6 additions & 1 deletion src/finally.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default function(callback) {
/**
* @this {Promise}
*/
function finallyConstructor(callback) {
var constructor = this.constructor;
return this.then(
function(value) {
Expand All @@ -13,3 +16,5 @@ export default function(callback) {
}
);
}

export default finallyConstructor;
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ function bind(fn, thisArg) {
};
}

/**
* @constructor
* @param {Function} fn
*/
function Promise(fn) {
if (!(this instanceof Promise))
throw new TypeError('Promises must be constructed via new');
if (typeof fn !== 'function') throw new TypeError('not a function');
/** @type {!number} */
this._state = 0;
/** @type {!boolean} */
this._handled = false;
/** @type {Promise|undefined} */
this._value = undefined;
/** @type {!Array<!Function>} */
this._deferreds = [];

doResolve(fn, this);
Expand Down Expand Up @@ -100,6 +108,9 @@ function finale(self) {
self._deferreds = null;
}

/**
* @constructor
*/
function Handler(onFulfilled, onRejected, promise) {
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
this.onRejected = typeof onRejected === 'function' ? onRejected : null;
Expand Down Expand Up @@ -139,6 +150,7 @@ Promise.prototype['catch'] = function(onRejected) {
};

Promise.prototype.then = function(onFulfilled, onRejected) {
// @ts-ignore
var prom = new this.constructor(noop);

handle(this, new Handler(onFulfilled, onRejected, prom));
Expand Down
5 changes: 3 additions & 2 deletions src/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Promise from './index';
import promiseFinally from './finally';

/** @suppress {undefinedVars} */
var globalNS = (function() {
// the only reliable means to get the global object is
// `Function('return this')()`
Expand All @@ -17,8 +18,8 @@ var globalNS = (function() {
throw new Error('unable to locate global object');
})();

if (!globalNS.Promise) {
globalNS.Promise = Promise;
if (!('Promise' in globalNS)) {
globalNS['Promise'] = Promise;
} else if (!globalNS.Promise.prototype['finally']) {
globalNS.Promise.prototype['finally'] = promiseFinally;
}

0 comments on commit f6d61c5

Please sign in to comment.