pref: divide gnc.
This commit is contained in:
parent
d55b4a76af
commit
38546d0159
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue