@nent/core
Version:
209 lines (202 loc) • 6.54 kB
JavaScript
/*!
* NENT 2022
*/
import { r as registerInstance, d as createEvent, h, a as getElement, H as Host } from './index-916ca544.js';
import { a as actionBus, e as eventBus } from './index-f7016b94.js';
import { f as debugIf, l as log, t as table, w as warn, a as dir } from './logging-5a93c8af.js';
import { a as state } from './state-27a8a5bc.js';
import { o as onChange, s as state$1 } from './state-6945acbc.js';
import { k as kebabToCamelCase } from './strings-47d55561.js';
import './index-4bfabbbd.js';
import './values-ddfac998.js';
let provider;
/**
* `setAppProvider` is a function that takes a string and an object as parameters and sets the value of
* the `provider` variable to the object.
* @param {string} name - The name of the provider.
* @param {any} p - the provider object
*/
function setAppProvider(name, p) {
debugIf(state.debug, `app-provider: ${name} registered`);
provider = p;
}
/**
* It returns the provider if it exists, otherwise it returns null
* @returns The provider variable.
*/
function getAppProvider() {
return provider;
}
/* istanbul ignore file */
const APP_TOPIC = 'app';
var APP_COMMANDS;
(function (APP_COMMANDS) {
APP_COMMANDS["RegisterProvider"] = "register-provider";
APP_COMMANDS["Log"] = "log";
APP_COMMANDS["Warn"] = "warn";
APP_COMMANDS["Dir"] = "dir";
APP_COMMANDS["SetDarkMode"] = "set-dark-mode";
})(APP_COMMANDS || (APP_COMMANDS = {}));
var APP_EVENTS;
(function (APP_EVENTS) {
APP_EVENTS["ThemeChanged"] = "theme";
})(APP_EVENTS || (APP_EVENTS = {}));
class DefaultAppProvider {
constructor(win = window, eventBus) {
this.win = win;
this.disposeThemeSubscription = onChange('darkMode', t => {
if (t == null)
win === null || win === void 0 ? void 0 : win.localStorage.removeItem('darkMode');
else
win === null || win === void 0 ? void 0 : win.localStorage.setItem('darkMode', t.toString());
eventBus === null || eventBus === void 0 ? void 0 : eventBus.emit(APP_EVENTS.ThemeChanged, t);
});
win.addEventListener('storage', () => {
this.getTheme();
});
this.getTheme();
}
getTheme() {
var _a;
const mode = (_a = this.win) === null || _a === void 0 ? void 0 : _a.localStorage.getItem('darkMode');
if (mode != null) {
state$1.darkMode = mode == 'true';
}
else {
state$1.darkMode = null;
}
}
setDarkMode(data) {
const { value } = data;
state$1.darkMode = value != undefined ? Boolean(value) : null;
}
log(data) {
const { message } = data;
if (message) {
log(message);
}
else {
table(data);
}
}
warn(data) {
const { message } = data;
if (message) {
warn(message);
}
else {
table(data);
}
}
dir(data) {
dir(data);
}
destroy() {
this.disposeThemeSubscription();
}
}
/* It listens for actions on the `APP_TOPIC` topic, and then calls the appropriate function on the
`AppProvider` that is registered for the current document */
class AppActionListener {
/**
* > This function is called by the `AppManager` to initialize the `AppProvider` with the `Window`
* object, the `actionBus` and the `eventBus`
* @param {Window} win - Window - the window object
* @param {IEventEmitter} actionBus - This is the event bus that the app will use to listen for
* actions.
* @param {IEventEmitter} eventBus - This is the event bus that the app will use to communicate with
* the rest of the application.
*/
initialize(win, actionBus, eventBus) {
this.eventBus = eventBus;
this.actionsSubscription = actionBus.on(APP_TOPIC, e => {
this.handleAction(e);
});
this.defaultProvider = new DefaultAppProvider(win, eventBus);
}
handleAction(actionEvent) {
debugIf(state.debug, `document-listener: action received ${JSON.stringify(actionEvent)}`);
if (actionEvent.command === APP_COMMANDS.RegisterProvider) {
const { name = 'unknown', provider } = actionEvent.data;
if (provider) {
setAppProvider(name, provider);
}
}
else {
const currentProvider = getAppProvider();
const commandFuncKey = kebabToCamelCase(actionEvent.command);
// Use the registered provider unless it doesn't implement this command
let commandFunc = currentProvider
? currentProvider[commandFuncKey]
: null;
if (!commandFunc)
commandFunc = this.defaultProvider[commandFuncKey];
if (commandFunc && typeof commandFunc === 'function') {
commandFunc.call(this.defaultProvider, actionEvent.data);
}
}
}
/**
* It unsubscribes from the actions subscription and destroys the default provider.
*/
destroy() {
var _a;
this.actionsSubscription();
(_a = this.defaultProvider) === null || _a === void 0 ? void 0 : _a.destroy();
}
}
const App = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.events = createEvent(this, "nent:events", 6);
this.actions = createEvent(this, "nent:actions", 3);
/**
* Turn on debugging to get helpful messages from the
* app, routing, data and action systems.
*/
this.debug = false;
}
/**
* Listen for outside command-requests. These events are delegated into
* the internal action-bus.
*/
delegateActionEventFromDOM(ev) {
const action = ev.detail;
actionBus.emit(action.topic, action);
}
componentWillLoad() {
state.debug = this.debug;
if (this.debug) {
log('n-app: initializing <debug>');
}
else {
log(`n-app: initializing`);
}
this.listener = new AppActionListener();
this.listener.initialize(window, actionBus, eventBus);
debugIf(state.debug, `n-app: services and listener registered`);
this.actionsSubscription = actionBus.on('*', (...args) => {
this.actions.emit(...args);
});
this.eventSubscription = eventBus.on('*', (...args) => {
this.events.emit(...args);
});
}
render() {
return h(Host, { style: { display: 'block' } });
}
componentDidLoad() {
log('n-app: initialized');
}
disconnectedCallback() {
var _a, _b;
this.listener.destroy();
(_a = this.actionsSubscription) === null || _a === void 0 ? void 0 : _a.call(this);
(_b = this.eventSubscription) === null || _b === void 0 ? void 0 : _b.call(this);
eventBus.removeAllListeners();
actionBus.removeAllListeners();
}
get el() { return getElement(this); }
};
App.style = "[n-cloak] { display: inherit; }";
export { App as n_app };