Skip to content

Commit

Permalink
Remove babel-preset-es2015
Browse files Browse the repository at this point in the history
  • Loading branch information
Guang Yang committed Feb 1, 2018
1 parent 4b05bc2 commit 51ff5ea
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 162 deletions.
1 change: 0 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"presets": ["es2015"],
"plugins": ["transform-es2015-modules-commonjs"]
}
276 changes: 122 additions & 154 deletions dist/src/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,198 +3,166 @@
Object.defineProperty(exports, "__esModule", {
value: true
});

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 && obj !== Symbol.prototype ? "symbol" : typeof obj; };

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var State = {
const State = {
Pending: 'Pending',
Fulfilled: 'Fulfilled',
Rejected: 'Rejected'
};

var Util = {
isFunction: function isFunction(val) {
const Util = {
isFunction(val) {
return val && typeof val === "function";
},
isObject: function isObject(val) {
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === "object";

isObject(val) {
return val && typeof val === "object";
}
};

var Promise = exports.Promise = function () {
function Promise(executor) {
_classCallCheck(this, Promise);

class Promise {
constructor(executor) {
this._state = State.Pending;
this._handlers = [];
this._value = null;
executor(this._resolve.bind(this), this._reject.bind(this));
}

_createClass(Promise, [{
key: '_resolve',
value: function _resolve(x) {
var _this = this;

if (x === this) {
throw new TypeError("Resolving object can not be the same object");
} else if (x instanceof Promise) {
x.then(this._resolve.bind(this), this._reject.bind(this));
} else if (Util.isObject(x) || Util.isFunction(x)) {
var called = false;
try {
var thenable = x.then;
if (Util.isFunction(thenable)) {
thenable.call(x, function (result) {
if (!called) _this._resolve(result);
called = true;
}, function (error) {
if (!called) _this._reject(error);
called = true;
});
} else {
this._fulfill(x);
}
} catch (ex) {
if (!called) {
this._reject(ex);
}
_resolve(x) {
if (x === this) {
throw new TypeError("Resolving object can not be the same object");
} else if (x instanceof Promise) {
x.then(this._resolve.bind(this), this._reject.bind(this));
} else if (Util.isObject(x) || Util.isFunction(x)) {
let called = false;
try {
const thenable = x.then;
if (Util.isFunction(thenable)) {
thenable.call(x, result => {
if (!called) this._resolve(result);
called = true;
}, error => {
if (!called) this._reject(error);
called = true;
});
} else {
this._fulfill(x);
}
} catch (ex) {
if (!called) {
this._reject(ex);
}
} else {
this._fulfill(x);
}
} else {
this._fulfill(x);
}
}, {
key: '_fulfill',
value: function _fulfill(result) {
var _this2 = this;
}

this._state = State.Fulfilled;
this._value = result;
this._handlers.forEach(function (handler) {
return _this2._callHandler(handler);
});
}
}, {
key: '_reject',
value: function _reject(error) {
var _this3 = this;
_fulfill(result) {
this._state = State.Fulfilled;
this._value = result;
this._handlers.forEach(handler => this._callHandler(handler));
}

this._state = State.Rejected;
this._value = error;
this._handlers.forEach(function (handler) {
return _this3._callHandler(handler);
});
}
}, {
key: '_isPending',
value: function _isPending() {
return this._state === State.Pending;
}
}, {
key: '_isFulfilled',
value: function _isFulfilled() {
return this._state === State.Fulfilled;
}
}, {
key: '_isRejected',
value: function _isRejected() {
return this._state === State.Rejected;
}
}, {
key: '_addHandler',
value: function _addHandler(onFulfilled, onRejected) {
this._handlers.push({
onFulfilled: onFulfilled,
onRejected: onRejected
});
}
}, {
key: '_callHandler',
value: function _callHandler(handler) {
if (this._isFulfilled() && Util.isFunction(handler.onFulfilled)) {
handler.onFulfilled(this._value);
} else if (this._isRejected() && Util.isFunction(handler.onRejected)) {
handler.onRejected(this._value);
}
}
}, {
key: 'then',
value: function then(onFulfilled, onRejected) {
var _this4 = this;
_reject(error) {
this._state = State.Rejected;
this._value = error;
this._handlers.forEach(handler => this._callHandler(handler));
}

switch (this._state) {
case State.Pending:
{
return new Promise(function (resolve, reject) {
_this4._addHandler(function (value) {
setTimeout(function () {
try {
if (Util.isFunction(onFulfilled)) {
resolve(onFulfilled(value));
} else {
resolve(value);
}
} catch (ex) {
reject(ex);
}
}, 0);
}, function (error) {
setTimeout(function () {
try {
if (Util.isFunction(onRejected)) {
resolve(onRejected(error));
} else {
reject(error);
}
} catch (ex) {
reject(ex);
}
}, 0);
});
});
}
_isPending() {
return this._state === State.Pending;
}

_isFulfilled() {
return this._state === State.Fulfilled;
}

case State.Fulfilled:
{
return new Promise(function (resolve, reject) {
setTimeout(function () {
_isRejected() {
return this._state === State.Rejected;
}

_addHandler(onFulfilled, onRejected) {
this._handlers.push({
onFulfilled,
onRejected
});
}

_callHandler(handler) {
if (this._isFulfilled() && Util.isFunction(handler.onFulfilled)) {
handler.onFulfilled(this._value);
} else if (this._isRejected() && Util.isFunction(handler.onRejected)) {
handler.onRejected(this._value);
}
}

then(onFulfilled, onRejected) {
switch (this._state) {
case State.Pending:
{
return new Promise((resolve, reject) => {
this._addHandler(value => {
setTimeout(() => {
try {
if (Util.isFunction(onFulfilled)) {
resolve(onFulfilled(_this4._value));
resolve(onFulfilled(value));
} else {
resolve(_this4._value);
resolve(value);
}
} catch (ex) {
reject(ex);
}
}, 0);
});
}

case State.Rejected:
{
return new Promise(function (resolve, reject) {
setTimeout(function () {
}, error => {
setTimeout(() => {
try {
if (Util.isFunction(onRejected)) {
resolve(onRejected(_this4._value));
resolve(onRejected(error));
} else {
reject(_this4._value);
reject(error);
}
} catch (ex) {
reject(ex);
}
}, 0);
});
}
}
}
}]);
});
}

return Promise;
}();
case State.Fulfilled:
{
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
if (Util.isFunction(onFulfilled)) {
resolve(onFulfilled(this._value));
} else {
resolve(this._value);
}
} catch (ex) {
reject(ex);
}
}, 0);
});
}

case State.Rejected:
{
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
if (Util.isFunction(onRejected)) {
resolve(onRejected(this._value));
} else {
reject(this._value);
}
} catch (ex) {
reject(ex);
}
}, 0);
});
}
}
}
}
exports.Promise = Promise;
8 changes: 4 additions & 4 deletions dist/test/aplus_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var _promise = require("../src/promise");

var promisesAplusTests = require("promises-aplus-tests");

var adapter = {
deferred: function deferred() {
var pending = {};
pending.promise = new _promise.Promise(function (resolver, reject) {
const adapter = {
deferred() {
const pending = {};
pending.promise = new _promise.Promise((resolver, reject) => {
pending.resolve = resolver;
pending.reject = reject;
});
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A 150 loc Promises/A+ implementation",
"main": "src/promise.js",
"scripts": {
"build": "babel src/** test/** -d dist",
"build": "babel src/**/* test/**/* -d dist",
"test": "node dist/test/aplus_test.js"
},
"author": "yanguango",
Expand All @@ -13,7 +13,6 @@
"babel-core": "^6.26.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"promises-aplus-tests": "*",
"typescript": "^2.6.2"
"promises-aplus-tests": "*"
}
}

0 comments on commit 51ff5ea

Please sign in to comment.