@expressive/react
Version:
Use classes to define state in React!
56 lines (54 loc) • 1.62 kB
JavaScript
import { provide } from "./context.js";
import { Context, METHOD, State, watch } from "@expressive/state";
import { Fragment } from "react";
import { Pragma } from "./state";
import Runtime from "react/jsx-runtime";
//#region src/jsx-runtime.ts
const RENDER = /* @__PURE__ */ new WeakMap();
function patch(type, ...args) {
if (State.is(type)) if (RENDER.has(type)) type = RENDER.get(type);
else RENDER.set(type, type = Render.bind(type));
return this(type, ...args);
}
const jsx = patch.bind(Runtime.jsx);
const jsxs = patch.bind(Runtime.jsxs);
function Render(props, props2) {
const { is, ...rest } = {
...props,
...props2
};
const ambient = Context.use();
const state = Pragma.useState(() => {
const instance = this.new(rest, is && ((x) => void is(x)));
const context = ambient.push(instance);
let ready;
let active;
watch(instance, (current) => {
active = current;
if (ready) state[1]((x) => x.bind(null));
});
function didMount() {
ready = true;
return () => {
context.pop();
instance.set(null);
};
}
function Render(props) {
const render = METHOD.get(active.render) || active.render;
return render ? render.call(active, props, active) : props.children;
}
return (props) => {
ready = false;
Pragma.useEffect(didMount, []);
Promise.resolve(instance.set(props)).finally(() => {
ready = true;
});
return provide(context, Pragma.createElement(Render, props), props.fallback || active.fallback, String(instance));
};
});
return state[0](rest);
}
//#endregion
export { Fragment, jsx, jsxs, patch };
//# sourceMappingURL=jsx-runtime.js.map