UNPKG

forty2

Version:

Collection of libraries that answer the ultimate question to the meaning of life

233 lines (228 loc) 7.94 kB
import Polyfill from './polyfill'; const globalScope = (typeof window !== 'undefined') ? window : global; class Promise { constructor(callback) { if(globalScope.Promise) { return new globalScope.Promise(callback); } else { return new Polyfill(callback); } } } function promisePolyfill() { } export default Promise; //const promise = (params) => { // params = params || {}; // // let resultData, // otherContext = 'ruf-undefined', // hasOtherContext = false, // /** // * status can be pending, fulfilled or rejected // */ // status = 'pending', // resolveCounter = 0, // garbage = [], // resolveFunctions = [], // rejectFunctions = [], // catchFunctions = [], // finallyFunctions = [], // paramActions = { // timeout:function(delay) { // setTimeout(function() { // if(status === 'pending') { // deferredObject.reject('Promise timed out after '+ delay +' milliseconds'); // } // }, delay); // } // }, // emptyFunction = function() {}, // // deferredObject = { // resolve:function(data) { // if(status === 'pending') { // status = 'fulfilled'; // } // // if(status !== 'rejected') { // //@TODO Put this in another function // switch(true) { // case resultData == null: // resultData = (data != null) ? data : {}; // parseResolveFunctions(); // break; // case hasOtherContext && otherContext !== 'ruf-undefined': // let newContext = otherContext; // otherContext = 'ruf-undefined'; // hasOtherContext = false; // parseResolveFunctions(newContext); // break; // case !hasOtherContext: // parseResolveFunctions(); // } // } // }, // reject:function(data) { // if(!garbage[resolveCounter]) { // status = 'rejected'; // // data = (data != null) ? data : 'The promise was rejected with no reason'; // resultData = data; // // parseRejectFunctions(); // } // }, // all:function(promises) { // let allPromises = rufPromise(), // returnData = [], // //@TODO Refactor complexity // addPromise = function(promise, index) { // if(!!promise && !!promise.then) { // promise.then(function(data) { // let hasAllData = true; // returnData[index] = data; // if(returnData.length === promises.length) { // for(let i = returnData.length; i--;) { // if(returnData[i] === undefined) { // hasAllData = false; // break; // } // } // if(hasAllData === true) { // allPromises.resolve(returnData); // } // } // }); // } // }; // for(let i = promises.length; i--;) { // addPromise(promises[i], i); // } // return allPromises.promise; // }, // promise: { // then:function(resolve, reject) { // resolveFunctions.push(resolve || emptyFunction); // rejectFunctions.push(reject || emptyFunction); // // if(status === 'fulfilled') { // deferredObject.resolve(); // } // else if(status === 'rejected') { // deferredObject.reject(); // } // // return this; // }, // catch:function(reject) { // catchFunctions.push(reject); // // if(status === 'rejected') { // deferredObject.reject(); // } // // return this; // }, // finally:function(finallyFunc) { // finallyFunctions.push(finallyFunc); // // if(status !== 'pending') { // parseFinallyFunctions(); // } // // return this; // } // } // }; // // for(let item in deferredObject.promise) { // if(deferredObject.promise.hasOwnProperty(item)) { // deferredObject[item] = deferredObject.promise[item]; // } // } // // Object.keys(params).map(function(param) { // if(paramActions[param]) { // paramActions[param](params[param]); // } // }); // // if(typeof params === 'function') { // params(deferredObject.resolve, deferredObject.reject); // } // // function parseResolveFunctions(newData) { // if(!resolveFunctions[resolveCounter]) { // return resetQueue(); // } // // let currentFunction = resolveFunctions[resolveCounter]; // ++resolveCounter; // // garbage.push(currentFunction); // let functionResult = currentFunction(newData || resultData); // // switch(true) { // case !!functionResult && !!functionResult.then: // hasOtherContext = true; // functionResult.then(function(data) { // otherContext = data; // deferredObject.resolve(data); // }, // function(err) { // deferredObject.reject(err); // }); // break; // case !!functionResult && typeof functionResult !== 'function': // resultData = functionResult; // deferredObject.resolve(); // break; // default: // deferredObject.resolve(); // } // } // // function resetQueue() { // resolveCounter = 0; // resolveFunctions.length = 0; // rejectFunctions.length = 0; // garbage.length = 0; // return parseFinallyFunctions(); // } // // function parseFinallyFunctions() { // for(let i = 0, iMax = finallyFunctions.length; i < iMax; i++) { // finallyFunctions[i](); // } // finallyFunctions.length = 0; // } // // function parseCatchFunctions() { // for(let i = 0, iMax = catchFunctions.length; i < iMax; i++) { // catchFunctions[i](resultData); // } // catchFunctions.length = 0; // } // // function parseRejectFunctions() { // if(!rejectFunctions[resolveCounter]) { // parseCatchFunctions(); // return resetQueue(); // } // // rejectFunctions[resolveCounter](resultData); // garbage.push(rejectFunctions[resolveCounter]); // ++resolveCounter; // // resolveCounter = 0; // resolveFunctions.length = 0; // rejectFunctions.length = 0; // garbage.length = 0; // } // // return deferredObject; //}; // //export { promise };