修复 events.defaultMaxListeners的问题
清理代码 更换 promise的实现
This commit is contained in:
1 parent
8d052d2b6f
commit
941cdccf31
6 files changed
+7
-221
No files matched your search
Generated
BIN
Binary file not shown.
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
module.exports = function (__runtime__, scope) {
|
module.exports = function (runtime, scope) {
|
||||||
var rtConsole = __runtime__.console;
|
var rtConsole = runtime.console;
|
||||||
var console = {};
|
var console = {};
|
||||||
|
|
||||||
console.assert = function (value, message) {
|
console.assert = function (value, message) {
|
||||||
|
|||||||
@@ -1,215 +1 @@
|
|||||||
'use strict';
|
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.ES6Promise=e()}(this,function(){"use strict";function t(t){var e=typeof t;return null!==t&&("object"===e||"function"===e)}function e(t){return"function"==typeof t}function n(t){B=t}function r(t){G=t}function o(){return function(){return process.nextTick(a)}}function i(){return"undefined"!=typeof z?function(){z(a)}:c()}function s(){var t=0,e=new J(a),n=document.createTextNode("");return e.observe(n,{characterData:!0}),function(){n.data=t=++t%2}}function u(){var t=new MessageChannel;return t.port1.onmessage=a,function(){return t.port2.postMessage(0)}}function c(){var t=setTimeout;return function(){return t(a,1)}}function a(){for(var t=0;t<W;t+=2){var e=V[t],n=V[t+1];e(n),V[t]=void 0,V[t+1]=void 0}W=0}function f(){try{var t=Function("return this")().require("vertx");return z=t.runOnLoop||t.runOnContext,i()}catch(e){return c()}}function l(t,e){var n=this,r=new this.constructor(p);void 0===r[Z]&&O(r);var o=n._state;if(o){var i=arguments[o-1];G(function(){return P(o,r,i,n._result)})}else E(n,r,t,e);return r}function h(t){var e=this;if(t&&"object"==typeof t&&t.constructor===e)return t;var n=new e(p);return g(n,t),n}function p(){}function v(){return new TypeError("You cannot resolve a promise with itself")}function d(){return new TypeError("A promises callback cannot return that same promise.")}function _(t){try{return t.then}catch(e){return nt.error=e,nt}}function y(t,e,n,r){try{t.call(e,n,r)}catch(o){return o}}function m(t,e,n){G(function(t){var r=!1,o=y(n,e,function(n){r||(r=!0,e!==n?g(t,n):S(t,n))},function(e){r||(r=!0,j(t,e))},"Settle: "+(t._label||" unknown promise"));!r&&o&&(r=!0,j(t,o))},t)}function b(t,e){e._state===tt?S(t,e._result):e._state===et?j(t,e._result):E(e,void 0,function(e){return g(t,e)},function(e){return j(t,e)})}function w(t,n,r){n.constructor===t.constructor&&r===l&&n.constructor.resolve===h?b(t,n):r===nt?(j(t,nt.error),nt.error=null):void 0===r?S(t,n):e(r)?m(t,n,r):S(t,n)}function g(e,n){e===n?j(e,v()):t(n)?w(e,n,_(n)):S(e,n)}function A(t){t._onerror&&t._onerror(t._result),T(t)}function S(t,e){t._state===$&&(t._result=e,t._state=tt,0!==t._subscribers.length&&G(T,t))}function j(t,e){t._state===$&&(t._state=et,t._result=e,G(A,t))}function E(t,e,n,r){var o=t._subscribers,i=o.length;t._onerror=null,o[i]=e,o[i+tt]=n,o[i+et]=r,0===i&&t._state&&G(T,t)}function T(t){var e=t._subscribers,n=t._state;if(0!==e.length){for(var r=void 0,o=void 0,i=t._result,s=0;s<e.length;s+=3)r=e[s],o=e[s+n],r?P(n,r,o,i):o(i);t._subscribers.length=0}}function M(t,e){try{return t(e)}catch(n){return nt.error=n,nt}}function P(t,n,r,o){var i=e(r),s=void 0,u=void 0,c=void 0,a=void 0;if(i){if(s=M(r,o),s===nt?(a=!0,u=s.error,s.error=null):c=!0,n===s)return void j(n,d())}else s=o,c=!0;n._state!==$||(i&&c?g(n,s):a?j(n,u):t===tt?S(n,s):t===et&&j(n,s))}function x(t,e){try{e(function(e){g(t,e)},function(e){j(t,e)})}catch(n){j(t,n)}}function C(){return rt++}function O(t){t[Z]=rt++,t._state=void 0,t._result=void 0,t._subscribers=[]}function k(){return new Error("Array Methods must be provided an Array")}function F(t){return new ot(this,t).promise}function Y(t){var e=this;return new e(U(t)?function(n,r){for(var o=t.length,i=0;i<o;i++)e.resolve(t[i]).then(n,r)}:function(t,e){return e(new TypeError("You must pass an array to race."))})}function q(t){var e=this,n=new e(p);return j(n,t),n}function D(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}function K(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}function L(){var t=void 0;if("undefined"!=typeof global)t=global;else if("undefined"!=typeof self)t=self;else try{t=Function("return this")()}catch(e){throw new Error("polyfill failed because global object is unavailable in this environment")}var n=t.Promise;if(n){var r=null;try{r=Object.prototype.toString.call(n.resolve())}catch(e){}if("[object Promise]"===r&&!n.cast)return}t.Promise=it}var N=void 0;N=Array.isArray?Array.isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)};var U=N,W=0,z=void 0,B=void 0,G=function(t,e){V[W]=t,V[W+1]=e,W+=2,2===W&&(B?B(a):X())},H="undefined"!=typeof window?window:void 0,I=H||{},J=I.MutationObserver||I.WebKitMutationObserver,Q="undefined"==typeof self&&"undefined"!=typeof process&&"[object process]"==={}.toString.call(process),R="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,V=new Array(1e3),X=void 0;X=Q?o():J?s():R?u():void 0===H&&"function"==typeof require?f():c();var Z=Math.random().toString(36).substring(2),$=void 0,tt=1,et=2,nt={error:null},rt=0,ot=function(){function t(t,e){this._instanceConstructor=t,this.promise=new t(p),this.promise[Z]||O(this.promise),U(e)?(this.length=e.length,this._remaining=e.length,this._result=new ArLine truncated
|
||||||
|
|
||||||
function asap(action){
|
|
||||||
action();
|
|
||||||
}
|
|
||||||
|
|
||||||
function noop() {}
|
|
||||||
|
|
||||||
// States:
|
|
||||||
//
|
|
||||||
// 0 - pending
|
|
||||||
// 1 - fulfilled with _value
|
|
||||||
// 2 - rejected with _value
|
|
||||||
// 3 - adopted the state of another promise, _value
|
|
||||||
//
|
|
||||||
// once the state is no longer pending (0) it is immutable
|
|
||||||
|
|
||||||
// All `_` prefixed properties will be reduced to `_{random number}`
|
|
||||||
// at build time to obfuscate them and discourage their use.
|
|
||||||
// We don't use symbols or Object.defineProperty to fully hide them
|
|
||||||
// because the performance isn't good enough.
|
|
||||||
|
|
||||||
|
|
||||||
// to avoid using try/catch inside critical functions, we
|
|
||||||
// extract them to here.
|
|
||||||
var LAST_ERROR = null;
|
|
||||||
var IS_ERROR = {};
|
|
||||||
function getThen(obj) {
|
|
||||||
try {
|
|
||||||
return obj.then;
|
|
||||||
} catch (ex) {
|
|
||||||
LAST_ERROR = ex;
|
|
||||||
return IS_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function tryCallOne(fn, a) {
|
|
||||||
try {
|
|
||||||
return fn(a);
|
|
||||||
} catch (ex) {
|
|
||||||
LAST_ERROR = ex;
|
|
||||||
return IS_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function tryCallTwo(fn, a, b) {
|
|
||||||
try {
|
|
||||||
fn(a, b);
|
|
||||||
} catch (ex) {
|
|
||||||
LAST_ERROR = ex;
|
|
||||||
return IS_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = Promise;
|
|
||||||
|
|
||||||
function Promise(fn) {
|
|
||||||
if (typeof this !== 'object') {
|
|
||||||
throw new TypeError('Promises must be constructed via new');
|
|
||||||
}
|
|
||||||
if (typeof fn !== 'function') {
|
|
||||||
throw new TypeError('Promise constructor\'s argument is not a function');
|
|
||||||
}
|
|
||||||
this._deferredState = 0;
|
|
||||||
this._state = 0;
|
|
||||||
this._value = null;
|
|
||||||
this._deferreds = null;
|
|
||||||
if (fn === noop) return;
|
|
||||||
doResolve(fn, this);
|
|
||||||
}
|
|
||||||
Promise._onHandle = null;
|
|
||||||
Promise._onReject = null;
|
|
||||||
Promise._noop = noop;
|
|
||||||
|
|
||||||
Promise.prototype.then = function(onFulfilled, onRejected) {
|
|
||||||
if (this.constructor !== Promise) {
|
|
||||||
return safeThen(this, onFulfilled, onRejected);
|
|
||||||
}
|
|
||||||
var res = new Promise(noop);
|
|
||||||
handle(this, new Handler(onFulfilled, onRejected, res));
|
|
||||||
return res;
|
|
||||||
};
|
|
||||||
|
|
||||||
function safeThen(self, onFulfilled, onRejected) {
|
|
||||||
return new self.constructor(function (resolve, reject) {
|
|
||||||
var res = new Promise(noop);
|
|
||||||
res.then(resolve, reject);
|
|
||||||
handle(self, new Handler(onFulfilled, onRejected, res));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function handle(self, deferred) {
|
|
||||||
while (self._state === 3) {
|
|
||||||
self = self._value;
|
|
||||||
}
|
|
||||||
if (Promise._onHandle) {
|
|
||||||
Promise._onHandle(self);
|
|
||||||
}
|
|
||||||
if (self._state === 0) {
|
|
||||||
if (self._deferredState === 0) {
|
|
||||||
self._deferredState = 1;
|
|
||||||
self._deferreds = deferred;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (self._deferredState === 1) {
|
|
||||||
self._deferredState = 2;
|
|
||||||
self._deferreds = [self._deferreds, deferred];
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
self._deferreds.push(deferred);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
handleResolved(self, deferred);
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleResolved(self, deferred) {
|
|
||||||
asap(function() {
|
|
||||||
var cb = self._state === 1 ? deferred.onFulfilled : deferred.onRejected;
|
|
||||||
if (cb === null) {
|
|
||||||
if (self._state === 1) {
|
|
||||||
resolve(deferred.promise, self._value);
|
|
||||||
} else {
|
|
||||||
reject(deferred.promise, self._value);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var ret = tryCallOne(cb, self._value);
|
|
||||||
if (ret === IS_ERROR) {
|
|
||||||
reject(deferred.promise, LAST_ERROR);
|
|
||||||
} else {
|
|
||||||
resolve(deferred.promise, ret);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function resolve(self, newValue) {
|
|
||||||
// Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
|
|
||||||
if (newValue === self) {
|
|
||||||
return reject(
|
|
||||||
self,
|
|
||||||
new TypeError('A promise cannot be resolved with itself.')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
newValue &&
|
|
||||||
(typeof newValue === 'object' || typeof newValue === 'function')
|
|
||||||
) {
|
|
||||||
var then = getThen(newValue);
|
|
||||||
if (then === IS_ERROR) {
|
|
||||||
return reject(self, LAST_ERROR);
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
then === self.then &&
|
|
||||||
newValue instanceof Promise
|
|
||||||
) {
|
|
||||||
self._state = 3;
|
|
||||||
self._value = newValue;
|
|
||||||
finale(self);
|
|
||||||
return;
|
|
||||||
} else if (typeof then === 'function') {
|
|
||||||
doResolve(then.bind(newValue), self);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self._state = 1;
|
|
||||||
self._value = newValue;
|
|
||||||
finale(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
function reject(self, newValue) {
|
|
||||||
self._state = 2;
|
|
||||||
self._value = newValue;
|
|
||||||
if (Promise._onReject) {
|
|
||||||
Promise._onReject(self, newValue);
|
|
||||||
}
|
|
||||||
finale(self);
|
|
||||||
}
|
|
||||||
function finale(self) {
|
|
||||||
if (self._deferredState === 1) {
|
|
||||||
handle(self, self._deferreds);
|
|
||||||
self._deferreds = null;
|
|
||||||
}
|
|
||||||
if (self._deferredState === 2) {
|
|
||||||
for (var i = 0; i < self._deferreds.length; i++) {
|
|
||||||
handle(self, self._deferreds[i]);
|
|
||||||
}
|
|
||||||
self._deferreds = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function Handler(onFulfilled, onRejected, promise){
|
|
||||||
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
|
|
||||||
this.onRejected = typeof onRejected === 'function' ? onRejected : null;
|
|
||||||
this.promise = promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Take a potentially misbehaving resolver function and make sure
|
|
||||||
* onFulfilled and onRejected are only called once.
|
|
||||||
*
|
|
||||||
* Makes no guarantees about asynchrony.
|
|
||||||
*/
|
|
||||||
function doResolve(fn, promise) {
|
|
||||||
var done = false;
|
|
||||||
var res = tryCallTwo(fn, function (value) {
|
|
||||||
if (done) return;
|
|
||||||
done = true;
|
|
||||||
resolve(promise, value);
|
|
||||||
}, function (reason) {
|
|
||||||
if (done) return;
|
|
||||||
done = true;
|
|
||||||
reject(promise, reason);
|
|
||||||
});
|
|
||||||
if (!done && res === IS_ERROR) {
|
|
||||||
done = true;
|
|
||||||
reject(promise, LAST_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -40,7 +40,7 @@ public class EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void ensureListenersNotAtLimit() {
|
private void ensureListenersNotAtLimit() {
|
||||||
if (mMaxListeners != 0 && mListenersMap.size() >= mMaxListeners) {
|
if (mMaxListeners != 0 && mListenerWrappers.size() >= mMaxListeners) {
|
||||||
throw new ScriptException(new TooManyListenersException("max = " + mMaxListeners));
|
throw new ScriptException(new TooManyListenersException("max = " + mMaxListeners));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":451,"versionName":"4.1.0 Alpha","enabled":true,"outputFile":"commonRelease-4.1.0 Alpha.apk","fullName":"commonRelease","baseName":"common-release"},"path":"commonRelease-4.1.0 Alpha.apk","properties":{}}]
|
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":452,"versionName":"4.1.0 Alpha2","enabled":true,"outputFile":"commonRelease-4.1.0 Alpha2.apk","fullName":"commonRelease","baseName":"common-release"},"path":"commonRelease-4.1.0 Alpha2.apk","properties":{}}]
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"appVersionCode": 451,
|
"appVersionCode": 452,
|
||||||
"appVersionName": "4.1.0 Alpha",
|
"appVersionName": "4.1.0 Alpha2",
|
||||||
"target": 28,
|
"target": 28,
|
||||||
"mini": 17,
|
"mini": 17,
|
||||||
"compile": 28,
|
"compile": 28,
|
||||||
|
|||||||
Reference in new issue
Block a user