Skip to content

Commit

Permalink
Fixed atomic support in generators context by the CPromise.run method;
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed Jan 31, 2022
1 parent 9aeb5d5 commit 2366f27
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,10 @@ const CPromise= class CPromise extends Promise {
if (!shadow.isPending) return this;

const complete = (value, isRejected) => {
shadow.timer && clearTimeout(shadow.timer);
if (shadow.timer) {
clearTimeout(shadow.timer);
shadow.timer = null;
}

const resolve = () => {
shadow.isRejected= isRejected;
Expand Down Expand Up @@ -769,10 +772,9 @@ const CPromise= class CPromise extends Promise {
this[_setInnerChain](value);
}

super.then.call(
value,
value.then(
(value) => {
complete(value)
complete(value);
},
(err) => {
complete(err, true)
Expand Down Expand Up @@ -879,8 +881,8 @@ const CPromise= class CPromise extends Promise {
return false;
}

if(atomic === ATOMIC_TYPE_AWAIT){
shadow.canceledWith= err;
if (atomic === ATOMIC_TYPE_AWAIT) {
shadow.canceledWith = err;
return true;
}

Expand Down Expand Up @@ -1640,8 +1642,9 @@ const CPromise= class CPromise extends Promise {
return thing;
}

const {resolveSignatures = true, atomic= true} =
(typeof options === 'boolean' ? {resolveSignatures: options} : options) || {};
const {resolveSignatures = true, atomic= true} = (typeof options === 'boolean' ? {
resolveSignatures: options
} : options) || {};

const type = typeof thing;

Expand Down Expand Up @@ -1806,20 +1809,19 @@ const CPromise= class CPromise extends Promise {
case "generator":
return function (...args) {
return context.run(decoratedFn, {
context: this,
context: isContextDefined(this) ? this : null,
scopeArg,
args
});
}
case "async":
return function (...args) {
if (!scopeArg) {
return context.resolve(decoratedFn.apply(this, args));
}

return context.resolve().then((v, scope) => {
return context.resolve(decoratedFn.apply(this, [scope, ...args]));
})
return new context((resolve, reject, scope) => {
decoratedFn.apply(
isContextDefined(this) ? this : scope,
scopeArg ? [scope, ...args] : args
).then(resolve, reject);
}).atomic();
}
}
throw TypeError('promisify requires a Function|GeneratorFunction|AsyncFunction as the first argument');
Expand Down Expand Up @@ -1888,6 +1890,7 @@ const CPromise= class CPromise extends Promise {
}

const onFulfilled = (result) => {
if (scope[_shadow].isCanceled) return;
try {
next(generator.next(result));
} catch (e) {
Expand All @@ -1896,6 +1899,7 @@ const CPromise= class CPromise extends Promise {
}

const onRejected = (err) => {
if (scope[_shadow].isCanceled) return;
try {
next(generator.throw(err));
} catch (e) {
Expand Down Expand Up @@ -2325,7 +2329,10 @@ Object.defineProperties(CPromise, Object.entries({
E_REASON_CANCELED,
E_REASON_TIMEOUT,
E_REASON_DISPOSED,
E_REASON_UNMOUNTED
E_REASON_UNMOUNTED,
ATOMIC_TYPE_DISABLED,
ATOMIC_TYPE_DETACHED,
ATOMIC_TYPE_AWAIT
}).reduce((descriptors, [prop, value]) => {
descriptors[prop] = {value, configurable: true};
return descriptors;
Expand Down Expand Up @@ -2623,7 +2630,14 @@ Object.entries({
'innerWeight': number,
'atomic': [
validators.undefined, boolean,
validators.values(ATOMIC_TYPE_DISABLED, ATOMIC_TYPE_DETACHED, ATOMIC_TYPE_AWAIT, 'detached', 'disabled', 'await')
validators.values(
ATOMIC_TYPE_DISABLED,
ATOMIC_TYPE_DETACHED,
ATOMIC_TYPE_AWAIT,
'detached',
'disabled',
'await'
)
]
}).forEach(([name, type])=>{
decorators[name]= propertyDecorator((decorator, [value], {context}) => {
Expand Down

0 comments on commit 2366f27

Please sign in to comment.