essor 0.0.3-beta.1 → 0.0.4

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/dist/index.cjs.js CHANGED
@@ -1,803 +1,24 @@
1
1
  'use strict';
2
2
 
3
- // src/signal/signal.ts
4
- var activeEffect = null;
5
- var activeComputed = null;
6
- var computedSet = /* @__PURE__ */ new Set();
7
- var targetMap = /* @__PURE__ */ new WeakMap();
8
- var EffectDeps = /* @__PURE__ */ new Set();
9
- function track(target, key) {
10
- let depsMap = targetMap.get(target);
11
- if (!depsMap) {
12
- depsMap = /* @__PURE__ */ new Map();
13
- targetMap.set(target, depsMap);
14
- }
15
- let dep = depsMap.get(key);
16
- if (!dep) {
17
- dep = /* @__PURE__ */ new Set();
18
- depsMap.set(key, dep);
19
- }
20
- if (activeEffect)
21
- dep.add(activeEffect);
22
- if (activeComputed) {
23
- computedSet.add(activeComputed);
24
- }
25
- }
26
- function trigger(target, key) {
27
- if (computedSet.size > 0) {
28
- computedSet.forEach((computedSignal) => computedSignal.run());
29
- }
30
- const depsMap = targetMap.get(target);
31
- if (!depsMap) {
32
- return;
33
- }
34
- const dep = depsMap.get(key);
35
- if (dep) {
36
- dep.forEach((effect2) => EffectDeps.has(effect2) && effect2());
37
- }
38
- }
39
- var Signal = class {
40
- constructor(value) {
41
- this._value = value;
42
- }
43
- valueOf() {
44
- track(this, "value");
45
- return this._value;
46
- }
47
- toString() {
48
- track(this, "value");
49
- return String(this._value);
50
- }
51
- toJSON() {
52
- return this._value;
53
- }
54
- get value() {
55
- track(this, "value");
56
- return this._value;
57
- }
58
- set value(newValue) {
59
- if (this._value !== newValue) {
60
- this._value = newValue;
61
- trigger(this, "value");
62
- }
63
- }
64
- peek() {
65
- return this._value;
66
- }
67
- update() {
68
- trigger(this, "value");
69
- }
70
- };
71
- function signal(value) {
72
- if (isSignal(value)) {
73
- return value;
74
- }
75
- return new Signal(value);
76
- }
77
- function isSignal(value) {
78
- return value instanceof Signal;
79
- }
80
- var Computed = class {
81
- constructor(fn) {
82
- this.fn = fn;
83
- const prev = activeComputed;
84
- activeComputed = this;
85
- track(this, "_value");
86
- this._value = this.fn();
87
- activeComputed = prev;
88
- }
89
- peek() {
90
- return this._value;
91
- }
92
- run() {
93
- const newValue = this.fn();
94
- if (newValue !== this._value) {
95
- this._value = newValue;
96
- }
97
- }
98
- get value() {
99
- track(this, "_value");
100
- return this._value;
101
- }
102
- };
103
- function computed(fn) {
104
- return new Computed(fn);
105
- }
106
- function effect(fn) {
107
- function effectFn() {
108
- const prev = activeEffect;
109
- activeEffect = effectFn;
110
- fn();
111
- activeEffect = prev;
112
- }
113
- EffectDeps.add(effectFn);
114
- effectFn();
115
- return () => {
116
- EffectDeps.delete(effectFn);
117
- activeEffect = null;
118
- };
119
- }
120
- function createSignal(value) {
121
- const signal2 = new Signal(value);
122
- return [
123
- signal2.value,
124
- (newValue) => {
125
- signal2.value = newValue;
126
- }
127
- ];
128
- }
129
- function toSignalObject(initialValues) {
130
- const signals = Object.entries(initialValues).reduce((acc, [key, value]) => {
131
- acc[key] = isSignal(value) ? value : signal(value);
132
- return acc;
133
- }, {});
134
- return signals;
135
- }
136
-
137
- // src/signal/store.ts
138
- var _id = 0;
139
- var StoreMap = /* @__PURE__ */ new Map();
140
- function createOptionsStore(options) {
141
- const { state, getters, actions } = options;
142
- const signalState = toSignalObject(state != null ? state : {});
143
- const states = {
144
- _id: `store_${_id}`
145
- };
146
- for (const key in getters) {
147
- const getter = getters[key];
148
- if (getter) {
149
- states[key] = computed(() => {
150
- return getter.call(signalState);
151
- });
152
- }
153
- }
154
- for (const key in actions) {
155
- const action = actions[key];
156
- if (action) {
157
- states[key] = action.bind(signalState);
158
- }
159
- }
160
- StoreMap.set(_id, signal);
161
- ++_id;
162
- return new Proxy(
163
- {},
164
- {
165
- get(_, key) {
166
- if (key in states) {
167
- return states[key];
168
- }
169
- return signalState[key].value;
170
- }
171
- }
172
- );
173
- }
174
- function createStore(options) {
175
- return function() {
176
- if (StoreMap.has(_id)) {
177
- return StoreMap.get(_id);
178
- }
179
- return createOptionsStore(options);
180
- };
181
- }
182
-
183
- // src/utils/comm.ts
184
- function coerceArray(data) {
185
- return Array.isArray(data) ? data.flat() : [data];
186
- }
187
-
188
- // src/utils/is.ts
189
- var isArray = Array.isArray;
190
- function isNil(x) {
191
- return x === null || x === void 0;
192
- }
193
- var isFunction = (val) => typeof val === "function";
194
- function isFalsy(x) {
195
- return x === false || x === null || x === void 0 || x === "";
196
- }
197
-
198
- // src/utils/string.ts
199
- var kebabCase = (string) => {
200
- return string.replaceAll(/[A-Z]+/g, (match, offset) => {
201
- return `${offset > 0 ? "-" : ""}${match.toLocaleLowerCase()}`;
202
- });
203
- };
204
-
205
- // src/template/utils.ts
206
- function coerceNode(data) {
207
- if (isJsxElement(data) || data instanceof Node) {
208
- return data;
209
- }
210
- const text = isFalsy(data) ? "" : String(data);
211
- return document.createTextNode(text);
212
- }
213
- function insertChild(parent, child, before = null) {
214
- const beforeNode = isJsxElement(before) ? before.firstChild : before;
215
- if (isJsxElement(child)) {
216
- child.mount(parent, beforeNode);
217
- } else if (beforeNode) {
218
- beforeNode.before(child);
219
- } else {
220
- parent.append(child);
221
- }
222
- }
223
- function removeChild(child) {
224
- if (isJsxElement(child)) {
225
- child.unmount();
226
- } else {
227
- const parent = child.parentNode;
228
- if (parent) {
229
- child.remove();
230
- }
231
- }
232
- }
233
- function replaceChild(parent, node, child) {
234
- insertChild(parent, node, child);
235
- removeChild(child);
236
- }
237
- function setAttribute(element, attr, value) {
238
- if (attr === "class") {
239
- if (typeof value === "string") {
240
- element.className = value;
241
- } else if (Array.isArray(value)) {
242
- element.className = value.join(" ");
243
- } else if (value && typeof value === "object") {
244
- element.className = Object.entries(value).reduce((acc, [key, value2]) => acc + (value2 ? ` ${key}` : ""), "").trim();
245
- }
246
- return;
247
- }
248
- if (attr === "style") {
249
- if (typeof value === "string") {
250
- element.style.cssText = value;
251
- } else if (value && typeof value === "object") {
252
- const obj = value;
253
- Object.keys(obj).forEach((key) => {
254
- element.style.setProperty(kebabCase(key), String(obj[key]));
255
- });
256
- }
257
- return;
258
- }
259
- if (isFalsy(value)) {
260
- element.removeAttribute(attr);
261
- } else if (value === true) {
262
- element.setAttribute(attr, "");
263
- } else {
264
- element.setAttribute(attr, String(value));
265
- }
266
- }
267
- function binNode(node, setter) {
268
- if (node instanceof HTMLInputElement) {
269
- if (node.type === "checkbox") {
270
- return addEventListener(node, "change", () => {
271
- setter(Boolean(node.checked));
272
- });
273
- }
274
- if (node.type === "date") {
275
- return addEventListener(node, "change", () => {
276
- setter(node.value ? node.value : "");
277
- });
278
- }
279
- if (node.type === "file") {
280
- return addEventListener(node, "change", () => {
281
- if (node.files) {
282
- setter(node.files);
283
- }
284
- });
285
- }
286
- if (node.type === "number") {
287
- return addEventListener(node, "input", () => {
288
- const value = Number.parseFloat(node.value);
289
- setter(Number.isNaN(value) ? "" : String(value));
290
- });
291
- }
292
- if (node.type === "radio") {
293
- return addEventListener(node, "change", () => {
294
- setter(node.checked ? node.value : "");
295
- });
296
- }
297
- if (node.type === "text") {
298
- return addEventListener(node, "input", () => {
299
- setter(node.value);
300
- });
301
- }
302
- }
303
- if (node instanceof HTMLSelectElement) {
304
- return addEventListener(node, "change", () => {
305
- setter(node.value);
306
- });
307
- }
308
- if (node instanceof HTMLTextAreaElement) {
309
- return addEventListener(node, "input", () => {
310
- setter(node.value);
311
- });
312
- }
313
- }
314
- var p = Promise.resolve();
315
- function nextTick(fn) {
316
- return fn ? p.then(fn) : p;
317
- }
318
- function addEventListener(node, eventName, handler) {
319
- node.addEventListener(eventName, handler);
320
- return () => node.removeEventListener(eventName, handler);
321
- }
322
-
323
- // src/template/component-node.ts
324
- var _ComponentNode = class _ComponentNode {
325
- constructor(template2, props, key) {
326
- this.template = template2;
327
- this.props = props;
328
- this.key = key;
329
- this.proxyProps = {};
330
- this.context = {};
331
- this.emitter = /* @__PURE__ */ new Set();
332
- this.mounted = false;
333
- this.rootNode = null;
334
- this.hooks = {
335
- mounted: /* @__PURE__ */ new Set(),
336
- destroy: /* @__PURE__ */ new Set()
337
- };
338
- this.trackMap = /* @__PURE__ */ new Map();
339
- this.proxyProps = toSignalObject(props);
340
- }
341
- addEventListener() {
342
- }
343
- removeEventListener() {
344
- }
345
- get firstChild() {
346
- var _a, _b;
347
- return (_b = (_a = this.rootNode) == null ? void 0 : _a.firstChild) != null ? _b : null;
348
- }
349
- get isConnected() {
350
- var _a, _b;
351
- return (_b = (_a = this.rootNode) == null ? void 0 : _a.isConnected) != null ? _b : false;
352
- }
353
- addHook(hook, cb) {
354
- var _a;
355
- (_a = this.hooks[hook]) == null ? void 0 : _a.add(cb);
356
- }
357
- getContext(context) {
358
- return _ComponentNode.context[context];
359
- }
360
- setContext(context, value) {
361
- _ComponentNode.context[context] = value;
362
- }
363
- inheritNode(node) {
364
- this.context = node.context;
365
- this.hooks = node.hooks;
366
- Object.assign(this.proxyProps, node.proxyProps);
367
- this.rootNode = node.rootNode;
368
- this.trackMap = node.trackMap;
369
- const props = this.props;
370
- this.props = node.props;
371
- this.patchProps(props);
372
- }
373
- unmount() {
374
- var _a;
375
- this.hooks.destroy.forEach((handler) => handler());
376
- Object.values(this.hooks).forEach((set) => set.clear());
377
- (_a = this.rootNode) == null ? void 0 : _a.unmount();
378
- this.rootNode = null;
379
- this.proxyProps = {};
380
- this.mounted = false;
381
- this.emitter.forEach((emitter) => emitter());
382
- _ComponentNode.context = {};
383
- }
384
- mount(parent, before) {
385
- var _a, _b, _c, _d;
386
- if (!isFunction(this.template)) {
387
- throw new Error("Template must be a function");
388
- }
389
- if (this.isConnected) {
390
- return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
391
- }
392
- _ComponentNode.ref = this;
393
- this.rootNode = this.template(this.proxyProps);
394
- _ComponentNode.ref = null;
395
- this.mounted = true;
396
- const mountedNode = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
397
- this.hooks.mounted.forEach((handler) => handler());
398
- this.patchProps(this.props);
399
- return mountedNode;
400
- }
401
- getNodeTrack(trackKey, suppressCleanupCall) {
402
- let track2 = this.trackMap.get(trackKey);
403
- if (!track2) {
404
- track2 = { cleanup: () => {
405
- } };
406
- this.trackMap.set(trackKey, track2);
407
- }
408
- if (!suppressCleanupCall) {
409
- track2.cleanup();
410
- }
411
- return track2;
412
- }
413
- patchProps(props) {
414
- var _a, _b, _c, _d, _e;
415
- for (const [key, prop] of Object.entries(props)) {
416
- if (key.indexOf("on") === 0 && ((_a = this.rootNode) == null ? void 0 : _a.nodes)) {
417
- const event = key.slice(2).toLowerCase();
418
- const listener = prop;
419
- const cleanup = addEventListener(this.rootNode.nodes[0], event, listener);
420
- this.emitter.add(cleanup);
421
- } else if (key.indexOf("bind:") === 0) {
422
- this.proxyProps[key] = signal(prop);
423
- } else if (key === "ref") {
424
- isFunction(prop) ? prop((_b = this.rootNode) == null ? void 0 : _b.nodes[0]) : props[key] = (_c = this.rootNode) == null ? void 0 : _c.nodes[0];
425
- } else {
426
- const newValue = (_e = (_d = this.proxyProps)[key]) != null ? _e : _d[key] = signal(prop);
427
- const track2 = this.getNodeTrack(key);
428
- track2.cleanup = effect(() => {
429
- newValue.value = prop;
430
- });
431
- }
432
- }
433
- this.props = props;
434
- }
435
- };
436
- _ComponentNode.ref = null;
437
- _ComponentNode.context = {};
438
- var ComponentNode = _ComponentNode;
439
-
440
- // src/template/patch.ts
441
- function patchChildren(parent, childrenMap, nextChildren, before) {
442
- const result = /* @__PURE__ */ new Map();
443
- const children = childrenMap.values();
444
- const parentChildNodesLength = parent.childNodes.length;
445
- if (childrenMap.size > 0 && nextChildren.length === 0) {
446
- if (parentChildNodesLength === childrenMap.size + (before ? 1 : 0)) {
447
- const parentElement = parent;
448
- parentElement.innerHTML = "";
449
- if (before) {
450
- insertChild(parent, before);
451
- }
452
- } else {
453
- const range = document.createRange();
454
- const child = children.next().value;
455
- const start = isJsxElement(child) ? child.firstChild : child;
456
- range.setStartBefore(start);
457
- if (before) {
458
- range.setEndBefore(before);
459
- } else {
460
- range.setEndAfter(parent);
461
- }
462
- range.deleteContents();
463
- }
464
- childrenMap.forEach((node) => {
465
- if (isJsxElement(node)) {
466
- node.unmount();
467
- }
468
- });
469
- return result;
470
- }
471
- const replaces = [];
472
- const nextChildrenMap = mapKeys(nextChildren);
473
- for (let [i, child] of nextChildren.entries()) {
474
- let currChild = children.next().value;
475
- let currKey = getKey(currChild, i);
476
- while (currChild && !nextChildrenMap.has(currKey)) {
477
- removeChild(currChild);
478
- childrenMap.delete(currKey);
479
- currChild = children.next().value;
480
- currKey = getKey(currChild, i);
481
- }
482
- const key = getKey(child, i);
483
- const origChild = childrenMap.get(key);
484
- if (origChild) {
485
- child = patch(parent, origChild, child);
486
- }
487
- if (currChild) {
488
- if (currChild) {
489
- const placeholder = document.createComment("");
490
- insertChild(parent, placeholder, currChild);
491
- replaces.push([placeholder, child]);
492
- } else {
493
- insertChild(parent, child, before);
494
- }
495
- } else {
496
- insertChild(parent, child, before);
497
- }
498
- result.set(key, child);
499
- }
500
- replaces.forEach(([placeholder, child]) => replaceChild(parent, child, placeholder));
501
- childrenMap.forEach((child, key) => {
502
- if (child.isConnected && !result.has(key)) {
503
- removeChild(child);
504
- }
505
- });
506
- return result;
507
- }
508
- function patch(parent, node, next) {
509
- if (node === next) {
510
- return node;
511
- }
512
- if (isJsxElement(node) && isJsxElement(next) && node.template === next.template) {
513
- next.inheritNode(node);
514
- return next;
515
- }
516
- if (node instanceof Text && next instanceof Text) {
517
- if (node.textContent !== next.textContent) {
518
- node.textContent = next.textContent;
519
- }
520
- return node;
521
- }
522
- replaceChild(parent, next, node);
523
- return next;
524
- }
525
- function mapKeys(children) {
526
- const result = /* @__PURE__ */ new Map();
527
- for (const [i, child] of children.entries()) {
528
- const key = getKey(child, i);
529
- result.set(key, child);
530
- }
531
- return result;
532
- }
533
- function getKey(node, index) {
534
- const id = node instanceof Element ? node.id : void 0;
535
- const result = id === "" ? void 0 : id;
536
- return result != null ? result : `_$${index}$`;
537
- }
538
-
539
- // src/template/template-node.ts
540
- var TemplateNode = class _TemplateNode {
541
- constructor(template2, props, key) {
542
- this.template = template2;
543
- this.props = props;
544
- this.key = key;
545
- this.treeMap = /* @__PURE__ */ new Map();
546
- this.mounted = false;
547
- this.nodes = [];
548
- this.provides = {};
549
- this.trackMap = /* @__PURE__ */ new Map();
550
- this.parent = null;
551
- }
552
- get firstChild() {
553
- var _a;
554
- return (_a = this.nodes[0]) != null ? _a : null;
555
- }
556
- get isConnected() {
557
- return this.mounted;
558
- }
559
- addEventListener() {
560
- }
561
- removeEventListener() {
562
- }
563
- unmount() {
564
- this.trackMap.forEach((track2) => {
565
- var _a, _b;
566
- (_a = track2.cleanup) == null ? void 0 : _a.call(track2);
567
- (_b = track2.lastNodes) == null ? void 0 : _b.forEach((node) => {
568
- if (track2.isRoot) {
569
- removeChild(node);
570
- } else if (node instanceof _TemplateNode) {
571
- node.unmount();
572
- }
573
- });
574
- });
575
- this.trackMap.clear();
576
- this.treeMap.clear();
577
- this.nodes.forEach((node) => removeChild(node));
578
- this.nodes = [];
579
- this.mounted = false;
580
- }
581
- mount(parent, before) {
582
- var _a;
583
- this.parent = parent;
584
- if (this.isConnected) {
585
- this.nodes.forEach((node) => insertChild(parent, node, before));
586
- return this.nodes;
587
- }
588
- const cloneNode = this.template.content.cloneNode(true);
589
- const firstChild = cloneNode.firstChild;
590
- if ((_a = firstChild == null ? void 0 : firstChild.hasAttribute) == null ? void 0 : _a.call(firstChild, "_svg_")) {
591
- firstChild.remove();
592
- firstChild == null ? void 0 : firstChild.childNodes.forEach((node) => {
593
- cloneNode.append(node);
594
- });
595
- }
596
- this.nodes = Array.from(cloneNode.childNodes);
597
- this.mapNodeTree(parent, cloneNode);
598
- insertChild(parent, cloneNode, before);
599
- this.patchNodes(this.props);
600
- this.mounted = true;
601
- return this.nodes;
602
- }
603
- mapNodeTree(parent, tree) {
604
- let index = 1;
605
- this.treeMap.set(0, parent);
606
- const walk = (node) => {
607
- if (node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
608
- this.treeMap.set(index++, node);
609
- }
610
- let child = node.firstChild;
611
- while (child) {
612
- walk(child);
613
- child = child.nextSibling;
614
- }
615
- };
616
- walk(tree);
617
- }
618
- patchNodes(props) {
619
- for (const key in props) {
620
- const index = Number(key);
621
- const node = this.treeMap.get(index);
622
- if (node) {
623
- const value = this.props[key];
624
- this.patchNode(key, node, value, index === 0);
625
- }
626
- }
627
- this.props = props;
628
- }
629
- getNodeTrack(trackKey, trackLastNodes, isRoot) {
630
- var _a;
631
- let track2 = this.trackMap.get(trackKey);
632
- if (!track2) {
633
- track2 = { cleanup: () => {
634
- } };
635
- if (trackLastNodes) {
636
- track2.lastNodes = /* @__PURE__ */ new Map();
637
- }
638
- if (isRoot) {
639
- track2.isRoot = true;
640
- }
641
- this.trackMap.set(trackKey, track2);
642
- }
643
- (_a = track2.cleanup) == null ? void 0 : _a.call(track2);
644
- return track2;
645
- }
646
- inheritNode(node) {
647
- this.mounted = node.mounted;
648
- this.nodes = node.nodes;
649
- this.trackMap = node.trackMap;
650
- this.treeMap = node.treeMap;
651
- const props = this.props;
652
- this.props = node.props;
653
- this.patchNodes(props);
654
- }
655
- patchNode(key, node, props, isRoot) {
656
- for (const attr in props) {
657
- if (attr === "children" && props.children) {
658
- if (!isArray(props.children)) {
659
- const trackKey = `${key}:${attr}:${0}`;
660
- const track2 = this.getNodeTrack(trackKey, true, isRoot);
661
- patchChild(track2, node, props.children, null);
662
- } else {
663
- props.children.forEach(([child, path], index) => {
664
- var _a;
665
- const before = isNil(path) ? null : (_a = this.treeMap.get(path)) != null ? _a : null;
666
- const trackKey = `${key}:${attr}:${index}`;
667
- const track2 = this.getNodeTrack(trackKey, true, isRoot);
668
- patchChild(track2, node, child, before);
669
- });
670
- }
671
- } else if (attr === "ref") {
672
- if (isFunction(props[attr])) {
673
- props[attr](node);
674
- } else {
675
- props[attr] = node;
676
- }
677
- } else if (attr.indexOf("on") === 0) {
678
- const eventName = attr.slice(2).toLocaleLowerCase();
679
- const track2 = this.getNodeTrack(`${key}:${attr}`);
680
- const listener = props[attr];
681
- track2.cleanup = addEventListener(node, eventName, listener);
682
- } else if (attr.indexOf("bind:") === 0) {
683
- const bindKey = attr.slice(5).toLocaleLowerCase();
684
- const val = props[attr];
685
- const track2 = this.getNodeTrack(`${key}:${attr}`);
686
- const triggerValue = isSignal(val) ? val : signal(val);
687
- const cleanup = effect(() => {
688
- triggerValue.value = val;
689
- node[bindKey] = triggerValue.value;
690
- });
691
- const cleanupBind = binNode(node, (value) => {
692
- props[`update:${bindKey}`](value);
693
- });
694
- track2.cleanup = () => {
695
- cleanup == null ? void 0 : cleanup();
696
- cleanupBind == null ? void 0 : cleanupBind();
697
- };
698
- } else if (attr.indexOf("update:") !== 0) {
699
- const track2 = this.getNodeTrack(`${key}:${attr}`);
700
- const val = props[attr];
701
- const triggerValue = isSignal(val) ? val : signal(val);
702
- const cleanup = effect(() => {
703
- triggerValue.value = val;
704
- patchAttribute(track2, node, attr, triggerValue.value);
705
- });
706
- track2.cleanup = () => {
707
- cleanup == null ? void 0 : cleanup();
708
- };
709
- }
710
- }
711
- }
712
- };
713
- function patchAttribute(track2, node, attr, data) {
714
- const element = node;
715
- if (!element.setAttribute) {
716
- return;
717
- }
718
- if (isFunction(data)) {
719
- track2.cleanup = effect(() => {
720
- setAttribute(element, attr, data());
721
- });
722
- } else {
723
- setAttribute(element, attr, data);
724
- }
725
- }
726
- function patchChild(track2, parent, child, before) {
727
- if (isFunction(child)) {
728
- track2.cleanup = effect(() => {
729
- const nextNodes = coerceArray(child()).map(coerceNode);
730
- track2.lastNodes = patchChildren(parent, track2.lastNodes, nextNodes, before);
731
- });
732
- } else {
733
- coerceArray(child).forEach((node, i) => {
734
- const newNode = coerceNode(node);
735
- track2.lastNodes.set(String(i), newNode);
736
- insertChild(parent, newNode, before);
737
- });
738
- }
739
- }
740
-
741
- // src/template/template.ts
742
- function h(template2, props, key) {
743
- return isFunction(template2) ? new ComponentNode(template2, props, key) : new TemplateNode(template2, props, key);
744
- }
745
- function isJsxElement(node) {
746
- return node instanceof ComponentNode || node instanceof TemplateNode;
747
- }
748
- function template(html) {
749
- const template2 = document.createElement("template");
750
- template2.innerHTML = html;
751
- return template2;
752
- }
753
- function Fragment(props) {
754
- return props.children;
755
- }
756
-
757
- // src/template/hooks.ts
758
- function onMount(cb) {
759
- var _a;
760
- throwIfOutsideComponent("onMounted");
761
- (_a = ComponentNode.ref) == null ? void 0 : _a.addHook("mounted", cb);
762
- }
763
- function onDestroy(cb) {
764
- var _a;
765
- throwIfOutsideComponent("onDestroy");
766
- (_a = ComponentNode.ref) == null ? void 0 : _a.addHook("destroy", cb);
767
- }
768
- function throwIfOutsideComponent(hook) {
769
- if (!ComponentNode.ref) {
770
- throw new Error(
771
- `"${hook}" can only be called within the component function body
772
- and cannot be used in asynchronous or deferred calls.`
773
- );
774
- }
775
- }
776
- function useProvide(key, value) {
777
- var _a;
778
- throwIfOutsideComponent("useProvide");
779
- (_a = ComponentNode.ref) == null ? void 0 : _a.setContext(key, value);
780
- }
781
- function useInject(key, defaultValue) {
782
- var _a;
783
- throwIfOutsideComponent("useInject");
784
- return ((_a = ComponentNode.ref) == null ? void 0 : _a.getContext(key)) || defaultValue;
785
- }
786
-
787
- exports.Fragment = Fragment;
788
- exports.computed = computed;
789
- exports.createSignal = createSignal;
790
- exports.createStore = createStore;
791
- exports.effect = effect;
792
- exports.h = h;
793
- exports.isJsxElement = isJsxElement;
794
- exports.nextTick = nextTick;
795
- exports.onDestroy = onDestroy;
796
- exports.onMount = onMount;
797
- exports.signal = signal;
798
- exports.template = template;
799
- exports.toSignalObject = toSignalObject;
800
- exports.useInject = useInject;
801
- exports.useProvide = useProvide;
3
+ var essorShared = require('essor-shared');
4
+
5
+ var oe=Object.defineProperty;var z=Object.getOwnPropertySymbols;var re=Object.prototype.hasOwnProperty,ie=Object.prototype.propertyIsEnumerable;var B=(n,e,t)=>e in n?oe(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,U=(n,e)=>{for(var t in e||(e={}))re.call(e,t)&&B(n,t,e[t]);if(z)for(var t of z(e))ie.call(e,t)&&B(n,t,e[t]);return n};var E=null,C=null,$=new Set,P=new WeakMap,K=new Set;function w(n,e){let t=P.get(n);t||(t=new Map,P.set(n,t));let o=t.get(e);o||(o=new Set,t.set(e,o)),E&&o.add(E),C&&$.add(C);}function W(n,e){$.size>0&&$.forEach(r=>r.run());let t=P.get(n);if(!t)return;let o=t.get(e);o&&o.forEach(r=>K.has(r)&&r());}var M=class{constructor(e){this._value=e;}valueOf(){return w(this,"value"),this._value}toString(){return w(this,"value"),String(this._value)}toJSON(){return this._value}get value(){return w(this,"value"),this._value}set value(e){this._value!==e&&(this._value=e,W(this,"value"));}peek(){return this._value}update(){W(this,"value");}};function g(n){return x(n)?n:new M(n)}function x(n){return n instanceof M}var H=class{constructor(e){this.fn=e;let t=C;C=this,w(this,"_value"),this._value=this.fn(),C=t;}peek(){return this._value}run(){let e=this.fn();e!==this._value&&(this._value=e);}get value(){return w(this,"_value"),this._value}};function _(n){return new H(n)}function y(n){function e(){let t=E;E=e,n(),E=t;}return K.add(e),e(),()=>{K.delete(e),E=null;}}function se(n){let e=new M(n);return [e.value,t=>{e.value=t;}]}function S(n){return Object.entries(n).reduce((t,[o,r])=>(t[o]=x(r)?r:g(r),t),{})}function k(n){return n?x(n)?n.peek():Object.entries(n).reduce((e,[t,o])=>(e[t]=x(o)?o.peek():o,e),{}):{}}var A=0,F=new Map;function ce(n){let{state:e,getters:t,actions:o}=n,r=U({},e!=null?e:{}),i=S(e!=null?e:{}),a=[],l=[],p={patch$(s){Object.assign(i,S(s)),a.forEach(c=>c(k(i))),l.forEach(c=>c(k(i)));},subscribe$(s){a.push(s);},unsubscribe$(s){let c=a.indexOf(s);c!==-1&&a.splice(c,1);},onAction$(s){l.push(s);},reset$(){p.patch$(r);}},u={_id:`store_${A}`};for(let s in t){let c=t[s];c&&(u[s]=_(()=>c.call(i)));}for(let s in o){let c=o[s];c&&(u[s]=c.bind(i));}return F.set(A,g),++A,new Proxy({},{get(s,c){return c==="state"?k(i):c in u?u[c]:c in p?p[c]:i[c].value}})}function ae(n){return function(){return F.has(A)?F.get(A):ce(n)}}function J(n){if(d(n)||n instanceof Node)return n;let e=essorShared.isFalsy(n)?"":String(n);return document.createTextNode(e)}function N(n,e,t=null){let o=d(t)?t.firstChild:t;d(e)?e.mount(n,o):o?o.before(e):n.append(e);}function b(n){d(n)?n.unmount():n.parentNode&&n.remove();}function R(n,e,t){N(n,e,t),b(t);}function G(n,e,t){if(e==="class"){typeof t=="string"?n.className=t:Array.isArray(t)?n.className=t.join(" "):t&&typeof t=="object"&&(n.className=Object.entries(t).reduce((o,[r,i])=>o+(i?` ${r}`:""),"").trim());return}if(e==="style"){if(typeof t=="string")n.style.cssText=t;else if(t&&typeof t=="object"){let o=t;Object.keys(o).forEach(r=>{n.style.setProperty(essorShared.kebabCase(r),String(o[r]));});}return}essorShared.isFalsy(t)?n.removeAttribute(e):t===!0?n.setAttribute(e,""):n.setAttribute(e,String(t));}function Y(n,e){if(n instanceof HTMLInputElement){if(n.type==="checkbox")return f(n,"change",()=>{e(!!n.checked);});if(n.type==="date")return f(n,"change",()=>{e(n.value?n.value:"");});if(n.type==="file")return f(n,"change",()=>{n.files&&e(n.files);});if(n.type==="number")return f(n,"input",()=>{let t=Number.parseFloat(n.value);e(Number.isNaN(t)?"":String(t));});if(n.type==="radio")return f(n,"change",()=>{e(n.checked?n.value:"");});if(n.type==="text")return f(n,"input",()=>{e(n.value);})}if(n instanceof HTMLSelectElement)return f(n,"change",()=>{e(n.value);});if(n instanceof HTMLTextAreaElement)return f(n,"input",()=>{e(n.value);})}var q=Promise.resolve();function ue(n){return n?q.then(n):q}function f(n,e,t){return n.addEventListener(e,t),()=>n.removeEventListener(e,t)}var T=class T{constructor(e,t,o){this.template=e;this.props=t;this.key=o;this.proxyProps={};this.context={};this.emitter=new Set;this.mounted=!1;this.rootNode=null;this.hooks={mounted:new Set,destroy:new Set};this.trackMap=new Map;this.proxyProps=S(t);}addEventListener(){}removeEventListener(){}get firstChild(){var e,t;return (t=(e=this.rootNode)==null?void 0:e.firstChild)!=null?t:null}get isConnected(){var e,t;return (t=(e=this.rootNode)==null?void 0:e.isConnected)!=null?t:!1}addHook(e,t){var o;(o=this.hooks[e])==null||o.add(t);}getContext(e){return T.context[e]}setContext(e,t){T.context[e]=t;}inheritNode(e){this.context=e.context,this.hooks=e.hooks,Object.assign(this.proxyProps,e.proxyProps),this.rootNode=e.rootNode,this.trackMap=e.trackMap;let t=this.props;this.props=e.props,this.patchProps(t);}unmount(){var e;this.hooks.destroy.forEach(t=>t()),Object.values(this.hooks).forEach(t=>t.clear()),(e=this.rootNode)==null||e.unmount(),this.rootNode=null,this.proxyProps={},this.mounted=!1,this.emitter.forEach(t=>t()),T.context={};}mount(e,t){var r,i,a,l;if(!essorShared.isFunction(this.template))throw new Error("Template must be a function");if(this.isConnected)return (i=(r=this.rootNode)==null?void 0:r.mount(e,t))!=null?i:[];T.ref=this,this.rootNode=this.template(k(this.proxyProps)),T.ref=null,this.mounted=!0;let o=(l=(a=this.rootNode)==null?void 0:a.mount(e,t))!=null?l:[];return this.hooks.mounted.forEach(p=>p()),this.patchProps(this.props),o}getNodeTrack(e,t){let o=this.trackMap.get(e);return o||(o={cleanup:()=>{}},this.trackMap.set(e,o)),t||o.cleanup(),o}patchProps(e){var t,o,r,i,a;for(let[l,p]of Object.entries(e))if(l.indexOf("on")===0&&((t=this.rootNode)!=null&&t.nodes)){let u=l.slice(2).toLowerCase(),s=p,c=f(this.rootNode.nodes[0],u,s);this.emitter.add(c);}else if(l.indexOf("bind:")===0)this.proxyProps[l]=g(p);else if(l==="ref")essorShared.isFunction(p)?p((o=this.rootNode)==null?void 0:o.nodes[0]):e[l]=(r=this.rootNode)==null?void 0:r.nodes[0];else {let u=(a=(i=this.proxyProps)[l])!=null?a:i[l]=g(p),s=this.getNodeTrack(l);s.cleanup=y(()=>{u.value=p;});}this.props=e;}};T.ref=null,T.context={};var m=T;function ee(n,e,t,o){let r=new Map,i=e.values(),a=n.childNodes.length;if(e.size>0&&t.length===0){if(a===e.size+(o?1:0)){let u=n;u.innerHTML="",o&&N(n,o);}else {let u=document.createRange(),s=i.next().value,c=d(s)?s.firstChild:s;u.setStartBefore(c),o?u.setEndBefore(o):u.setEndAfter(n),u.deleteContents();}return e.forEach(u=>{d(u)&&u.unmount();}),r}let l=[],p=fe(t);for(let[u,s]of t.entries()){let c=i.next().value,v=j(c,u);for(;c&&!p.has(v);)b(c),e.delete(v),c=i.next().value,v=j(c,u);let X=j(s,u),V=e.get(X);if(V&&(s=pe(n,V,s)),c)if(c){let D=document.createComment("");N(n,D,c),l.push([D,s]);}else N(n,s,o);else N(n,s,o);r.set(X,s);}return l.forEach(([u,s])=>R(n,s,u)),e.forEach((u,s)=>{u.isConnected&&!r.has(s)&&b(u);}),r}function pe(n,e,t){return e===t?e:d(e)&&d(t)&&e.template===t.template?(t.inheritNode(e),t):e instanceof Text&&t instanceof Text?(e.textContent!==t.textContent&&(e.textContent=t.textContent),e):(R(n,t,e),t)}function fe(n){let e=new Map;for(let[t,o]of n.entries()){let r=j(o,t);e.set(r,o);}return e}function j(n,e){let t=n instanceof Element?n.id:void 0,o=t===""?void 0:t;return o!=null?o:`_$${e}$`}var O=class n{constructor(e,t,o){this.template=e;this.props=t;this.key=o;this.treeMap=new Map;this.mounted=!1;this.nodes=[];this.provides={};this.trackMap=new Map;this.parent=null;}get firstChild(){var e;return (e=this.nodes[0])!=null?e:null}get isConnected(){return this.mounted}addEventListener(){}removeEventListener(){}unmount(){this.trackMap.forEach(e=>{var t,o;(t=e.cleanup)==null||t.call(e),(o=e.lastNodes)==null||o.forEach(r=>{e.isRoot?b(r):r instanceof n&&r.unmount();});}),this.trackMap.clear(),this.treeMap.clear(),this.nodes.forEach(e=>b(e)),this.nodes=[],this.mounted=!1;}mount(e,t){var i;if(this.parent=e,this.isConnected)return this.nodes.forEach(a=>N(e,a,t)),this.nodes;let o=this.template.content.cloneNode(!0),r=o.firstChild;return (i=r==null?void 0:r.hasAttribute)!=null&&i.call(r,"_svg_")&&(r.remove(),r==null||r.childNodes.forEach(a=>{o.append(a);})),this.nodes=Array.from(o.childNodes),this.mapNodeTree(e,o),N(e,o,t),this.patchNodes(this.props),this.mounted=!0,this.nodes}mapNodeTree(e,t){let o=1;this.treeMap.set(0,e);let r=i=>{i.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&this.treeMap.set(o++,i);let a=i.firstChild;for(;a;)r(a),a=a.nextSibling;};r(t);}patchNodes(e){for(let t in e){let o=Number(t),r=this.treeMap.get(o);if(r){let i=this.props[t];this.patchNode(t,r,i,o===0);}}this.props=e;}getNodeTrack(e,t,o){var i;let r=this.trackMap.get(e);return r||(r={cleanup:()=>{}},t&&(r.lastNodes=new Map),o&&(r.isRoot=!0),this.trackMap.set(e,r)),(i=r.cleanup)==null||i.call(r),r}inheritNode(e){this.mounted=e.mounted,this.nodes=e.nodes,this.trackMap=e.trackMap,this.treeMap=e.treeMap;let t=this.props;this.props=e.props,this.patchNodes(t);}patchNode(e,t,o,r){for(let i in o)if(i==="children"&&o.children)if(essorShared.isArray(o.children))o.children.forEach(([a,l],p)=>{var v;let u=essorShared.isNil(l)?null:(v=this.treeMap.get(l))!=null?v:null,s=`${e}:${i}:${p}`,c=this.getNodeTrack(s,!0,r);ne(c,t,a,u);});else {let a=`${e}:${i}:0`,l=this.getNodeTrack(a,!0,r);ne(l,t,o.children,null);}else if(i==="ref")essorShared.isFunction(o[i])?o[i](t):o[i]=t;else if(i.indexOf("on")===0){let a=i.slice(2).toLocaleLowerCase(),l=this.getNodeTrack(`${e}:${i}`),p=o[i];l.cleanup=f(t,a,p);}else if(i.indexOf("bind:")===0){let a=i.slice(5).toLocaleLowerCase(),l=o[i],p=this.getNodeTrack(`${e}:${i}`),u=x(l)?l:g(l),s=y(()=>{u.value=l,t[a]=u.value;}),c=Y(t,v=>{o[`update:${a}`](v);});p.cleanup=()=>{s==null||s(),c==null||c();};}else if(i.indexOf("update:")!==0){let a=this.getNodeTrack(`${e}:${i}`),l=o[i],p=x(l)?l:g(l),u=y(()=>{p.value=l,he(a,t,i,p.value);});a.cleanup=()=>{u==null||u();};}}};function he(n,e,t,o){let r=e;r.setAttribute&&(essorShared.isFunction(o)?n.cleanup=y(()=>{G(r,t,o());}):G(r,t,o));}function ne(n,e,t,o){essorShared.isFunction(t)?n.cleanup=y(()=>{let r=essorShared.coerceArray(t()).map(J);n.lastNodes=ee(e,n.lastNodes,r,o);}):essorShared.coerceArray(t).forEach((r,i)=>{let a=J(r);n.lastNodes.set(String(i),a),N(e,a,o);});}function Ne(n,e,t){return essorShared.isFunction(n)?new m(n,e,t):new O(n,e,t)}function d(n){return n instanceof m||n instanceof O}function Te(n){let e=document.createElement("template");return e.innerHTML=n,e}function ve(n){return n.children}function ye(n){var e;L("onMounted"),(e=m.ref)==null||e.addHook("mounted",n);}function xe(n){var e;L("onDestroy"),(e=m.ref)==null||e.addHook("destroy",n);}function L(n){if(!m.ref)throw new Error(`"${n}" can only be called within the component function body
6
+ and cannot be used in asynchronous or deferred calls.`)}function be(n,e){var t;L("useProvide"),(t=m.ref)==null||t.setContext(n,e);}function Ee(n,e){var t;return L("useInject"),((t=m.ref)==null?void 0:t.getContext(n))||e}
7
+
8
+ exports.Fragment = ve;
9
+ exports.computed = _;
10
+ exports.createSignal = se;
11
+ exports.createStore = ae;
12
+ exports.effect = y;
13
+ exports.h = Ne;
14
+ exports.isJsxElement = d;
15
+ exports.nextTick = ue;
16
+ exports.onDestroy = xe;
17
+ exports.onMount = ye;
18
+ exports.signal = g;
19
+ exports.signalObject = S;
20
+ exports.template = Te;
21
+ exports.useInject = Ee;
22
+ exports.useProvide = be;
802
23
  //# sourceMappingURL=out.js.map
803
24
  //# sourceMappingURL=index.cjs.js.map