pref: divide gnc.

This commit is contained in:
Rintim 2024-01-02 14:29:02 +08:00
parent d55b4a76af
commit 38546d0159
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
2 changed files with 67 additions and 42 deletions

View File

@ -1,53 +1,51 @@
import { GeneratorFunction, Uninstantable } from "../util/index.js";
class Is extends Uninstantable {
static coroutine(item) {
return typeof item == "function" && item.name == "genCoroutine";
}
static generatorFunc(item) {
return item instanceof GeneratorFunction;
}
static generator(item) {
return (typeof item == "object") && ("constructor" in item) && item.constructor && ("constructor" in item.constructor) && item.constructor.constructor === GeneratorFunction;
}
};
import { Is } from "./is.js";
// gnc: GeNCoroutine
export class GNC extends Uninstantable {
/**
* @param {GeneratorFunction} fn
* @returns
*/
static of(fn) {
return gnc.is.generatorFunc(fn) ? function genCoroutine() {
let gen = fn.apply(this, arguments);
gen.status = "next";
gen.state = undefined;
const callback = (resolve, reject) => {
let result,
nexts = resolve,
throws = reject;
try {
result = gen[gen.status](gen.state);
} catch (error) {
reject(error);
return;
}
if (!result.done) {
nexts = (item) => {
gen.state = item;
gen.status = "next";
callback(resolve, reject);
return Is.generatorFunc(fn) ?
/**
* @param {Parameters<typeof fn>} args
* @returns {Promise<ReturnType<typeof fn>>}
*/
function genCoroutine(...args) {
let gen = fn.apply(this, args);
gen.status = "next";
gen.state = undefined;
const callback = (resolve, reject) => {
let result,
nexts = resolve,
throws = reject;
try {
result = gen[gen.status](gen.state);
} catch (error) {
reject(error);
return;
}
throws = (err) => {
gen.state = err;
gen.status = "throw";
callback(resolve, reject);
if (!result.done) {
nexts = (item) => {
gen.state = item;
gen.status = "next";
callback(resolve, reject);
};
throws = (err) => {
gen.state = err;
gen.status = "throw";
callback(resolve, reject);
};
}
}
result = result.value;
Promise.resolve(result).then(nexts, throws);
}
return new Promise(callback);
} : (() => { throw new TypeError("gnc.of needs a GeneratorFunction.") })()
result = result.value;
Promise.resolve(result).then(nexts, throws);
};
return new Promise(callback);
} : (() => { throw new TypeError("gnc.of needs a GeneratorFunction."); })();
}
static is = Is;
};
export const gnc = GNC;
export const gnc = GNC;

27
noname/gnc/is.js Normal file
View File

@ -0,0 +1,27 @@
import { GeneratorFunction, Uninstantable } from "../util/index.js";
export class Is extends Uninstantable {
/**
* @param {*} item
* @returns {boolean}
*/
static coroutine(item) {
return typeof item == "function" && item.name == "genCoroutine";
}
/**
* @param {*} item
* @returns {boolean}
*/
static generatorFunc(item) {
return item instanceof GeneratorFunction;
}
/**
* @param {*} item
* @returns {boolean}
*/
static generator(item) {
return (typeof item == "object") && ("constructor" in item) && item.constructor && ("constructor" in item.constructor) && item.constructor.constructor === GeneratorFunction;
}
};