fix: gnc eslint error.

This commit is contained in:
Rintim 2024-04-17 13:15:11 +08:00
parent 196212fa39
commit c82ad68d32
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
1 changed files with 42 additions and 38 deletions

View File

@ -8,45 +8,49 @@ export class GNC {
* @returns * @returns
*/ */
of(fn) { of(fn) {
return this.is.generatorFunc(fn) /**
? /** * @param {Parameters<typeof fn>} args
* @param {Parameters<typeof fn>} args * @returns {Promise<ReturnType<typeof fn>>}
* @returns {Promise<ReturnType<typeof fn>>} */
*/ function genCoroutine(...args) {
function genCoroutine(...args) { /**
let gen = fn.apply(this, args); * @type {Generator<unknown, any, unknown> & { status: "next" | "throw" , state?: any}}
gen.status = "next"; */
gen.state = undefined; // @ts-expect-error Must Ok
const callback = (resolve, reject) => { let gen = fn.apply(this, args);
let result, gen.status = "next";
nexts = resolve, gen.state = undefined;
throws = reject; const callback = (resolve, reject) => {
try { let result,
result = gen[gen.status](gen.state); nexts = resolve,
} catch (error) { throws = reject;
reject(error); try {
return; result = gen[gen.status](gen.state);
} } catch (error) {
if (!result.done) { reject(error);
nexts = (item) => { return;
gen.state = item; }
gen.status = "next"; if (!result.done) {
callback(resolve, reject); nexts = (item) => {
}; gen.state = item;
throws = (err) => { gen.status = "next";
gen.state = err; callback(resolve, reject);
gen.status = "throw";
callback(resolve, reject);
};
}
result = result.value;
Promise.resolve(result).then(nexts, throws);
}; };
return new Promise(callback); throws = (err) => {
} gen.state = err;
: (() => { gen.status = "throw";
throw new TypeError("gnc.of needs a GeneratorFunction."); callback(resolve, reject);
})(); };
}
result = result.value;
Promise.resolve(result).then(nexts, throws);
};
return new Promise(callback);
}
if (!this.is.generatorFunc(fn)) throw new TypeError("gnc.of needs a GeneratorFunction.");
return genCoroutine;
} }
is = new Is(); is = new Is();
} }