@virtualstate/examples
Version:
62 lines • 1.9 kB
JavaScript
import { ChildrenOptions, h, createFragment } from "@virtualstate/fringe";
import { domain, input } from "./typing.js";
function TryItOut() {
console.log({ TryItOut: this });
this.counter = 0;
console.log({ TryItOutSet: this });
return (h(createFragment, null,
h(Child, null),
h(Child, null),
h(Child, null),
domain.map(fn => h(fn))));
}
function Child() {
console.log({ Child: this });
this.counter += 1;
return h("output", null, this.counter);
}
const context = new WeakMap();
const Provided = Symbol("Provided");
function proxyNode(options, defaultContext, node) {
return new Proxy(node, {
get(target, p) {
if (p === ChildrenOptions) {
return target[p] || options;
}
const value = target[p];
if (p === "source" && typeof value === "function") {
let sourceContext = context.get(options);
if (!sourceContext) {
sourceContext = {
...defaultContext
};
context.set(options, sourceContext);
}
return value.bind(sourceContext);
}
return value;
}
});
}
function createOptions(defaultContext) {
const options = {
createNode: h,
proxyNode() {
throw new Error("Not Implemented");
}
};
options.proxyNode = proxyNode.bind(undefined, options, defaultContext);
return options;
}
function bind(defaultContext, node) {
const options = createOptions(defaultContext);
return options.proxyNode?.(node) ?? node;
}
const bound = bind({
[Provided]: true,
counter: undefined,
...input
}, h(TryItOut, null));
export const _EC001_ChildrenOptions = bound;
export const _EC001_URL = import.meta.url;
//# sourceMappingURL=index.js.map