typed-dom
Advanced tools
Comparing version 0.0.250 to 0.0.251
import { Symbol } from 'spica/global'; | ||
import { hasOwnProperty } from 'spica/alias'; | ||
import { Elem, El, ElChildren } from './proxy'; | ||
import { Elem, El } from './proxy'; | ||
import { Factory, TagNameMap, Attrs, shadow, html, svg, define } from './util/dom'; | ||
@@ -8,4 +8,4 @@ | ||
<M extends TagNameMap, F extends Factory<M> = Factory<M>> = | ||
BuilderFunction<Extract<keyof M, string>, Element, F> & | ||
{ readonly [P in Extract<keyof M, string>]: BuilderMethod<P, Extract<M[P], Element>, F>; }; | ||
BuilderFunction<M, Extract<keyof M, string>, Element, F> & | ||
{ readonly [P in Extract<keyof M, string>]: BuilderMethod<M, P, Extract<M[P], Element>, F>; }; | ||
export function API | ||
@@ -22,24 +22,29 @@ <M extends TagNameMap, F extends Factory<M> = Factory<M>> | ||
type ElFactory<F extends Factory<TagNameMap>, T extends string, C extends ElChildren, E extends Element> = (baseFactory: F, tag: T, attrs: Attrs, children: C) => E; | ||
type Empty = readonly []; | ||
type ElFactory<M extends TagNameMap, F extends Factory<M>, T extends string, C extends El.Children, E extends Element> = (baseFactory: F, tag: T, attrs: Attrs, children: C) => E; | ||
interface BuilderFunction<T extends string, E extends Element, F extends Factory<TagNameMap>> { | ||
(tag: T, factory?: ElFactory<F, T, ElChildren.Void, E>): El<T, E, ElChildren.Void>; | ||
<C extends ElChildren.Text >(tag: T, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, string>; | ||
<C extends ElChildren.Array >(tag: T, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends ElChildren.Struct>(tag: T, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, C>; | ||
(tag: T, attrs?: Attrs, factory?: ElFactory<F, T, ElChildren.Void, E>): El<T, E, ElChildren.Void>; | ||
<C extends ElChildren.Text >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, string>; | ||
<C extends ElChildren.Array >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends ElChildren.Struct>(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, C>; | ||
interface BuilderFunction<M extends TagNameMap, T extends string, E extends Element, F extends Factory<M>> { | ||
(tag: T, factory?: ElFactory<M, F, T, El.Children.Void, E>): El<T, E, El.Children.Void>; | ||
<C extends Empty >(tag: T, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, El.Children.Array>; | ||
<C extends El.Children.Text >(tag: T, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, string>; | ||
<C extends El.Children.Array >(tag: T, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends El.Children.Struct>(tag: T, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, C>; | ||
(tag: T, attrs?: Attrs, factory?: ElFactory<M, F, T, El.Children.Void, E>): El<T, E, El.Children.Void>; | ||
<C extends Empty >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, El.Children.Array>; | ||
<C extends El.Children.Text >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, string>; | ||
<C extends El.Children.Array >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends El.Children.Struct>(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, C>; | ||
} | ||
interface BuilderMethod<T extends string, E extends Element, F extends Factory<TagNameMap>> { | ||
( factory?: ElFactory<F, T, ElChildren.Void, E>): El<T, E, ElChildren.Void>; | ||
<C extends ElChildren.Text >( children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, string>; | ||
<C extends ElChildren.Array >( children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends ElChildren.Struct>( children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, C>; | ||
( attrs?: Attrs, factory?: ElFactory<F, T, ElChildren.Void, E>): El<T, E, ElChildren.Void>; | ||
<C extends ElChildren.Text >( attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, string>; | ||
<C extends ElChildren.Array >( attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends ElChildren.Struct>( attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, C>; | ||
interface BuilderMethod<M extends TagNameMap, T extends string, E extends Element, F extends Factory<M>> { | ||
( factory?: ElFactory<M, F, T, El.Children.Void, E>): El<T, E, El.Children.Void>; | ||
<C extends Empty >( children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, El.Children.Array>; | ||
<C extends El.Children.Text >( children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, string>; | ||
<C extends El.Children.Array >( children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends El.Children.Struct>( children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, C>; | ||
( attrs?: Attrs, factory?: ElFactory<M, F, T, El.Children.Void, E>): El<T, E, El.Children.Void>; | ||
<C extends Empty >( attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, El.Children.Array>; | ||
<C extends El.Children.Text >( attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, string>; | ||
<C extends El.Children.Array >( attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends El.Children.Struct>( attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, C>; | ||
} | ||
@@ -61,4 +66,4 @@ | ||
function builder(tag: Extract<keyof M, string>, baseFactory: F): (attrs?: Attrs, children?: ElChildren, factory?: () => Element) => El { | ||
return function build(attrs?: Attrs | ElChildren , children?: ElChildren, factory?: ElFactory<F, Extract<keyof M, string>, ElChildren, Element>): El { | ||
function builder(tag: Extract<keyof M, string>, baseFactory: F): (attrs?: Attrs, children?: El.Children, factory?: () => Element) => El { | ||
return function build(attrs?: Attrs | El.Children , children?: El.Children, factory?: ElFactory<M, F, Extract<keyof M, string>, El.Children, Element>): El { | ||
if (typeof children === 'function') return build(attrs, void 0, children); | ||
@@ -73,3 +78,3 @@ if (typeof attrs === 'function') return build(void 0, void 0, attrs); | ||
function isElChildren(param: Attrs | ElChildren): param is ElChildren { | ||
function isElChildren(param: Attrs | El.Children): param is El.Children { | ||
if (param === void 0) return false; | ||
@@ -84,3 +89,3 @@ if (param[Symbol.iterator]) return true; | ||
function elem(factory: ElFactory<F, Extract<keyof M, string>, ElChildren, Element> | undefined, attrs: Attrs | undefined, children: ElChildren): Element { | ||
function elem(factory: ElFactory<M, F, Extract<keyof M, string>, El.Children, Element> | undefined, attrs: Attrs | undefined, children: El.Children): Element { | ||
const el = factory | ||
@@ -87,0 +92,0 @@ ? define(factory(baseFactory as F, tag, attrs ?? {}, children), attrs) |
import 'spica/global'; | ||
export { Shadow, HTML, SVG, API } from './src/builder'; | ||
export { El, ElChildren, proxy } from './src/proxy'; | ||
export { El, proxy } from './src/proxy'; | ||
export { NS, shadow, frag, html, svg, text, element, define, defrag } from './src/util/dom'; | ||
@@ -6,0 +6,0 @@ export { listen, delegate, bind, once, wait, currentTarget } from './src/util/listener'; |
{ | ||
"name": "typed-dom", | ||
"version": "0.0.250", | ||
"version": "0.0.251", | ||
"description": "A DOM component builder creating type-level DOM structures.", | ||
@@ -37,3 +37,3 @@ "private": false, | ||
"@types/i18next": "13.0.0", | ||
"@types/mocha": "9.1.0", | ||
"@types/mocha": "9.1.1", | ||
"@types/power-assert": "1.5.8", | ||
@@ -53,3 +53,3 @@ "benchmark": "^2.1.4", | ||
"gulp-unassert": "^2.0.0", | ||
"karma": "^6.3.17", | ||
"karma": "^6.3.19", | ||
"karma-chrome-launcher": "^3.1.1", | ||
@@ -62,5 +62,5 @@ "karma-coverage-istanbul-instrumenter": "^1.0.4", | ||
"mocha": "^9.2.2", | ||
"npm-check-updates": "^12.5.4", | ||
"npm-check-updates": "^12.5.9", | ||
"power-assert": "^1.6.1", | ||
"spica": "0.0.515", | ||
"spica": "0.0.521", | ||
"tsify": "^5.0.4", | ||
@@ -67,0 +67,0 @@ "typescript": "4.6.3", |
90
proxy.ts
@@ -8,2 +8,26 @@ import { Mutable } from 'spica/type'; | ||
const tag = Symbol.for('typed-dom::tag'); | ||
export interface El< | ||
T extends string = string, | ||
E extends Element = Element, | ||
C extends El.Children = El.Children, | ||
> { | ||
readonly [tag]?: T; | ||
readonly element: E; | ||
children: C; | ||
} | ||
export namespace El { | ||
export type Children = | ||
| Children.Void | ||
| Children.Text | ||
| Children.Array | ||
| Children.Struct; | ||
export namespace Children { | ||
export type Void = undefined; | ||
export type Text = string; | ||
export type Array = readonly El[]; | ||
export type Struct = { [field: string]: El; }; | ||
} | ||
} | ||
const enum ElChildType { | ||
@@ -16,26 +40,2 @@ Void, | ||
export type ElChildren = | ||
| ElChildren.Void | ||
| ElChildren.Text | ||
| ElChildren.Array | ||
| ElChildren.Struct; | ||
export namespace ElChildren { | ||
export type Void = undefined; | ||
export type Text = string; | ||
export type Array = readonly El[]; | ||
export type Struct = { [field: string]: El; }; | ||
} | ||
const proxies = new WeakMap<Element, El>(); | ||
export function proxy<E extends Element>(el: E): El<string, E, ElChildren>; | ||
export function proxy<C extends ElChildren>(el: Element): El<string, Element, C>; | ||
export function proxy<E extends Element, C extends ElChildren>(el: E): El<string, E, C>; | ||
export function proxy(el: Element): El { | ||
const proxy = proxies.get(el); | ||
if (proxy) return proxy; | ||
throw new Error(`TypedDOM: This element has no proxy.`); | ||
} | ||
namespace privates { | ||
@@ -55,20 +55,9 @@ export const id = Symbol(); | ||
const tag = Symbol.for('typed-dom::tag'); | ||
let id = identity(); | ||
let counter = 0; | ||
export interface El< | ||
T extends string = string, | ||
E extends Element = Element, | ||
C extends ElChildren = ElChildren, | ||
> { | ||
readonly [tag]?: T; | ||
readonly element: E; | ||
children: C; | ||
} | ||
export class Elem< | ||
T extends string, | ||
E extends Element, | ||
C extends ElChildren, | ||
C extends El.Children, | ||
> { | ||
@@ -112,3 +101,3 @@ constructor( | ||
define(this[privates.container], []); | ||
this[privates.children] = [] as ElChildren.Array as C; | ||
this[privates.children] = [] as El.Children.Array as C; | ||
this.children = children; | ||
@@ -119,3 +108,3 @@ this[privates.isInit] = false; | ||
define(this[privates.container], []); | ||
this[privates.children] = this[privates.observe]({ ...children as ElChildren.Struct }) as C; | ||
this[privates.children] = this[privates.observe]({ ...children as El.Children.Struct }) as C; | ||
this.children = children; | ||
@@ -165,3 +154,3 @@ this[privates.isInit] = false; | ||
private [privates.isPartialUpdate] = false; | ||
private [privates.observe](children: ElChildren.Struct): C { | ||
private [privates.observe](children: El.Children.Struct): C { | ||
const descs: PropertyDescriptorMap = {}; | ||
@@ -243,3 +232,3 @@ let i = -1; | ||
const oldText = targetChildren.data; | ||
const newText = children as ElChildren.Text; | ||
const newText = children as El.Children.Text; | ||
targetChildren.data = newText; | ||
@@ -251,5 +240,5 @@ if (newText === oldText) return; | ||
case ElChildType.Array: { | ||
const sourceChildren = children as ElChildren.Array; | ||
const targetChildren = [] as Mutable<ElChildren.Array>; | ||
this[privates.children] = targetChildren as ElChildren as C; | ||
const sourceChildren = children as El.Children.Array; | ||
const targetChildren = [] as Mutable<El.Children.Array>; | ||
this[privates.children] = targetChildren as El.Children as C; | ||
const nodeChildren = this[privates.container].children; | ||
@@ -283,4 +272,4 @@ for (let i = 0; i < sourceChildren.length; ++i) { | ||
case ElChildType.Struct: { | ||
const sourceChildren = children as ElChildren.Struct; | ||
const targetChildren = this[privates.children] as ElChildren.Struct; | ||
const sourceChildren = children as El.Children.Struct; | ||
const targetChildren = this[privates.children] as El.Children.Struct; | ||
assert.deepStrictEqual(Object.keys(sourceChildren), Object.keys(targetChildren)); | ||
@@ -333,2 +322,13 @@ for (const name of ObjectKeys(targetChildren)) { | ||
const proxies = new WeakMap<Element, El>(); | ||
export function proxy<E extends Element>(el: E): El<string, E, El.Children>; | ||
export function proxy<C extends El.Children>(el: Element): El<string, Element, C>; | ||
export function proxy<E extends Element, C extends El.Children>(el: E): El<string, E, C>; | ||
export function proxy(el: Element): El { | ||
const proxy = proxies.get(el); | ||
if (proxy) return proxy; | ||
throw new Error(`TypedDOM: This element has no proxy.`); | ||
} | ||
function throwErrorIfNotUsable({ element }: El): void { | ||
@@ -335,0 +335,0 @@ if (!element.parentElement || !proxies.has(element.parentElement)) return; |
import { Symbol } from 'spica/global'; | ||
import { hasOwnProperty } from 'spica/alias'; | ||
import { Elem, El, ElChildren } from './proxy'; | ||
import { Elem, El } from './proxy'; | ||
import { Factory, TagNameMap, Attrs, shadow, html, svg, define } from './util/dom'; | ||
@@ -8,4 +8,4 @@ | ||
<M extends TagNameMap, F extends Factory<M> = Factory<M>> = | ||
BuilderFunction<Extract<keyof M, string>, Element, F> & | ||
{ readonly [P in Extract<keyof M, string>]: BuilderMethod<P, Extract<M[P], Element>, F>; }; | ||
BuilderFunction<M, Extract<keyof M, string>, Element, F> & | ||
{ readonly [P in Extract<keyof M, string>]: BuilderMethod<M, P, Extract<M[P], Element>, F>; }; | ||
export function API | ||
@@ -22,24 +22,29 @@ <M extends TagNameMap, F extends Factory<M> = Factory<M>> | ||
type ElFactory<F extends Factory<TagNameMap>, T extends string, C extends ElChildren, E extends Element> = (baseFactory: F, tag: T, attrs: Attrs, children: C) => E; | ||
type Empty = readonly []; | ||
type ElFactory<M extends TagNameMap, F extends Factory<M>, T extends string, C extends El.Children, E extends Element> = (baseFactory: F, tag: T, attrs: Attrs, children: C) => E; | ||
interface BuilderFunction<T extends string, E extends Element, F extends Factory<TagNameMap>> { | ||
(tag: T, factory?: ElFactory<F, T, ElChildren.Void, E>): El<T, E, ElChildren.Void>; | ||
<C extends ElChildren.Text >(tag: T, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, string>; | ||
<C extends ElChildren.Array >(tag: T, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends ElChildren.Struct>(tag: T, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, C>; | ||
(tag: T, attrs?: Attrs, factory?: ElFactory<F, T, ElChildren.Void, E>): El<T, E, ElChildren.Void>; | ||
<C extends ElChildren.Text >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, string>; | ||
<C extends ElChildren.Array >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends ElChildren.Struct>(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, C>; | ||
interface BuilderFunction<M extends TagNameMap, T extends string, E extends Element, F extends Factory<M>> { | ||
(tag: T, factory?: ElFactory<M, F, T, El.Children.Void, E>): El<T, E, El.Children.Void>; | ||
<C extends Empty >(tag: T, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, El.Children.Array>; | ||
<C extends El.Children.Text >(tag: T, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, string>; | ||
<C extends El.Children.Array >(tag: T, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends El.Children.Struct>(tag: T, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, C>; | ||
(tag: T, attrs?: Attrs, factory?: ElFactory<M, F, T, El.Children.Void, E>): El<T, E, El.Children.Void>; | ||
<C extends Empty >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, El.Children.Array>; | ||
<C extends El.Children.Text >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, string>; | ||
<C extends El.Children.Array >(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends El.Children.Struct>(tag: T, attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, C>; | ||
} | ||
interface BuilderMethod<T extends string, E extends Element, F extends Factory<TagNameMap>> { | ||
( factory?: ElFactory<F, T, ElChildren.Void, E>): El<T, E, ElChildren.Void>; | ||
<C extends ElChildren.Text >( children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, string>; | ||
<C extends ElChildren.Array >( children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends ElChildren.Struct>( children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, C>; | ||
( attrs?: Attrs, factory?: ElFactory<F, T, ElChildren.Void, E>): El<T, E, ElChildren.Void>; | ||
<C extends ElChildren.Text >( attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, string>; | ||
<C extends ElChildren.Array >( attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends ElChildren.Struct>( attrs?: Attrs, children?: C, factory?: ElFactory<F, T, C, E> ): El<T, E, C>; | ||
interface BuilderMethod<M extends TagNameMap, T extends string, E extends Element, F extends Factory<M>> { | ||
( factory?: ElFactory<M, F, T, El.Children.Void, E>): El<T, E, El.Children.Void>; | ||
<C extends Empty >( children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, El.Children.Array>; | ||
<C extends El.Children.Text >( children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, string>; | ||
<C extends El.Children.Array >( children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends El.Children.Struct>( children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, C>; | ||
( attrs?: Attrs, factory?: ElFactory<M, F, T, El.Children.Void, E>): El<T, E, El.Children.Void>; | ||
<C extends Empty >( attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, El.Children.Array>; | ||
<C extends El.Children.Text >( attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, string>; | ||
<C extends El.Children.Array >( attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, Readonly<C>>; | ||
<C extends El.Children.Struct>( attrs?: Attrs, children?: C, factory?: ElFactory<M, F, T, C, E> ): El<T, E, C>; | ||
} | ||
@@ -61,4 +66,4 @@ | ||
function builder(tag: Extract<keyof M, string>, baseFactory: F): (attrs?: Attrs, children?: ElChildren, factory?: () => Element) => El { | ||
return function build(attrs?: Attrs | ElChildren , children?: ElChildren, factory?: ElFactory<F, Extract<keyof M, string>, ElChildren, Element>): El { | ||
function builder(tag: Extract<keyof M, string>, baseFactory: F): (attrs?: Attrs, children?: El.Children, factory?: () => Element) => El { | ||
return function build(attrs?: Attrs | El.Children , children?: El.Children, factory?: ElFactory<M, F, Extract<keyof M, string>, El.Children, Element>): El { | ||
if (typeof children === 'function') return build(attrs, void 0, children); | ||
@@ -73,3 +78,3 @@ if (typeof attrs === 'function') return build(void 0, void 0, attrs); | ||
function isElChildren(param: Attrs | ElChildren): param is ElChildren { | ||
function isElChildren(param: Attrs | El.Children): param is El.Children { | ||
if (param === void 0) return false; | ||
@@ -84,3 +89,3 @@ if (param[Symbol.iterator]) return true; | ||
function elem(factory: ElFactory<F, Extract<keyof M, string>, ElChildren, Element> | undefined, attrs: Attrs | undefined, children: ElChildren): Element { | ||
function elem(factory: ElFactory<M, F, Extract<keyof M, string>, El.Children, Element> | undefined, attrs: Attrs | undefined, children: El.Children): Element { | ||
const el = factory | ||
@@ -87,0 +92,0 @@ ? define(factory(baseFactory as F, tag, attrs ?? {}, children), attrs) |
@@ -8,2 +8,26 @@ import { Mutable } from 'spica/type'; | ||
const tag = Symbol.for('typed-dom::tag'); | ||
export interface El< | ||
T extends string = string, | ||
E extends Element = Element, | ||
C extends El.Children = El.Children, | ||
> { | ||
readonly [tag]?: T; | ||
readonly element: E; | ||
children: C; | ||
} | ||
export namespace El { | ||
export type Children = | ||
| Children.Void | ||
| Children.Text | ||
| Children.Array | ||
| Children.Struct; | ||
export namespace Children { | ||
export type Void = undefined; | ||
export type Text = string; | ||
export type Array = readonly El[]; | ||
export type Struct = { [field: string]: El; }; | ||
} | ||
} | ||
const enum ElChildType { | ||
@@ -16,26 +40,2 @@ Void, | ||
export type ElChildren = | ||
| ElChildren.Void | ||
| ElChildren.Text | ||
| ElChildren.Array | ||
| ElChildren.Struct; | ||
export namespace ElChildren { | ||
export type Void = undefined; | ||
export type Text = string; | ||
export type Array = readonly El[]; | ||
export type Struct = { [field: string]: El; }; | ||
} | ||
const proxies = new WeakMap<Element, El>(); | ||
export function proxy<E extends Element>(el: E): El<string, E, ElChildren>; | ||
export function proxy<C extends ElChildren>(el: Element): El<string, Element, C>; | ||
export function proxy<E extends Element, C extends ElChildren>(el: E): El<string, E, C>; | ||
export function proxy(el: Element): El { | ||
const proxy = proxies.get(el); | ||
if (proxy) return proxy; | ||
throw new Error(`TypedDOM: This element has no proxy.`); | ||
} | ||
namespace privates { | ||
@@ -55,20 +55,9 @@ export const id = Symbol(); | ||
const tag = Symbol.for('typed-dom::tag'); | ||
let id = identity(); | ||
let counter = 0; | ||
export interface El< | ||
T extends string = string, | ||
E extends Element = Element, | ||
C extends ElChildren = ElChildren, | ||
> { | ||
readonly [tag]?: T; | ||
readonly element: E; | ||
children: C; | ||
} | ||
export class Elem< | ||
T extends string, | ||
E extends Element, | ||
C extends ElChildren, | ||
C extends El.Children, | ||
> { | ||
@@ -112,3 +101,3 @@ constructor( | ||
define(this[privates.container], []); | ||
this[privates.children] = [] as ElChildren.Array as C; | ||
this[privates.children] = [] as El.Children.Array as C; | ||
this.children = children; | ||
@@ -119,3 +108,3 @@ this[privates.isInit] = false; | ||
define(this[privates.container], []); | ||
this[privates.children] = this[privates.observe]({ ...children as ElChildren.Struct }) as C; | ||
this[privates.children] = this[privates.observe]({ ...children as El.Children.Struct }) as C; | ||
this.children = children; | ||
@@ -165,3 +154,3 @@ this[privates.isInit] = false; | ||
private [privates.isPartialUpdate] = false; | ||
private [privates.observe](children: ElChildren.Struct): C { | ||
private [privates.observe](children: El.Children.Struct): C { | ||
const descs: PropertyDescriptorMap = {}; | ||
@@ -243,3 +232,3 @@ let i = -1; | ||
const oldText = targetChildren.data; | ||
const newText = children as ElChildren.Text; | ||
const newText = children as El.Children.Text; | ||
targetChildren.data = newText; | ||
@@ -251,5 +240,5 @@ if (newText === oldText) return; | ||
case ElChildType.Array: { | ||
const sourceChildren = children as ElChildren.Array; | ||
const targetChildren = [] as Mutable<ElChildren.Array>; | ||
this[privates.children] = targetChildren as ElChildren as C; | ||
const sourceChildren = children as El.Children.Array; | ||
const targetChildren = [] as Mutable<El.Children.Array>; | ||
this[privates.children] = targetChildren as El.Children as C; | ||
const nodeChildren = this[privates.container].children; | ||
@@ -283,4 +272,4 @@ for (let i = 0; i < sourceChildren.length; ++i) { | ||
case ElChildType.Struct: { | ||
const sourceChildren = children as ElChildren.Struct; | ||
const targetChildren = this[privates.children] as ElChildren.Struct; | ||
const sourceChildren = children as El.Children.Struct; | ||
const targetChildren = this[privates.children] as El.Children.Struct; | ||
assert.deepStrictEqual(Object.keys(sourceChildren), Object.keys(targetChildren)); | ||
@@ -333,2 +322,13 @@ for (const name of ObjectKeys(targetChildren)) { | ||
const proxies = new WeakMap<Element, El>(); | ||
export function proxy<E extends Element>(el: E): El<string, E, El.Children>; | ||
export function proxy<C extends El.Children>(el: Element): El<string, Element, C>; | ||
export function proxy<E extends Element, C extends El.Children>(el: E): El<string, E, C>; | ||
export function proxy(el: Element): El { | ||
const proxy = proxies.get(el); | ||
if (proxy) return proxy; | ||
throw new Error(`TypedDOM: This element has no proxy.`); | ||
} | ||
function throwErrorIfNotUsable({ element }: El): void { | ||
@@ -335,0 +335,0 @@ if (!element.parentElement || !proxies.has(element.parentElement)) return; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
654887
15585