mirror of
https://github.com/Ylianst/MeshAgent
synced 2026-01-13 14:03:33 +00:00
1. Updated reference counting in promise
2. Added 'internal' prototype to Function, which sets infrastructure mode
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1342,6 +1342,13 @@ duk_ret_t ILibDuktape_EventEmitter_setFinalizerMetadata(duk_context *ctx)
|
||||
duk_put_prop_string(ctx, -2, ILibDuktape_EventEmitter_FinalizerDebugMessage);
|
||||
return(0);
|
||||
}
|
||||
duk_ret_t ILibDuktape_RefCountPointer(duk_context *ctx)
|
||||
{
|
||||
duk_push_this(ctx);
|
||||
duk_int_t *t = _get_refcount_ptr(ctx, -1);
|
||||
duk_push_pointer(ctx, t);
|
||||
return(1);
|
||||
}
|
||||
void ILibDuktape_EventEmitter_PUSH(duk_context *ctx, void *chain)
|
||||
{
|
||||
duk_push_object(ctx); // [emitter]
|
||||
@@ -1354,6 +1361,7 @@ void ILibDuktape_EventEmitter_PUSH(duk_context *ctx, void *chain)
|
||||
ILibDuktape_CreateInstanceMethod(ctx, "addHiddenReference", ILibDuktape_EventEmitter_addHidden, 2);
|
||||
ILibDuktape_CreateInstanceMethod(ctx, "deleteProperty", ILibDuktape_EventEmitter_deleteProperty, 2);
|
||||
ILibDuktape_CreateInstanceMethod(ctx, "setFinalizerMetadata", ILibDuktape_EventEmitter_setFinalizerMetadata, 1);
|
||||
ILibDuktape_CreateInstanceMethod(ctx, "_refCountPointer", ILibDuktape_RefCountPointer, 0);
|
||||
}
|
||||
void ILibDuktape_EventEmitter_Init(duk_context *ctx)
|
||||
{
|
||||
|
||||
@@ -59,9 +59,7 @@ function Promise(promiseFunc)
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
this._internal.on('newListener', function (eventName, eventCallback)
|
||||
this._internal.on('newListener', (function (eventName, eventCallback)
|
||||
{
|
||||
//console.log('newListener', eventName, 'errors/' + this.errors + ' completed/' + this.completed);
|
||||
var r = null;
|
||||
@@ -99,8 +97,9 @@ function Promise(promiseFunc)
|
||||
{
|
||||
eventCallback.apply(this, []);
|
||||
}
|
||||
});
|
||||
this._internal.resolver = function _resolver()
|
||||
}).internal);
|
||||
|
||||
this._internal.resolver = (function _resolver()
|
||||
{
|
||||
if (_resolver._self.completed) { return; }
|
||||
_resolver._self.errors = false;
|
||||
@@ -143,8 +142,8 @@ function Promise(promiseFunc)
|
||||
_resolver._self.emit.apply(_resolver._self, args);
|
||||
_resolver._self.emit('settled');
|
||||
}
|
||||
};
|
||||
this._internal.rejector = function _rejector()
|
||||
}).internal;
|
||||
this._internal.rejector = (function _rejector()
|
||||
{
|
||||
if (_rejector._self.completed) { return; }
|
||||
_rejector._self.errors = true;
|
||||
@@ -168,24 +167,24 @@ function Promise(promiseFunc)
|
||||
|
||||
_rejector._self.emit.apply(_rejector._self, args);
|
||||
_rejector._self.emit('settled');
|
||||
};
|
||||
}).internal;
|
||||
this._internal.rejector.internal = true;
|
||||
|
||||
this.catch = function(func)
|
||||
{
|
||||
var rt = getRootPromise(this);
|
||||
this._internal.once('rejected', event_switcher(this, func).func);
|
||||
this._internal.once('rejected', event_switcher(this, func).func.internal);
|
||||
}
|
||||
this.finally = function (func)
|
||||
{
|
||||
this._internal.once('settled', event_switcher(this, func).func);
|
||||
this._internal.once('settled', event_switcher(this, func).func.internal);
|
||||
};
|
||||
this.then = function (resolved, rejected)
|
||||
{
|
||||
if (resolved) { this._internal.once('resolved', event_switcher(this, resolved).func); }
|
||||
if (resolved) { this._internal.once('resolved', event_switcher(this, resolved).func.internal); }
|
||||
if (rejected)
|
||||
{
|
||||
this._internal.once('rejected', event_switcher(this, rejected).func);
|
||||
this._internal.once('rejected', event_switcher(this, rejected).func.internal);
|
||||
}
|
||||
|
||||
var retVal = new Promise(function (r, j) { this._rej = j; });
|
||||
@@ -220,6 +219,7 @@ function Promise(promiseFunc)
|
||||
this._internal.once('rejected', retVal._internal.rejector);
|
||||
}
|
||||
this.__childPromise = retVal;
|
||||
|
||||
return (retVal);
|
||||
};
|
||||
|
||||
@@ -243,7 +243,10 @@ function Promise(promiseFunc)
|
||||
{
|
||||
// Save reference of this object
|
||||
refTable[this._internal._hashCode()] = this._internal;
|
||||
this._internal.once('settled', function () { delete refTable[this._hashCode()]; });
|
||||
this._internal.once('settled', (function ()
|
||||
{
|
||||
delete refTable[this._hashCode()];
|
||||
}).internal);
|
||||
}
|
||||
Object.defineProperty(this, "completed", {
|
||||
get: function ()
|
||||
@@ -251,6 +254,19 @@ function Promise(promiseFunc)
|
||||
return (this._internal.completed);
|
||||
}
|
||||
});
|
||||
|
||||
this._internal.once('settled', (function ()
|
||||
{
|
||||
delete this.resolver._self;
|
||||
delete this.rejector._self;
|
||||
delete this.promise._up;
|
||||
delete this.promise.__childPromise;
|
||||
delete this.promise.promise;
|
||||
|
||||
delete this._up;
|
||||
delete this.__childPromise;
|
||||
delete this.promise;
|
||||
}).internal);
|
||||
}
|
||||
|
||||
Promise.resolve = function resolve()
|
||||
|
||||
Reference in New Issue
Block a user