diff --git a/noname/gnc/index.js b/noname/gnc/index.js index 3f42741f5..df5a6d83b 100644 --- a/noname/gnc/index.js +++ b/noname/gnc/index.js @@ -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} args + * @returns {Promise>} + */ + 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; \ No newline at end of file +export const gnc = GNC; diff --git a/noname/gnc/is.js b/noname/gnc/is.js new file mode 100644 index 000000000..7089fa988 --- /dev/null +++ b/noname/gnc/is.js @@ -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; + } +};