UNPKG

@virtualstate/examples

Version:
82 lines 2.6 kB
import { EnableThen, h } from "@virtualstate/fringe"; export function f(defaultValue) { let theThing; function ThingConstructor() { if (this instanceof ThingConstructor) { // Some other known state // console.log("Construct"); return f(defaultValue); } else { // console.log("Call"); return f(defaultValue); } } const almost = ThingConstructor; defineIt(almost); theThing = almost; return proxyIt(theThing); function defineIt(thing) { Object.defineProperties(thing, { [Symbol.asyncIterator]: { value: () => asyncIterable()[Symbol.asyncIterator]() }, [Symbol.iterator]: { value: () => iterable()[Symbol.iterator]() }, next: { value: () => iterable()[Symbol.iterator]().next() }, then: { value: (resolve, reject) => { // console.log("Await"); return asyncIterable()[Symbol.asyncIterator]().next().then(result => { if (result.done) { throw new Error("No value"); } return result.value; }).then(resolve, reject); } }, [EnableThen]: { value: true } }); } async function* asyncIterable() { // console.log("asyncIterable") yield defaultValue; } function* iterable() { // console.log("iterable") yield defaultValue; } function proxyIt(thing) { return new Proxy(thing, { get(target, p) { return target[p]; } }); } } async function F() { const t = f(1); const T = t; // if you wanted to use with h console.log({ t }); console.log({ await: await t }); console.log({ value: await t() }); console.log({ new: await new t() }); console.log({ h: (await (h(T, { h: 1 })))[0].source }); console.log({ string: await t `h` }); console.log({ string: await t `h` `what` }); console.log({ string: await (new t `h`()) `what` }); for (const item of t) console.log({ item }); for await (const awaitItem of t) console.log({ awaitItem }); console.log({ awaitNext: (await t.next()).value }); return t; } export const _EF0001_F = h(F, null); export const _EF0001_URL = import.meta.url; //# sourceMappingURL=the-thing.js.map