diff --git a/src/app/app.ts b/src/app/app.ts index 235f99a2..3dd4f76b 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -31,7 +31,7 @@ export class App extends TemplateSet { this.props = props; } - configure(config: Config) { + configure(config: Config): App { if (config.dev) { this.dev = config.dev; console.info(DEV_MSG); @@ -46,6 +46,7 @@ export class App extends TemplateSet { if (config.translatableAttributes) { this.translatableAttributes = config.translatableAttributes; } + return this; } mount(target: HTMLElement, options?: MountOptions): Promise> { diff --git a/src/app/template_helpers.ts b/src/app/template_helpers.ts index c8fe974a..ba815896 100644 --- a/src/app/template_helpers.ts +++ b/src/app/template_helpers.ts @@ -1,6 +1,11 @@ import { BDom, multi, text, toggler } from "../blockdom"; 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 { return value === undefined || value === null || value === false ? defaultValue : value; } @@ -19,7 +24,6 @@ function callSlot( if (defaultSlot) { let child1: BDom | undefined = undefined; let child2: BDom | undefined = undefined; - // const result = new BMulti(2); if (slotBDom) { child1 = dynamic ? toggler(name, slotBDom) : slotBDom; } else { diff --git a/src/component/fibers.ts b/src/component/fibers.ts index bb0c9ceb..39d3b204 100644 --- a/src/component/fibers.ts +++ b/src/component/fibers.ts @@ -3,7 +3,6 @@ import { mount } from "../blockdom"; import type { ComponentNode } from "./component_node"; import { STATUS } from "./status"; import { fibersInError } from "./error_handling"; -// import { mountBlock } from "./bdom/block"; export function makeChildFiber(node: ComponentNode, parent: Fiber): Fiber { let current = node.fiber; diff --git a/src/event_bus.ts b/src/event_bus.ts deleted file mode 100644 index 7ae4e86e..00000000 --- a/src/event_bus.ts +++ /dev/null @@ -1,5 +0,0 @@ -export class EventBus extends EventTarget { - trigger(name: string, payload?: any) { - this.dispatchEvent(new CustomEvent(name, { detail: payload })); - } -} diff --git a/src/index.ts b/src/index.ts index 154809f3..72d41ca2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import { toggler, } from "./blockdom"; import { mainEventHandler } from "./component/handler"; +import { EventBus, whenReady, loadFile } from "./utils"; config.shouldNormalizeDom = false; config.mainEventHandler = mainEventHandler; @@ -57,7 +58,7 @@ export { Memo } from "./misc/memo"; export { css, xml } from "./tags"; export { useState } from "./reactivity"; export { useRef } from "./refs"; -export { EventBus } from "./event_bus"; +export const utils = { EventBus, whenReady, loadFile }; export { onWillStart, diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 00000000..160bde91 --- /dev/null +++ b/src/utils.ts @@ -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 { + 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 { + const result = await fetch(url); + if (!result.ok) { + throw new Error("Error while fetching xml templates"); + } + return await result.text(); +} diff --git a/tests/event_bus.test.ts b/tests/utils.test.ts similarity index 95% rename from tests/event_bus.test.ts rename to tests/utils.test.ts index ac2b67f0..b3892fe2 100644 --- a/tests/event_bus.test.ts +++ b/tests/utils.test.ts @@ -1,4 +1,4 @@ -import { EventBus } from "../src/event_bus"; +import { EventBus } from "../src/utils"; describe("event bus behaviour", () => { test("can subscribe and be notified", () => {