[REF] move event_bus into utils, readd 2 functions

This commit is contained in:
Géry Debongnie
2021-11-13 16:21:01 +01:00
committed by Aaron Bohy
parent c1439814bf
commit 1658d15b87
7 changed files with 33 additions and 10 deletions
+2 -1
View File
@@ -31,7 +31,7 @@ export class App<T extends typeof Component = any> extends TemplateSet {
this.props = props; this.props = props;
} }
configure(config: Config) { configure(config: Config): App<T> {
if (config.dev) { if (config.dev) {
this.dev = config.dev; this.dev = config.dev;
console.info(DEV_MSG); console.info(DEV_MSG);
@@ -46,6 +46,7 @@ export class App<T extends typeof Component = any> extends TemplateSet {
if (config.translatableAttributes) { if (config.translatableAttributes) {
this.translatableAttributes = config.translatableAttributes; this.translatableAttributes = config.translatableAttributes;
} }
return this;
} }
mount(target: HTMLElement, options?: MountOptions): Promise<InstanceType<T>> { mount(target: HTMLElement, options?: MountOptions): Promise<InstanceType<T>> {
+5 -1
View File
@@ -1,6 +1,11 @@
import { BDom, multi, text, toggler } from "../blockdom"; import { BDom, multi, text, toggler } from "../blockdom";
import { validateProps } from "../component/props_validation"; import { validateProps } from "../component/props_validation";
/**
* This file contains utility functions that will be injected in each template,
* to perform various useful tasks in the compiled code.
*/
function withDefault(value: any, defaultValue: any): any { function withDefault(value: any, defaultValue: any): any {
return value === undefined || value === null || value === false ? defaultValue : value; return value === undefined || value === null || value === false ? defaultValue : value;
} }
@@ -19,7 +24,6 @@ function callSlot(
if (defaultSlot) { if (defaultSlot) {
let child1: BDom | undefined = undefined; let child1: BDom | undefined = undefined;
let child2: BDom | undefined = undefined; let child2: BDom | undefined = undefined;
// const result = new BMulti(2);
if (slotBDom) { if (slotBDom) {
child1 = dynamic ? toggler(name, slotBDom) : slotBDom; child1 = dynamic ? toggler(name, slotBDom) : slotBDom;
} else { } else {
-1
View File
@@ -3,7 +3,6 @@ import { mount } from "../blockdom";
import type { ComponentNode } from "./component_node"; import type { ComponentNode } from "./component_node";
import { STATUS } from "./status"; import { STATUS } from "./status";
import { fibersInError } from "./error_handling"; import { fibersInError } from "./error_handling";
// import { mountBlock } from "./bdom/block";
export function makeChildFiber(node: ComponentNode, parent: Fiber): Fiber { export function makeChildFiber(node: ComponentNode, parent: Fiber): Fiber {
let current = node.fiber; let current = node.fiber;
-5
View File
@@ -1,5 +0,0 @@
export class EventBus extends EventTarget {
trigger(name: string, payload?: any) {
this.dispatchEvent(new CustomEvent(name, { detail: payload }));
}
}
+2 -1
View File
@@ -11,6 +11,7 @@ import {
toggler, toggler,
} from "./blockdom"; } from "./blockdom";
import { mainEventHandler } from "./component/handler"; import { mainEventHandler } from "./component/handler";
import { EventBus, whenReady, loadFile } from "./utils";
config.shouldNormalizeDom = false; config.shouldNormalizeDom = false;
config.mainEventHandler = mainEventHandler; config.mainEventHandler = mainEventHandler;
@@ -57,7 +58,7 @@ export { Memo } from "./misc/memo";
export { css, xml } from "./tags"; export { css, xml } from "./tags";
export { useState } from "./reactivity"; export { useState } from "./reactivity";
export { useRef } from "./refs"; export { useRef } from "./refs";
export { EventBus } from "./event_bus"; export const utils = { EventBus, whenReady, loadFile };
export { export {
onWillStart, onWillStart,
+23
View File
@@ -0,0 +1,23 @@
export class EventBus extends EventTarget {
trigger(name: string, payload?: any) {
this.dispatchEvent(new CustomEvent(name, { detail: payload }));
}
}
export function whenReady(fn?: any): Promise<void> {
return new Promise(function (resolve) {
if (document.readyState !== "loading") {
resolve(true);
} else {
document.addEventListener("DOMContentLoaded", resolve, false);
}
}).then(fn || function () {});
}
export async function loadFile(url: string): Promise<string> {
const result = await fetch(url);
if (!result.ok) {
throw new Error("Error while fetching xml templates");
}
return await result.text();
}
@@ -1,4 +1,4 @@
import { EventBus } from "../src/event_bus"; import { EventBus } from "../src/utils";
describe("event bus behaviour", () => { describe("event bus behaviour", () => {
test("can subscribe and be notified", () => { test("can subscribe and be notified", () => {