UNPKG

frontend-hamroun

Version:

A lightweight frontend JavaScript framework with React-like syntax

177 lines (176 loc) 6.21 kB
/** * Lifecycle events for components and application */ import { eventBus } from './event-bus.js'; export var LifecycleEvents; (function (LifecycleEvents) { LifecycleEvents["APP_INIT"] = "app:init"; LifecycleEvents["APP_MOUNTED"] = "app:mounted"; LifecycleEvents["APP_UPDATED"] = "app:updated"; LifecycleEvents["APP_ERROR"] = "app:error"; LifecycleEvents["APP_DESTROYED"] = "app:destroyed"; LifecycleEvents["COMPONENT_CREATED"] = "component:created"; LifecycleEvents["COMPONENT_MOUNTED"] = "component:mounted"; LifecycleEvents["COMPONENT_UPDATED"] = "component:updated"; LifecycleEvents["COMPONENT_ERROR"] = "component:error"; LifecycleEvents["COMPONENT_UNMOUNTED"] = "component:unmounted"; LifecycleEvents["ROUTER_BEFORE_CHANGE"] = "router:before-change"; LifecycleEvents["ROUTER_AFTER_CHANGE"] = "router:after-change"; LifecycleEvents["ROUTER_ERROR"] = "router:error"; LifecycleEvents["STORE_INITIALIZED"] = "store:initialized"; LifecycleEvents["STORE_BEFORE_ACTION"] = "store:before-action"; LifecycleEvents["STORE_AFTER_ACTION"] = "store:after-action"; LifecycleEvents["STORE_ERROR"] = "store:error"; })(LifecycleEvents || (LifecycleEvents = {})); // Event emitters export function emitAppInit(data) { eventBus.emit(LifecycleEvents.APP_INIT, data); } export function emitAppMounted(rootElement) { eventBus.emit(LifecycleEvents.APP_MOUNTED, rootElement); } export function emitAppUpdated() { eventBus.emit(LifecycleEvents.APP_UPDATED); } export function emitAppError(error) { eventBus.emit(LifecycleEvents.APP_ERROR, error); } export function emitAppDestroyed() { eventBus.emit(LifecycleEvents.APP_DESTROYED); } export function emitComponentCreated(info) { eventBus.emit(LifecycleEvents.COMPONENT_CREATED, info); } export function emitComponentMounted(info, element) { eventBus.emit(LifecycleEvents.COMPONENT_MOUNTED, info, element); } export function emitComponentUpdated(info, prevProps, newProps) { eventBus.emit(LifecycleEvents.COMPONENT_UPDATED, info, prevProps, newProps); } export function emitComponentError(info, error) { eventBus.emit(LifecycleEvents.COMPONENT_ERROR, info, error); } export function emitComponentUnmounted(info) { eventBus.emit(LifecycleEvents.COMPONENT_UNMOUNTED, info); } export function emitRouterBeforeChange(info) { return new Promise(resolve => { // Allow hooks to prevent navigation let prevented = false; const prevent = () => { prevented = true; }; eventBus.emit(LifecycleEvents.ROUTER_BEFORE_CHANGE, info, prevent); // Resolve with whether navigation should continue resolve(!prevented); }); } export function emitRouterAfterChange(info) { eventBus.emit(LifecycleEvents.ROUTER_AFTER_CHANGE, info); } export function emitRouterError(error, info) { eventBus.emit(LifecycleEvents.ROUTER_ERROR, error, info); } export function emitStoreInitialized(state) { eventBus.emit(LifecycleEvents.STORE_INITIALIZED, state); } export function emitStoreBeforeAction(actionType, payload, state) { eventBus.emit(LifecycleEvents.STORE_BEFORE_ACTION, actionType, payload, state); } export function emitStoreAfterAction(info) { eventBus.emit(LifecycleEvents.STORE_AFTER_ACTION, info); } export function emitStoreError(error, actionType, payload) { eventBus.emit(LifecycleEvents.STORE_ERROR, error, actionType, payload); } // Event listeners export function onAppInit(handler) { return eventBus.on(LifecycleEvents.APP_INIT, handler); } export function onAppMounted(handler) { return eventBus.on(LifecycleEvents.APP_MOUNTED, handler); } export function onAppUpdated(handler) { return eventBus.on(LifecycleEvents.APP_UPDATED, handler); } export function onAppError(handler) { return eventBus.on(LifecycleEvents.APP_ERROR, handler); } export function onAppDestroyed(handler) { return eventBus.on(LifecycleEvents.APP_DESTROYED, handler); } export function onComponentCreated(handler) { return eventBus.on(LifecycleEvents.COMPONENT_CREATED, handler); } export function onComponentMounted(handler) { return eventBus.on(LifecycleEvents.COMPONENT_MOUNTED, handler); } export function onComponentUpdated(handler) { return eventBus.on(LifecycleEvents.COMPONENT_UPDATED, handler); } export function onComponentError(handler) { return eventBus.on(LifecycleEvents.COMPONENT_ERROR, handler); } export function onComponentUnmounted(handler) { return eventBus.on(LifecycleEvents.COMPONENT_UNMOUNTED, handler); } export function onRouterBeforeChange(handler) { return eventBus.on(LifecycleEvents.ROUTER_BEFORE_CHANGE, handler); } export function onRouterAfterChange(handler) { return eventBus.on(LifecycleEvents.ROUTER_AFTER_CHANGE, handler); } export function onRouterError(handler) { return eventBus.on(LifecycleEvents.ROUTER_ERROR, handler); } export function onStoreInitialized(handler) { return eventBus.on(LifecycleEvents.STORE_INITIALIZED, handler); } export function onStoreBeforeAction(handler) { return eventBus.on(LifecycleEvents.STORE_BEFORE_ACTION, handler); } export function onStoreAfterAction(handler) { return eventBus.on(LifecycleEvents.STORE_AFTER_ACTION, handler); } export function onStoreError(handler) { return eventBus.on(LifecycleEvents.STORE_ERROR, handler); } export default { LifecycleEvents, // Emitters emitAppInit, emitAppMounted, emitAppUpdated, emitAppError, emitAppDestroyed, emitComponentCreated, emitComponentMounted, emitComponentUpdated, emitComponentError, emitComponentUnmounted, emitRouterBeforeChange, emitRouterAfterChange, emitRouterError, emitStoreInitialized, emitStoreBeforeAction, emitStoreAfterAction, emitStoreError, // Listeners onAppInit, onAppMounted, onAppUpdated, onAppError, onAppDestroyed, onComponentCreated, onComponentMounted, onComponentUpdated, onComponentError, onComponentUnmounted, onRouterBeforeChange, onRouterAfterChange, onRouterError, onStoreInitialized, onStoreBeforeAction, onStoreAfterAction, onStoreError };