nscomponentsreact
Version:
React Wrapper Components for NSComponents
379 lines (349 loc) • 9.92 kB
JavaScript
var nsModuleExport = function(root,name,prototype)
{
if(typeof exports === 'object' && typeof module === 'object')
{
module.exports[name] = prototype;
}
else if (typeof define === "function" && define.amd)
{
define(name,[], function () {return prototype;});
}
else if(typeof exports === 'object')
{
exports[name] = prototype;
}
else
{
root[name] = prototype;
}
};var __nsGlobal = (function () {
if (typeof globalThis !== "undefined") return globalThis;
if (typeof self !== "undefined") return self;
if (typeof window !== "undefined") return window;
if (typeof global !== "undefined") return global;
return Function("return this")();
})();var nsIsWeb = function(root)
{
if(typeof exports === 'object' && typeof module === 'object')
{
return false;
}
else if (typeof define === "function" && define.amd)
{
return false;
}
else if(typeof exports === 'object')
{
return false;
}
else
{
return true;
}
};if(!nsIsWeb())
{
var nsutilRef = require('./nsUtil.min.js');
var NSUtil = nsutilRef.NSUtil;
}
//https://github.com/YahooArchive/ypromise/blob/master/promise.js
var Promise = (function ()
{
function Resolver()
{
this._callbacks = [];
this._errbacks = [];
this._status = 'pending';
this._result = null;
}
Resolver.prototype.fulfill = function (value)
{
var status = this._status;
if (status === 'pending' || status === 'accepted')
{
this._result = value;
this._status = 'fulfilled';
}
if (this._status === 'fulfilled')
{
this._notify(this._callbacks, this._result);
this._callbacks = [];
this._errbacks = null;
}
};
Resolver.prototype.reject = function (reason)
{
var status = this._status;
if (status === 'pending' || status === 'accepted')
{
this._result = reason;
this._status = 'rejected';
}
if (this._status === 'rejected')
{
this._notify(this._errbacks, this._result);
this._callbacks = null;
this._errbacks = [];
}
};
Resolver.prototype.resolve = function (value)
{
if (this._status === 'pending')
{
this._status = 'accepted';
this._value = value;
if ((this._callbacks && this._callbacks.length) || this._errbacks && this._errbacks.length)
{
this._unwrap(this._value);
}
}
};
Resolver.prototype._unwrap = function (value)
{
var self = this, unwrapped = false, then;
if (!value || (typeof value !== 'object' && typeof value !== 'function'))
{
self.fulfill(value);
return;
}
try
{
then = value.then;
if (typeof then === 'function')
{
then.call(value, function (value)
{
if (!unwrapped)
{
unwrapped = true;
self._unwrap(value);
}
},
function (reason)
{
if (!unwrapped)
{
unwrapped = true;
self.reject(reason);
}
});
}
else
{
self.fulfill(value);
}
}
catch (e)
{
if (!unwrapped)
{
self.reject(e);
}
}
};
Resolver.prototype._addCallbacks = function (callback, errback)
{
var callbackList = this._callbacks,
errbackList = this._errbacks;
if (callbackList)
{
callbackList.push(callback);
}
if (errbackList)
{
errbackList.push(errback);
}
switch (this._status)
{
case 'accepted':
this._unwrap(this._value);
break;
case 'fulfilled':
this.fulfill(this._result);
break;
case 'rejected':
this.reject(this._result);
break;
}
};
Resolver.prototype._notify = function (subs, result)
{
if (subs.length)
{
Promise.async(function ()
{
var i, len;
for (i = 0, len = subs.length; i < len; ++i)
{
subs[i](result);
}
});
}
};
function Promise(fn)
{
if (!(this instanceof Promise))
{
throw new TypeError('Promises must be constructed via new');
}
if (typeof fn !== 'function')
{
throw new TypeError("Promise constructor takes a function argument");
}
var resolver = new Resolver();
this._resolver = resolver;
try
{
fn(function (value)
{
resolver.resolve(value);
},
function (reason)
{
resolver.reject(reason);
});
}
catch (e)
{
resolver.reject(e);
}
}
Promise.prototype.then = function (callback, errback)
{
var resolve, reject,
promise = new this.constructor(function (res, rej)
{
resolve = res;
reject = rej;
});
this._resolver._addCallbacks(typeof callback === 'function' ? Promise._makeCallback(promise, resolve, reject, callback) : resolve,
typeof errback === 'function' ? Promise._makeCallback(promise, resolve, reject, errback) : reject);
return promise;
};
Promise.prototype["catch"] = function(errback)
{
return this.then(undefined, errback);
};
Promise.resolve = function (value)
{
if (value && value.constructor === this)
{
return value;
}
return new this(function (resolve)
{
resolve(value);
});
};
Promise.reject = function (reason)
{
var promise = new this(function () {});
promise._resolver._result = reason;
promise._resolver._status = 'rejected';
return promise;
};
Promise.all = function (values)
{
var Promise = this;
var util = new NSUtil();
return new Promise(function (resolve, reject)
{
if (!util.isArray(values))
{
reject(new TypeError('Promise.all expects an array of values or promises'));
return;
}
var remaining = values.length,
i = 0,
length = values.length,
results = [];
function oneDone(index)
{
return function (value)
{
results[index] = value;
remaining--;
if (!remaining)
{
resolve(results);
}
};
}
if (length < 1)
{
return resolve(results);
}
for (; i < length; i++)
{
Promise.resolve(values[i]).then(oneDone(i), reject);
}
});
};
Promise.race = function (values)
{
var Promise = this;
var util = new NSUtil();
return new Promise(function (resolve, reject)
{
if (!util.isArray(values))
{
reject(new TypeError('Promise.race expects an array of values or promises'));
return;
}
for (var i = 0, count = values.length; i < count; i++)
{
Promise.resolve(values[i]).then(resolve, reject);
}
});
};
Promise.async = typeof setImmediate !== 'undefined' ?
function (fn) {setImmediate(fn);} :
typeof process !== 'undefined' && process.nextTick ?
process.nextTick :
function (fn) {setTimeout(fn, 0);};
Promise._makeCallback = function (promise, resolve, reject, fn)
{
return function (valueOrReason)
{
var result;
try
{
result = fn(valueOrReason);
}
catch (e)
{
reject(e);
return;
}
if (result === promise)
{
reject(new TypeError('Cannot resolve a promise with itself'));
return;
}
resolve(result);
};
};
Promise.Resolver = Resolver;
var globalNS = (function()
{
if (typeof self !== 'undefined') {
return self;
}
if (typeof window !== 'undefined') {
return window;
}
if (typeof global !== 'undefined') {
return global;
}
throw new Error('unable to locate global object');
})();
if (typeof globalNS['Promise'] !== 'function')
{
globalNS['Promise'] = Promise;
}
else
{
Promise = globalNS['Promise'];
}
return Promise;
}());
nsModuleExport(__nsGlobal,"Promise",Promise);