@wrnrlr/prelude 0.2.10 → 0.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/runtime.ts CHANGED
@@ -1,12 +1,15 @@
1
1
  // @ts-nocheck:
2
2
  import {effect,untrack,root} from './reactive.ts'
3
3
  import {SVGNamespace,SVGElements,ChildProperties,getPropAlias,Properties,Aliases,DelegatedEvents} from './constants.ts'
4
+ import {reconcileArrays} from './domdiff.ts'
4
5
  // import type {Mountable} from './constants.ts'
5
6
 
6
7
  const {isArray} = Array
7
8
 
8
9
  export type Mountable = HTMLElement | Document | ShadowRoot | DocumentFragment | Node | string | number | bigint | symbol;
9
10
 
11
+ export const r = { render, insert, spread, assign, element, component, text, isChild, clearDelegatedEvents}
12
+
10
13
  // declare global {
11
14
  // interface Document {
12
15
  // '_$DX_DELEGATE'?: Record<string, Set<unknown>>
@@ -45,264 +48,271 @@ Create `Runtime` for `window`
45
48
  @param window
46
49
  @group Internal
47
50
  */
48
- export function runtime(w:Window):Runtime {
49
- const document = w.document
50
- const isSVG = (e: unknown) => e instanceof (w.SVGElement),
51
- element = (name:string) => SVGElements.has(name) ?
52
- document.createElementNS("http://www.w3.org/2000/svg",name) : document.createElement(name),
53
- text = (s:string) => document.createTextNode(s)
54
-
55
- function isChild(a:unknown): a is Element {
56
- return a instanceof Element
57
- }
58
51
 
59
- function component(fn:()=>unknown) {
60
- return untrack(fn)
61
- }
62
52
 
63
- function render(code: ()=>void, element:Element|Document, init?: unknown): void {
64
- if (!element) throw new Error("The `element` passed to `render(..., element)` doesn't exist.");
65
- root(() => {
66
- if (element instanceof Document) code()
67
- else insert(element as Element, code(), element.firstChild ? null : undefined, init)
68
- })
69
- }
53
+ const document = globalThis.document
70
54
 
71
- type Value = string | number | Element | Text
72
- type RxValue = (()=>Value) | Value
55
+ const isSVG = (e: unknown) => e instanceof (globalThis.window.SVGElement)
73
56
 
74
- function insert(parent: Element, accessor: string, marker?: Node|null, initial?: any): void
75
- function insert(parent: Element, accessor: ()=>string, marker?: Node|null, initial?: any): void
76
- function insert(parent: Element, accessor: string|(()=>string), marker?: Node|null, initial?: any): void {
77
- if (marker !== undefined && !initial) initial = []
78
- if (typeof accessor !== 'function') return insertExpression(parent, accessor, initial||[], marker)
79
- let current = initial||[]
80
- effect(() => {current = insertExpression(parent, accessor(), current, marker)})
81
- }
57
+ function element(name:string) {
58
+ return SVGElements.has(name) ?
59
+ globalThis.document.createElementNS("http://www.w3.org/2000/svg",name) : globalThis.document.createElement(name)
60
+ }
82
61
 
83
- function spread(node:Element, props:any = {}, skipChildren:boolean) {
84
- const prevProps:any = {}
85
- if (!skipChildren) effect(() => (prevProps.children = insertExpression(node, props.children, prevProps.children)))
86
- effect(() => (props.ref?.call ? untrack(() => props.ref(node)) : (props.ref = node)))
87
- effect(() => assign(node, props, true, prevProps, true))
88
- return prevProps
89
- }
62
+ function text(s:string) {
63
+ return globalThis.document.createTextNode(s)
64
+ }
90
65
 
91
- function assign(node:Element, props:any, skipChildren:boolean, prevProps:any = {}, skipRef:boolean = false) {
92
- const svg = isSVG(node)
93
- props || (props = {})
94
- for (const prop in prevProps) {
95
- if (!(prop in props)) {
96
- if (prop === "children") continue;
97
- prevProps[prop] = assignProp(node, prop, null, prevProps[prop], svg, skipRef);
98
- }
66
+ function isChild(a:unknown): a is Element {
67
+ return a instanceof Element
68
+ }
69
+
70
+ function component(fn:()=>unknown) {
71
+ return untrack(fn)
72
+ }
73
+
74
+ function render(code: ()=>void, element:Element|Document, init?: unknown): void {
75
+ if (!element) throw new Error("The `element` passed to `render(..., element)` doesn't exist.");
76
+ root(() => {
77
+ if (element instanceof Document) code()
78
+ else insert(element as Element, code(), element.firstChild ? null : undefined, init)
79
+ })
80
+ }
81
+
82
+ type Value = string | number | Element | Text
83
+ type RxValue = (()=>Value) | Value
84
+
85
+ function insert(parent: Element, accessor: string, marker?: Node|null, initial?: any): void
86
+ function insert(parent: Element, accessor: ()=>string, marker?: Node|null, initial?: any): void
87
+ function insert(parent: Element, accessor: string|(()=>string), marker?: Node|null, initial?: any): void {
88
+ if (marker !== undefined && !initial) initial = []
89
+ if (typeof accessor !== 'function') return insertExpression(parent, accessor, initial||[], marker)
90
+ let current = initial||[]
91
+ effect(() => {current = insertExpression(parent, accessor(), current, marker)})
92
+ }
93
+
94
+ function spread(node:Element, props:any = {}, skipChildren:boolean) {
95
+ const prevProps:any = {}
96
+ if (!skipChildren) effect(() => (prevProps.children = insertExpression(node, props.children, prevProps.children)))
97
+ effect(() => (props.ref?.call ? untrack(() => props.ref(node)) : (props.ref = node)))
98
+ effect(() => assign(node, props, true, prevProps, true))
99
+ return prevProps
100
+ }
101
+
102
+ function assign(node:Element, props:any, skipChildren:boolean, prevProps:any = {}, skipRef:boolean = false) {
103
+ const svg = isSVG(node)
104
+ props || (props = {})
105
+ for (const prop in prevProps) {
106
+ if (!(prop in props)) {
107
+ if (prop === "children") continue;
108
+ prevProps[prop] = assignProp(node, prop, null, prevProps[prop], svg, skipRef);
99
109
  }
100
- for (const prop in props) {
101
- if (prop === "children") {
102
- if (!skipChildren) insertExpression(node, props.children);
103
- continue;
104
- }
105
- const value = props[prop];
106
- prevProps[prop] = assignProp(node, prop, value, prevProps[prop], svg, skipRef);
110
+ }
111
+ for (const prop in props) {
112
+ if (prop === "children") {
113
+ if (!skipChildren) insertExpression(node, props.children);
114
+ continue;
107
115
  }
116
+ const value = props[prop];
117
+ prevProps[prop] = assignProp(node, prop, value, prevProps[prop], svg, skipRef);
108
118
  }
119
+ }
109
120
 
110
- function assignProp(node: Element, prop: 'style', value: StyleProps, prev: StyleProps, isSVG: boolean, skipRef: boolean): StyleProps
111
- function assignProp(node: Element, prop: 'classList', value: ClassListProps, prev: ClassListProps, isSVG: boolean, skipRef: boolean): ClassListProps
112
- function assignProp(node: Element, prop: 'ref', value: ()=>string, prev:string, isSVG: boolean, skipRef: false): string
113
- function assignProp(node: Element, prop: string, value: string, prev: string|undefined, isSVG: boolean, skipRef: boolean): string
114
- function assignProp(node: Element, prop: string, value: undefined, prev: string|undefined, isSVG: boolean, skipRef: boolean): undefined
115
- function assignProp(
116
- node: Element,
117
- prop: 'style' | 'classList' | 'ref' | string,
118
- value: StyleProps | ClassListProps | (()=>string) | string | undefined,
119
- prev: StyleProps | ClassListProps | (()=>string) | string | undefined,
120
- isSVG: false | boolean,
121
- skipRef: boolean
121
+ function assignProp(node: Element, prop: 'style', value: StyleProps, prev: StyleProps, isSVG: boolean, skipRef: boolean): StyleProps
122
+ function assignProp(node: Element, prop: 'classList', value: ClassListProps, prev: ClassListProps, isSVG: boolean, skipRef: boolean): ClassListProps
123
+ function assignProp(node: Element, prop: 'ref', value: ()=>string, prev:string, isSVG: boolean, skipRef: false): string
124
+ function assignProp(node: Element, prop: string, value: string, prev: string|undefined, isSVG: boolean, skipRef: boolean): string
125
+ function assignProp(node: Element, prop: string, value: undefined, prev: string|undefined, isSVG: boolean, skipRef: boolean): undefined
126
+ function assignProp(
127
+ node: Element,
128
+ prop: 'style' | 'classList' | 'ref' | string,
129
+ value: StyleProps | ClassListProps | (()=>string) | string | undefined,
130
+ prev: StyleProps | ClassListProps | (()=>string) | string | undefined,
131
+ isSVG: false | boolean,
132
+ skipRef: boolean
133
+ ) {
134
+ let isCE, isProp, isChildProp, propAlias, forceProp;
135
+ if (prop === 'style') return style(node, value, prev);
136
+ if (prop === 'classList') return classList(node, value, prev);
137
+ if (value === prev) return prev;
138
+ if (prop === 'ref') {
139
+ if (!skipRef) value(node);
140
+ } else if (prop.slice(0, 3) === 'on:') {
141
+ const e = prop.slice(3);
142
+ prev && node.removeEventListener(e, prev);
143
+ value && node.addEventListener(e, value);
144
+ } else if (prop.slice(0, 10) === 'oncapture:') {
145
+ const e = prop.slice(10);
146
+ prev && node.removeEventListener(e, prev, true);
147
+ value && node.addEventListener(e, value, true);
148
+ } else if (prop.slice(0, 2) === 'on') {
149
+ const name = prop.slice(2).toLowerCase();
150
+ const delegate = DelegatedEvents.has(name);
151
+ if (!delegate && prev) {
152
+ const h = isArray(prev) ? prev[0] : prev;
153
+ node.removeEventListener(name, h);
154
+ }
155
+ if (delegate || value) {
156
+ addEventListener(node, name, value, delegate);
157
+ delegate && delegateEvents([name],globalThis.document);
158
+ }
159
+ } else if (prop.slice(0, 5) === 'attr:') {
160
+ setAttribute(node, prop.slice(5), value);
161
+ } else if (
162
+ (forceProp = prop.slice(0, 5) === 'prop:') ||
163
+ (isChildProp = ChildProperties.has(prop)) ||
164
+ (!isSVG && ((propAlias = getPropAlias(prop, node.tagName)) || (isProp = Properties.has(prop)))) ||
165
+ (isCE = node.nodeName.includes('-'))
122
166
  ) {
123
- let isCE, isProp, isChildProp, propAlias, forceProp;
124
- if (prop === 'style') return style(node, value, prev);
125
- if (prop === 'classList') return classList(node, value, prev);
126
- if (value === prev) return prev;
127
- if (prop === 'ref') {
128
- if (!skipRef) value(node);
129
- } else if (prop.slice(0, 3) === 'on:') {
130
- const e = prop.slice(3);
131
- prev && node.removeEventListener(e, prev);
132
- value && node.addEventListener(e, value);
133
- } else if (prop.slice(0, 10) === 'oncapture:') {
134
- const e = prop.slice(10);
135
- prev && node.removeEventListener(e, prev, true);
136
- value && node.addEventListener(e, value, true);
137
- } else if (prop.slice(0, 2) === 'on') {
138
- const name = prop.slice(2).toLowerCase();
139
- const delegate = DelegatedEvents.has(name);
140
- if (!delegate && prev) {
141
- const h = isArray(prev) ? prev[0] : prev;
142
- node.removeEventListener(name, h);
143
- }
144
- if (delegate || value) {
145
- addEventListener(node, name, value, delegate);
146
- delegate && delegateEvents([name],document);
147
- }
148
- } else if (prop.slice(0, 5) === 'attr:') {
149
- setAttribute(node, prop.slice(5), value);
150
- } else if (
151
- (forceProp = prop.slice(0, 5) === 'prop:') ||
152
- (isChildProp = ChildProperties.has(prop)) ||
153
- (!isSVG && ((propAlias = getPropAlias(prop, node.tagName)) || (isProp = Properties.has(prop)))) ||
154
- (isCE = node.nodeName.includes('-'))
155
- ) {
156
- if (forceProp) {
157
- prop = prop.slice(5);
158
- isProp = true;
159
- }
160
- if (prop === 'class' || prop === 'className') if (value) node.className = value; else node.removeAttribute('class')
161
- else if (isCE && !isProp && !isChildProp) node[toPropertyName(prop)] = value;
162
- else node[propAlias || prop] = value;
163
- } else {
164
- const ns = isSVG && prop.indexOf(':') > -1 && SVGNamespace[prop.split(':')[0]]
165
- if (ns) setAttributeNS(node, ns, prop, value)
166
- else setAttribute(node, Aliases[prop] || prop, value)
167
+ if (forceProp) {
168
+ prop = prop.slice(5);
169
+ isProp = true;
167
170
  }
168
- return value;
171
+ if (prop === 'class' || prop === 'className') if (value) node.className = value; else node.removeAttribute('class')
172
+ else if (isCE && !isProp && !isChildProp) node[toPropertyName(prop)] = value;
173
+ else node[propAlias || prop] = value;
174
+ } else {
175
+ const ns = isSVG && prop.indexOf(':') > -1 && SVGNamespace[prop.split(':')[0]]
176
+ if (ns) setAttributeNS(node, ns, prop, value)
177
+ else setAttribute(node, Aliases[prop] || prop, value)
169
178
  }
179
+ return value;
180
+ }
170
181
 
171
- function insertExpression(
172
- parent: Element,
173
- value: RxValue,
174
- current?: RxValue|Value[],
175
- marker?: Node,
176
- unwrapArray?: boolean
177
- ): Value|{():Value} {
178
- while (typeof current === 'function') current = current();
179
- if (value === current) return current;
180
- const t = typeof value,
181
- multi = marker !== undefined;
182
- parent = (multi && current[0] && current[0].parentNode) || parent;
183
-
184
- if (t === "string" || t === "number") {
185
- if (t === "number") {
186
- value = value.toString();
187
- if (value === current) return current;
188
- }
189
- if (multi) {
190
- let node = current[0];
191
- if (node && node.nodeType === 3) {
192
- node.data !== value && (node.data = value);
193
- } else node = document.createTextNode(value);
194
- current = cleanChildren(parent, current, marker, node);
195
- } else {
196
- if (current !== "" && typeof current === 'string') {
197
- current = (parent.firstChild as Text).data = value;
198
- } else current = parent.textContent = value;
199
- }
200
- } else if (value == null || t === 'boolean') {
201
- current = cleanChildren(parent, current, marker);
202
- } else if (t === 'function') {
203
- effect(() => {
204
- let v = value();
205
- while (typeof v === 'function') v = v();
206
- current = insertExpression(parent, v, current, marker);
207
- });
182
+ function insertExpression(
183
+ parent: Element,
184
+ value: RxValue,
185
+ current?: RxValue|Value[],
186
+ marker?: Node,
187
+ unwrapArray?: boolean
188
+ ): Value|{():Value} {
189
+ while (typeof current === 'function') current = current();
190
+ if (value === current) return current;
191
+ const t = typeof value,
192
+ multi = marker !== undefined;
193
+ parent = (multi && current[0] && current[0].parentNode) || parent;
194
+
195
+ if (t === "string" || t === "number") {
196
+ if (t === "number") {
197
+ value = value.toString();
198
+ if (value === current) return current;
199
+ }
200
+ if (multi) {
201
+ let node = current[0];
202
+ if (node && node.nodeType === 3) {
203
+ node.data !== value && (node.data = value);
204
+ } else node = globalThis.document.createTextNode(value);
205
+ current = cleanChildren(parent, current, marker, node);
206
+ } else {
207
+ if (current !== "" && typeof current === 'string') {
208
+ current = (parent.firstChild as Text).data = value;
209
+ } else current = parent.textContent = value;
210
+ }
211
+ } else if (value == null || t === 'boolean') {
212
+ current = cleanChildren(parent, current, marker);
213
+ } else if (t === 'function') {
214
+ effect(() => {
215
+ let v = value();
216
+ while (typeof v === 'function') v = v();
217
+ current = insertExpression(parent, v, current, marker);
218
+ });
219
+ return () => current;
220
+ } else if (isArray(value)) {
221
+ const array:Node[] = [];
222
+ const currentArray = current && isArray(current);
223
+ if (normalizeIncomingArray(array, value, current, unwrapArray)) {
224
+ effect(() => (current = insertExpression(parent, array, current, marker, true)));
208
225
  return () => current;
209
- } else if (isArray(value)) {
210
- const array:Node[] = [];
211
- const currentArray = current && isArray(current);
212
- if (normalizeIncomingArray(array, value, current, unwrapArray)) {
213
- effect(() => (current = insertExpression(parent, array, current, marker, true)));
214
- return () => current;
215
- }
216
- if (array.length === 0) {
217
- current = cleanChildren(parent, current, marker);
218
- if (multi) return current;
219
- } else if (currentArray) {
220
- if (current.length === 0) {
221
- appendNodes(parent, array, marker);
222
- } else reconcileArrays(parent, current, array);
223
- } else {
224
- current && cleanChildren(parent);
225
- appendNodes(parent, array);
226
- }
227
- current = array;
228
- } else if (value.nodeType) {
229
- if (isArray(current)) {
230
- if (multi) return (current = cleanChildren(parent, current, marker, value));
231
- cleanChildren(parent, current, null, value);
232
- } else if (current == null || current === "" || !parent.firstChild) {
233
- parent.appendChild(value);
234
- } else parent.replaceChild(value, parent.firstChild);
235
- current = value;
236
- } else console.warn(`Unrecognized value. Skipped inserting`, value);
237
- return current;
238
- }
226
+ }
227
+ if (array.length === 0) {
228
+ current = cleanChildren(parent, current, marker);
229
+ if (multi) return current;
230
+ } else if (currentArray) {
231
+ if (current.length === 0) {
232
+ appendNodes(parent, array, marker);
233
+ } else reconcileArrays(parent, current, array);
234
+ } else {
235
+ current && cleanChildren(parent);
236
+ appendNodes(parent, array);
237
+ }
238
+ current = array;
239
+ } else if (value.nodeType) {
240
+ if (isArray(current)) {
241
+ if (multi) return (current = cleanChildren(parent, current, marker, value));
242
+ cleanChildren(parent, current, null, value);
243
+ } else if (current == null || current === "" || !parent.firstChild) {
244
+ parent.appendChild(value);
245
+ } else parent.replaceChild(value, parent.firstChild);
246
+ current = value;
247
+ } else console.warn(`Unrecognized value. Skipped inserting`, value);
248
+ return current;
249
+ }
239
250
 
240
- function normalizeIncomingArray(normalized:Node[], array:Node[], current:Node[], unwrap?:boolean): boolean {
241
- let dynamic = false;
242
- for (let i = 0, len = array.length; i < len; i++) {
243
- let item = array[i]
244
- const prev = current && current[normalized.length];
245
- // if (item == null || item === true || item === false) {
246
- // // matches null, undefined, true or false skip
247
- // } else
248
- if (typeof item === 'object' && item.nodeType) {
249
- normalized.push(item);
250
- } else if (isArray(item)) {
251
- dynamic = normalizeIncomingArray(normalized, item, prev) || dynamic;
252
- } else if (item.call) {
253
- if (unwrap) {
254
- while (typeof item === 'function') item = item();
255
- dynamic = normalizeIncomingArray(
256
- normalized,
257
- isArray(item) ? item : [item],
258
- isArray(prev) ? prev : [prev]
259
- ) || dynamic;
260
- } else {
261
- normalized.push(item);
262
- dynamic = true;
263
- }
251
+ function normalizeIncomingArray(normalized:Node[], array:Node[], current:Node[], unwrap?:boolean): boolean {
252
+ let dynamic = false;
253
+ for (let i = 0, len = array.length; i < len; i++) {
254
+ let item = array[i]
255
+ const prev = current && current[normalized.length];
256
+ // if (item == null || item === true || item === false) {
257
+ // // matches null, undefined, true or false skip
258
+ // } else
259
+ if (typeof item === 'object' && item.nodeType) {
260
+ normalized.push(item);
261
+ } else if (isArray(item)) {
262
+ dynamic = normalizeIncomingArray(normalized, item, prev) || dynamic;
263
+ } else if (item.call) {
264
+ if (unwrap) {
265
+ while (typeof item === 'function') item = item();
266
+ dynamic = normalizeIncomingArray(
267
+ normalized,
268
+ isArray(item) ? item : [item],
269
+ isArray(prev) ? prev : [prev]
270
+ ) || dynamic;
264
271
  } else {
265
- const value = String(item);
266
- if (prev && prev.nodeType === 3 && prev.data === value) normalized.push(prev);
267
- else normalized.push(document.createTextNode(value));
272
+ normalized.push(item);
273
+ dynamic = true;
268
274
  }
275
+ } else {
276
+ const value = String(item);
277
+ if (prev && prev.nodeType === 3 && prev.data === value) normalized.push(prev);
278
+ else normalized.push(globalThis.document.createTextNode(value));
269
279
  }
270
- return dynamic;
271
- }
272
-
273
- function cleanChildren(
274
- parent: Element,
275
- current?: Node[],
276
- marker?: Node|null,
277
- replacement?: boolean
278
- ): string | Node[] {
279
- if (marker === undefined) return (parent.textContent = '');
280
- const node = replacement || document.createTextNode('');
281
- if (current.length) {
282
- let inserted = false;
283
- for (let i = current.length - 1; i >= 0; i--) {
284
- const el = current[i];
285
- if (node !== el) {
286
- const isParent = el.parentNode === parent;
287
- if (!inserted && !i)
288
- isParent ? parent.replaceChild(node, el) : parent.insertBefore(node, marker);
289
- else isParent && el.remove();
290
- } else inserted = true;
291
- }
292
- } else parent.insertBefore(node, marker);
293
- return [node];
294
280
  }
281
+ return dynamic;
282
+ }
295
283
 
296
- function clearDelegatedEvents() {
297
- if (globalThis[$$EVENTS]) {
298
- for (const name of globalThis[$$EVENTS].keys()) document.removeEventListener(name, eventHandler);
299
- delete globalThis[$$EVENTS];
284
+ function cleanChildren(
285
+ parent: Element,
286
+ current?: Node[],
287
+ marker?: Node|null,
288
+ replacement?: boolean
289
+ ): string | Node[] {
290
+ if (marker === undefined) return (parent.textContent = '');
291
+ const node = replacement || globalThis.document.createTextNode('');
292
+ if (current.length) {
293
+ let inserted = false;
294
+ for (let i = current.length - 1; i >= 0; i--) {
295
+ const el = current[i];
296
+ if (node !== el) {
297
+ const isParent = el.parentNode === parent;
298
+ if (!inserted && !i)
299
+ isParent ? parent.replaceChild(node, el) : parent.insertBefore(node, marker);
300
+ else isParent && el.remove();
301
+ } else inserted = true;
300
302
  }
301
- }
303
+ } else parent.insertBefore(node, marker);
304
+ return [node];
305
+ }
302
306
 
303
- return {render,component,insert,spread,assign,element,text,isChild,clearDelegatedEvents}
307
+ function clearDelegatedEvents() {
308
+ if (globalThis[$$EVENTS]) {
309
+ for (const name of globalThis[$$EVENTS].keys()) globalThis.document.removeEventListener(name, eventHandler);
310
+ delete globalThis[$$EVENTS];
311
+ }
304
312
  }
305
313
 
314
+ export {render,component,insert,spread,assign,element,text,isChild,clearDelegatedEvents}
315
+
306
316
  const $$EVENTS = "_$DX_DELEGATE"
307
317
 
308
318
  function delegateEvents(eventNames:string[], document:Document) {
@@ -322,7 +332,7 @@ function eventHandler(e: Event) {
322
332
  // reverse Shadow DOM retargetting
323
333
  if (e.target !== node) Object.defineProperty(e, 'target', {configurable: true, value: node})
324
334
  // simulate currentTarget
325
- Object.defineProperty(e, 'currentTarget', {configurable: true, get() {return node || document}})
335
+ Object.defineProperty(e, 'currentTarget', {configurable: true, get() {return node || globalThis.document}})
326
336
  while (node) {
327
337
  const handler = node[key];
328
338
  if (handler && !node.disabled) {
@@ -418,78 +428,3 @@ function appendNodes(parent:Node, array:Node[], marker:null|Node = null) {
418
428
  for (let i = 0, len = array.length; i < len; i++)
419
429
  parent.insertBefore(array[i], marker)
420
430
  }
421
-
422
- // Slightly modified version of: https://github.com/WebReflection/udomdiff/blob/master/index.js
423
- function reconcileArrays(parentNode:Node, a:Element[], b:Element[]) {
424
- const bLength = b.length
425
- let aEnd = a.length,
426
- bEnd = bLength,
427
- aStart = 0,
428
- bStart = 0,
429
- map:Map<Node,number>|null = null;
430
- const after = a[aEnd - 1].nextSibling
431
-
432
- while (aStart < aEnd || bStart < bEnd) {
433
- // common prefix
434
- if (a[aStart] === b[bStart]) {
435
- aStart++;
436
- bStart++;
437
- continue;
438
- }
439
- // common suffix
440
- while (a[aEnd - 1] === b[bEnd - 1]) {
441
- aEnd--;
442
- bEnd--;
443
- }
444
- // append
445
- if (aEnd === aStart) {
446
- const node =
447
- bEnd < bLength
448
- ? bStart
449
- ? b[bStart - 1].nextSibling
450
- : b[bEnd - bStart]
451
- : after;
452
-
453
- while (bStart < bEnd) parentNode.insertBefore(b[bStart++], node);
454
- // remove
455
- } else if (bEnd === bStart) {
456
- while (aStart < aEnd) {
457
- if (!map || !map.has(a[aStart])) a[aStart].remove();
458
- aStart++;
459
- }
460
- // swap backward
461
- } else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
462
- const node = a[--aEnd].nextSibling;
463
- parentNode.insertBefore(b[bStart++], a[aStart++].nextSibling);
464
- parentNode.insertBefore(b[--bEnd], node);
465
-
466
- a[aEnd] = b[bEnd];
467
- // fallback to map
468
- } else {
469
- if (!map) {
470
- map = new Map();
471
- let i = bStart;
472
- while (i < bEnd) map.set(b[i], i++);
473
- }
474
-
475
- const index = map.get(a[aStart]);
476
- if (index != null) {
477
- if (bStart < index && index < bEnd) {
478
- let i = aStart,
479
- sequence = 1,
480
- t:number|undefined;
481
-
482
- while (++i < aEnd && i < bEnd) {
483
- if ((t = map.get(a[i])) == null || t !== index + sequence) break;
484
- sequence++;
485
- }
486
-
487
- if (sequence > index - bStart) {
488
- const node = a[aStart];
489
- while (bStart < index) parentNode.insertBefore(b[bStart++], node);
490
- } else parentNode.replaceChild(b[bStart++], a[aStart++]);
491
- } else aStart++;
492
- } else a[aStart++].remove();
493
- }
494
- }
495
- }
package/src/select.js ADDED
@@ -0,0 +1 @@
1
+
package/src/show.ts ADDED
@@ -0,0 +1,48 @@
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2016-2025 Ryan Carniato
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ import type { Child } from './hyperscript.ts'
26
+ import {untrack, memo} from './reactive.ts'
27
+
28
+ export type ShowProps<T> = {
29
+ when: T,
30
+ children: Child | ((a:()=>T)=>void),
31
+ fallback: unknown
32
+ }
33
+
34
+ /**
35
+ Show children if `when` prop is true, otherwise show `fallback`.
36
+ @group Components
37
+ */
38
+ export function Show<T>(props:ShowProps<T>) {
39
+ const condition = memo(()=>props.when)
40
+ return memo(()=>{
41
+ const c = condition()
42
+ if (c) {
43
+ const child = props.children
44
+ const fn = typeof child === "function" && child.length > 0
45
+ return fn ? untrack(() => child(() => props.when)) : child
46
+ } else return props.fallback
47
+ })
48
+ }
package/src/svg.js ADDED
File without changes