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