import { useSyncExternalStore } from "react";
const { page, base } = window.devpagePage;
const world = (window.devpageWorld ??= {
parts: {},
watchers: new Set(),
send: null,
put(part, state) {
this.parts[part] = state;
for (const watcher of this.watchers) watcher();
},
call(name, input = {}) {
this.send?.({ type: "call", name, input });
},
});
const watch = (watcher) => {
world.watchers.add(watcher);
return () => world.watchers.delete(watcher);
};
function useWorld(part) {
return useSyncExternalStore(watch, () => world.parts[part]);
}
export { page, base, world, useWorld };