@muban/muban
Version:
Writing components for server-rendered HTML
53 lines (52 loc) • 2.16 kB
JavaScript
// eslint-disable-next-line no-shadow
var DevtoolsHooks;
(function (DevtoolsHooks) {
DevtoolsHooks["APP_INIT"] = "app:init";
DevtoolsHooks["APP_UNMOUNT"] = "app:unmount";
DevtoolsHooks["COMPONENT_UPDATED"] = "component:updated";
DevtoolsHooks["COMPONENT_ADDED"] = "component:added";
DevtoolsHooks["COMPONENT_REMOVED"] = "component:removed";
DevtoolsHooks["COMPONENT_EMIT"] = "component:emit";
})(DevtoolsHooks || (DevtoolsHooks = {}));
// eslint-disable-next-line import/no-mutable-exports
export let devtools;
export function setDevtoolsHook(hook) {
devtools = hook;
}
export function devtoolsInitApp(app, version) {
// TODO queue if devtools is undefined
if (!devtools)
return;
devtools.emit(DevtoolsHooks.APP_INIT, app, version, {});
}
export function devtoolsUnmountApp(app) {
if (!devtools)
return;
devtools.emit(DevtoolsHooks.APP_UNMOUNT, app);
}
export const devtoolsComponentAdded = /*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_ADDED);
export const devtoolsComponentUpdated = /*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_UPDATED);
export const devtoolsComponentRemoved = /*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_REMOVED);
function createDevtoolsComponentHook(hook) {
return (component) => {
if (!devtools)
return;
devtools.emit(hook, component.appContext.app, component.uid, component.parent ? component.parent.uid : undefined, component);
};
}
export function devtoolsComponentEmit(component, event, parameters) {
if (!devtools)
return;
devtools.emit(DevtoolsHooks.COMPONENT_EMIT, component.appContext.app, component, event, parameters);
}
// eslint-disable-next-line unicorn/prevent-abbreviations
export function initDev() {
// TODO window interface
const target = (globalThis || window || {});
target.__MUBAN__ = true;
setDevtoolsHook(target.__MUBAN_DEVTOOLS_GLOBAL_HOOK__);
// console.info(
// `You are running a development build of Muban.\n` +
// `Make sure to use the production build when deploying for production.`,
// );
}