From 9163bb1e08f4d96582e6f41335850a5cf276cccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sat, 13 Nov 2021 15:58:59 +0100 Subject: [PATCH] [REF] app: move TemplateSet into its own file --- src/app/app.ts | 2 +- src/app/template_helpers.ts | 87 +++++---------------------- src/app/template_set.ts | 56 +++++++++++++++++ src/tags.ts | 2 +- tests/compiler/event_handling.test.ts | 2 +- tests/compiler/t_ref.test.ts | 2 +- tests/compiler/validation.test.ts | 2 +- tests/helpers.ts | 3 +- 8 files changed, 78 insertions(+), 78 deletions(-) create mode 100644 src/app/template_set.ts diff --git a/src/app/app.ts b/src/app/app.ts index 7217f658..235f99a2 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -2,7 +2,7 @@ import { Component } from "../component/component"; import { ComponentNode } from "../component/component_node"; import { MountOptions } from "../component/fibers"; import { Scheduler } from "../component/scheduler"; -import { TemplateSet } from "./template_helpers"; +import { TemplateSet } from "./template_set"; // reimplement dev mode stuff see last change in 0f7a8289a6fb8387c3c1af41c6664b2a8448758f diff --git a/src/app/template_helpers.ts b/src/app/template_helpers.ts index d2bb2115..c8fe974a 100644 --- a/src/app/template_helpers.ts +++ b/src/app/template_helpers.ts @@ -1,12 +1,6 @@ -import { BDom, createBlock, html, list, multi, text, toggler } from "../blockdom"; -import { compile, Template } from "../compiler"; -import { component } from "../component/component_node"; +import { BDom, multi, text, toggler } from "../blockdom"; import { validateProps } from "../component/props_validation"; -const bdom = { text, createBlock, list, multi, html, toggler, component }; - -export const globalTemplates: { [key: string]: string } = {}; - function withDefault(value: any, defaultValue: any): any { return value === undefined || value === null || value === false ? defaultValue : value; } @@ -82,71 +76,6 @@ function setContextValue(ctx: { [key: string]: any }, key: string, value: any): ctx[key] = value; } -export const UTILS = { - // elem, - // setText, - withDefault, - zero: Symbol("zero"), - isBoundary, - callSlot, - capture, - // toClassObj, - withKey, - prepareList, - setContextValue, - shallowEqual, - toNumber, - validateProps, -}; - -export class TemplateSet { - rawTemplates: { [name: string]: string } = Object.create(globalTemplates); - templates: { [name: string]: Template } = {}; - translateFn?: (s: string) => string; - translatableAttributes?: string[]; - utils: typeof UTILS; - dev?: boolean; - - constructor() { - const call = (subTemplate: string, ctx: any, parent: any) => { - const template = this.getTemplate(subTemplate); - return toggler(subTemplate, template(ctx, parent)); - }; - - const getTemplate = (name: string) => this.getTemplate(name); - this.utils = Object.assign({}, UTILS, { getTemplate, call }); - } - - addTemplate(name: string, template: string, options: { allowDuplicate?: boolean } = {}) { - if (name in this.rawTemplates && !options.allowDuplicate) { - throw new Error(`Template ${name} already defined`); - } - this.rawTemplates[name] = template; - } - - getTemplate(name: string): Template { - if (!(name in this.templates)) { - const rawTemplate = this.rawTemplates[name]; - if (rawTemplate === undefined) { - throw new Error(`Missing template: "${name}"`); - } - const templateFn = compile(rawTemplate, { - name, - dev: this.dev, - translateFn: this.translateFn, - translatableAttributes: this.translatableAttributes, - }); - - // first add a function to lazily get the template, in case there is a - // recursive call to the template name - this.templates[name] = (context, parent) => this.templates[name](context, parent); - const template = templateFn(bdom, this.utils); - this.templates[name] = template; - } - return this.templates[name]; - } -} - function toNumber(val: string): number | string { const n = parseFloat(val); return isNaN(n) ? val : n; @@ -160,3 +89,17 @@ function shallowEqual(l1: any[], l2: any[]): boolean { } return true; } + +export const UTILS = { + withDefault, + zero: Symbol("zero"), + isBoundary, + callSlot, + capture, + withKey, + prepareList, + setContextValue, + shallowEqual, + toNumber, + validateProps, +}; diff --git a/src/app/template_set.ts b/src/app/template_set.ts new file mode 100644 index 00000000..887b5e92 --- /dev/null +++ b/src/app/template_set.ts @@ -0,0 +1,56 @@ +import { createBlock, html, list, multi, text, toggler } from "../blockdom"; +import { compile, Template } from "../compiler"; +import { component } from "../component/component_node"; +import { UTILS } from "./template_helpers"; + +const bdom = { text, createBlock, list, multi, html, toggler, component }; + +export const globalTemplates: { [key: string]: string } = {}; + +export class TemplateSet { + rawTemplates: { [name: string]: string } = Object.create(globalTemplates); + templates: { [name: string]: Template } = {}; + translateFn?: (s: string) => string; + translatableAttributes?: string[]; + utils: typeof UTILS; + dev?: boolean; + + constructor() { + const call = (subTemplate: string, ctx: any, parent: any) => { + const template = this.getTemplate(subTemplate); + return toggler(subTemplate, template(ctx, parent)); + }; + + const getTemplate = (name: string) => this.getTemplate(name); + this.utils = Object.assign({}, UTILS, { getTemplate, call }); + } + + addTemplate(name: string, template: string, options: { allowDuplicate?: boolean } = {}) { + if (name in this.rawTemplates && !options.allowDuplicate) { + throw new Error(`Template ${name} already defined`); + } + this.rawTemplates[name] = template; + } + + getTemplate(name: string): Template { + if (!(name in this.templates)) { + const rawTemplate = this.rawTemplates[name]; + if (rawTemplate === undefined) { + throw new Error(`Missing template: "${name}"`); + } + const templateFn = compile(rawTemplate, { + name, + dev: this.dev, + translateFn: this.translateFn, + translatableAttributes: this.translatableAttributes, + }); + + // first add a function to lazily get the template, in case there is a + // recursive call to the template name + this.templates[name] = (context, parent) => this.templates[name](context, parent); + const template = templateFn(bdom, this.utils); + this.templates[name] = template; + } + return this.templates[name]; + } +} diff --git a/src/tags.ts b/src/tags.ts index 2195f34e..d5ff02cd 100644 --- a/src/tags.ts +++ b/src/tags.ts @@ -1,5 +1,5 @@ import { registerSheet } from "./component/style"; -import { globalTemplates } from "./app/template_helpers"; +import { globalTemplates } from "./app/template_set"; // ----------------------------------------------------------------------------- // Global templates diff --git a/tests/compiler/event_handling.test.ts b/tests/compiler/event_handling.test.ts index 6693cd70..3be41843 100644 --- a/tests/compiler/event_handling.test.ts +++ b/tests/compiler/event_handling.test.ts @@ -1,4 +1,4 @@ -import { TemplateSet } from "../../src/app/template_helpers"; +import { TemplateSet } from "../../src/app/template_set"; import { mount } from "../../src/blockdom"; import { makeTestFixture, renderToBdom, renderToString, snapshotEverything } from "../helpers"; diff --git a/tests/compiler/t_ref.test.ts b/tests/compiler/t_ref.test.ts index 96979094..3ec735d8 100644 --- a/tests/compiler/t_ref.test.ts +++ b/tests/compiler/t_ref.test.ts @@ -1,4 +1,4 @@ -import { TemplateSet } from "../../src/app/template_helpers"; +import { TemplateSet } from "../../src/app/template_set"; import { mount } from "../../src/blockdom"; import { renderToBdom, snapshotEverything } from "../helpers"; diff --git a/tests/compiler/validation.test.ts b/tests/compiler/validation.test.ts index 7c9b894e..db5680fe 100644 --- a/tests/compiler/validation.test.ts +++ b/tests/compiler/validation.test.ts @@ -1,4 +1,4 @@ -import { TemplateSet } from "../../src/app/template_helpers"; +import { TemplateSet } from "../../src/app/template_set"; import { renderToString, TestContext, compile } from "../helpers"; // ----------------------------------------------------------------------------- diff --git a/tests/helpers.ts b/tests/helpers.ts index 167a3c53..862cdcd5 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -16,7 +16,8 @@ import { BDom } from "../src/blockdom"; import { blockDom } from "../src"; import { compile as compileTemplate, Template } from "../src/compiler"; import { CodeGenOptions } from "../src/compiler/code_generator"; -import { globalTemplates, TemplateSet, UTILS } from "../src/app/template_helpers"; +import { UTILS } from "../src/app/template_helpers"; +import { globalTemplates, TemplateSet } from "../src/app/template_set"; import { xml } from "../src/tags"; const mount = blockDom.mount;