essor 0.0.11 → 0.0.12

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.
@@ -1,11 +1,11 @@
1
1
  /**
2
- * essor v0.0.11
2
+ * essor v0.0.12
3
3
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
4
4
  * @license MIT
5
5
  **/
6
6
 
7
7
  // src/version.ts
8
- var essor_version = "0.0.11";
8
+ var essor_version = "0.0.12";
9
9
 
10
10
  // ../shared/dist/shared.js
11
11
  var isObject = (val) => val !== null && typeof val === "object";
@@ -13,6 +13,9 @@ var isArray = Array.isArray;
13
13
  function isString(val) {
14
14
  return typeof val === "string";
15
15
  }
16
+ function isSymbol(val) {
17
+ return typeof val === "symbol";
18
+ }
16
19
  function isNil(x) {
17
20
  return x === null || x === void 0;
18
21
  }
@@ -29,6 +32,24 @@ function startsWith(str, searchString) {
29
32
  }
30
33
  return str.indexOf(searchString) === 0;
31
34
  }
35
+ function escape(str) {
36
+ return str.replaceAll(/["&'<>]/g, (char) => {
37
+ switch (char) {
38
+ case "&":
39
+ return "&amp;";
40
+ case "<":
41
+ return "&lt;";
42
+ case ">":
43
+ return "&gt;";
44
+ case '"':
45
+ return "&quot;";
46
+ case "'":
47
+ return "&#039;";
48
+ default:
49
+ return char;
50
+ }
51
+ });
52
+ }
32
53
  var kebabCase = (string) => {
33
54
  return string.replaceAll(/[A-Z]+/g, (match, offset) => {
34
55
  return `${offset > 0 ? "-" : ""}${match.toLocaleLowerCase()}`;
@@ -60,9 +81,6 @@ var isArray2 = Array.isArray;
60
81
  function isString2(val) {
61
82
  return typeof val === "string";
62
83
  }
63
- function isNull(val) {
64
- return val === null;
65
- }
66
84
  function isSet(val) {
67
85
  return _toString.call(val) === "[object Set]";
68
86
  }
@@ -76,41 +94,90 @@ function isMap(val) {
76
94
  return _toString.call(val) === "[object Map]";
77
95
  }
78
96
  var isFunction2 = (val) => typeof val === "function";
79
- var isPrimitive = (val) => ["string", "number", "boolean", "symbol", "undefined"].includes(typeof val) || isNull(val);
80
- function isHTMLElement(obj) {
81
- if (!obj) return false;
82
- return obj && typeof obj === "object" && obj.nodeType === 1 && typeof obj.nodeName === "string";
97
+ var isPlainObject = (val) => _toString.call(val) === "[object Object]";
98
+ function isStringNumber(val) {
99
+ if (!isString2(val)) {
100
+ return false;
101
+ }
102
+ return !Number.isNaN(Number(val));
83
103
  }
84
104
  var _toString = Object.prototype.toString;
85
105
  var hasOwnProperty = Object.prototype.hasOwnProperty;
86
106
  var hasOwn = (val, key) => hasOwnProperty.call(val, key);
87
107
  var hasChanged = (value, oldValue) => value !== oldValue && (value === value || oldValue === oldValue);
88
108
  var noop2 = Function.prototype;
89
- function startsWith2(str, searchString) {
90
- if (!isString2(str)) {
91
- return false;
92
- }
93
- return str.indexOf(searchString) === 0;
94
- }
95
109
  function isExclude(key, exclude) {
96
110
  return Array.isArray(exclude) ? exclude.includes(key) : isFunction2(exclude) ? exclude(key) : false;
97
111
  }
98
112
  function warn(msg, ...args) {
99
113
  console.warn.apply(console, [`[Essor warn]: ${msg}`].concat(args));
100
114
  }
115
+ var queue = [];
116
+ var activePreFlushCbs = [];
117
+ var p = Promise.resolve();
118
+ var isFlushPending = false;
119
+ function nextTick(fn) {
120
+ return fn ? p.then(fn) : p;
121
+ }
122
+ function queueJob(job) {
123
+ if (!queue.includes(job)) {
124
+ queue.push(job);
125
+ queueFlush();
126
+ }
127
+ }
128
+ function queueFlush() {
129
+ if (isFlushPending) {
130
+ return;
131
+ }
132
+ isFlushPending = true;
133
+ nextTick(flushJobs);
134
+ }
135
+ function queuePreFlushCb(cb) {
136
+ queueCb(cb, activePreFlushCbs);
137
+ }
138
+ function queueCb(cb, activeQueue) {
139
+ if (!activeQueue.includes(cb)) {
140
+ activeQueue.push(cb);
141
+ queueFlush();
142
+ }
143
+ }
144
+ function flushJobs() {
145
+ isFlushPending = false;
146
+ flushPreFlushCbs();
147
+ let job;
148
+ while (job = queue.shift()) {
149
+ if (job) {
150
+ job();
151
+ }
152
+ }
153
+ }
154
+ function flushPreFlushCbs() {
155
+ while (activePreFlushCbs.length > 0) {
156
+ const cb = activePreFlushCbs.shift();
157
+ if (cb) {
158
+ cb();
159
+ }
160
+ }
161
+ }
101
162
  var activeEffect = null;
102
163
  var activeComputed = null;
103
- var computedMap = /* @__PURE__ */ new WeakMap();
104
- var signalMap = /* @__PURE__ */ new WeakMap();
105
- var effectDeps = /* @__PURE__ */ new Set();
164
+ var triggerMap = /* @__PURE__ */ new WeakMap();
106
165
  var reactiveMap = /* @__PURE__ */ new WeakMap();
107
- var arrayMethods = ["push", "pop", "shift", "unshift", "splice", "sort", "reverse"];
166
+ var ReactiveSymbol = Symbol("ReactiveSymbol");
167
+ var ReactivePeekSymbol = Symbol("__raw");
168
+ var SignalValueKey = Symbol("SignalValueKey");
169
+ var ComputedValueKey = Symbol("ComputedValueKey");
170
+ var reactiveArrayKey = Symbol("ReactiveArrayKey");
171
+ var ReactiveCollectionKey = Symbol("ReactiveCollectionKey");
172
+ var ReactiveWeakCollectionKey = Symbol("ReactiveWeakCollectionKey");
173
+ var inBatch = false;
174
+ var batchQueue = /* @__PURE__ */ new Set();
108
175
  function track(target, key) {
109
176
  if (!activeEffect && !activeComputed) return;
110
- let depsMap = signalMap.get(target);
177
+ let depsMap = triggerMap.get(target);
111
178
  if (!depsMap) {
112
179
  depsMap = /* @__PURE__ */ new Map();
113
- signalMap.set(target, depsMap);
180
+ triggerMap.set(target, depsMap);
114
181
  }
115
182
  let dep = depsMap.get(key);
116
183
  if (!dep) {
@@ -118,75 +185,63 @@ function track(target, key) {
118
185
  depsMap.set(key, dep);
119
186
  }
120
187
  if (activeEffect) dep.add(activeEffect);
121
- let computedDepsMap = computedMap.get(target);
122
- if (!computedDepsMap) {
123
- computedDepsMap = /* @__PURE__ */ new Map();
124
- computedMap.set(target, computedDepsMap);
125
- }
126
- let computedDeps = computedDepsMap.get(key);
127
- if (!computedDeps) {
128
- computedDeps = /* @__PURE__ */ new Set();
129
- computedDepsMap.set(key, computedDeps);
130
- }
131
- if (activeComputed) {
132
- computedDeps.add(activeComputed);
133
- }
134
188
  }
135
189
  function trigger(target, key) {
136
- const depsMap = signalMap.get(target);
190
+ const depsMap = triggerMap.get(target);
137
191
  if (!depsMap) return;
138
192
  const dep = depsMap.get(key);
139
193
  if (dep) {
140
- dep.forEach((effect) => effectDeps.has(effect) && effect());
141
- }
142
- const computedDepsMap = computedMap.get(target);
143
- if (computedDepsMap) {
144
- const computeds = computedDepsMap.get(key);
145
- if (computeds) {
146
- computeds.forEach((computed) => computed.run());
147
- }
194
+ dep.forEach((effect) => {
195
+ if (hasOwn(effect, "active") && !effect.active) {
196
+ dep.delete(effect);
197
+ return;
198
+ }
199
+ if (inBatch) {
200
+ batchQueue.add(effect);
201
+ } else {
202
+ effect();
203
+ }
204
+ });
148
205
  }
149
206
  }
150
207
  var Signal = class {
208
+ /**
209
+ * Creates a new Signal instance.
210
+ * @param {T} value - The initial value of the Signal.
211
+ * @param {boolean} [shallow] - Whether to create a shallow Signal.
212
+ */
151
213
  constructor(value, shallow = false) {
152
- this._value = value;
214
+ this.__signal = true;
153
215
  this._shallow = shallow;
216
+ this._value = value;
154
217
  }
155
218
  /**
156
- * Get the current value of the Signal and track its usage.
219
+ * Gets the current value of the Signal.
220
+ * @returns {T} The current value.
157
221
  */
158
222
  get value() {
159
- track(this, "_sv");
160
- this.__triggerObject();
161
- return this._value;
162
- }
163
- /**
164
- * Trigger reactivity for non-primitive and non-HTMLElement values.
165
- * Recursively applies reactivity to nested objects.
166
- */
167
- __triggerObject() {
168
- if (!isPrimitive(this._value) && !isHTMLElement(this._value) && !this._shallow) {
169
- useReactive(this._value);
223
+ track(this, SignalValueKey);
224
+ if (isObject2(this._value) && !this._shallow) {
225
+ return useReactive(this._value);
170
226
  }
227
+ return this._value;
171
228
  }
172
229
  /**
173
- * Set a new value to the Signal and trigger updates if the value has changed.
230
+ * Sets a new value for the Signal.
231
+ * @param {T} newValue - The new value to set.
174
232
  */
175
233
  set value(newValue) {
176
234
  if (isSignal(newValue)) {
177
- console.warn("Signal cannot be set to another signal, use .peek() instead");
178
235
  newValue = newValue.peek();
179
236
  }
180
237
  if (hasChanged(newValue, this._value)) {
181
238
  this._value = newValue;
182
- if (!isPrimitive(this._value) && !isHTMLElement(this._value)) {
183
- this.__triggerObject();
184
- }
185
- trigger(this, "_sv");
239
+ trigger(this, SignalValueKey);
186
240
  }
187
241
  }
188
242
  /**
189
- * Peek at the current value of the Signal without tracking it.
243
+ * Returns the current value without triggering reactivity.
244
+ * @returns {T} The current value.
190
245
  */
191
246
  peek() {
192
247
  return this._value;
@@ -202,15 +257,16 @@ function shallowSignal(value) {
202
257
  return new Signal(value, true);
203
258
  }
204
259
  function isSignal(value) {
205
- return value instanceof Signal;
260
+ return !!(value && (value == null ? void 0 : value.__signal));
206
261
  }
207
262
  var Computed = class {
208
263
  constructor(fn) {
209
264
  this.fn = fn;
210
- const prev = activeComputed;
211
- activeComputed = this;
265
+ this.__computed = true;
266
+ const prev = activeEffect;
267
+ activeEffect = this.run.bind(this);
212
268
  this._value = this.fn();
213
- activeComputed = prev;
269
+ activeEffect = prev;
214
270
  }
215
271
  /**
216
272
  * Get the current computed value without tracking it.
@@ -225,14 +281,14 @@ var Computed = class {
225
281
  const newValue = this.fn();
226
282
  if (hasChanged(newValue, this._value)) {
227
283
  this._value = newValue;
228
- trigger(this, "_cv");
284
+ trigger(this, ComputedValueKey);
229
285
  }
230
286
  }
231
287
  /**
232
288
  * Get the current computed value and track its usage.
233
289
  */
234
290
  get value() {
235
- track(this, "_cv");
291
+ track(this, ComputedValueKey);
236
292
  return this._value;
237
293
  }
238
294
  };
@@ -240,23 +296,46 @@ function useComputed(fn) {
240
296
  return new Computed(fn);
241
297
  }
242
298
  function isComputed(value) {
243
- return value instanceof Computed;
299
+ return !!(value && value.__computed);
300
+ }
301
+ function createScheduler(effect, flush) {
302
+ if (flush === "sync") {
303
+ return () => effect();
304
+ } else if (flush === "pre") {
305
+ return () => queuePreFlushCb(effect);
306
+ } else {
307
+ return () => {
308
+ nextTick(() => queueJob(effect));
309
+ };
310
+ }
244
311
  }
245
- function useEffect(fn) {
312
+ function useEffect(fn, options = {}) {
313
+ const { flush = "pre", onTrack, onTrigger } = options;
246
314
  function effectFn() {
247
315
  const prev = activeEffect;
248
- activeEffect = effectFn;
316
+ activeEffect = effectFn.init ? effectFn : effectFn.scheduler;
249
317
  fn();
318
+ onTrigger && onTrigger();
250
319
  activeEffect = prev;
251
320
  }
252
- effectDeps.add(effectFn);
321
+ const scheduler = createScheduler(effectFn, flush);
322
+ effectFn.scheduler = scheduler;
323
+ effectFn.init = true;
324
+ effectFn.active = true;
325
+ onTrack && onTrack();
253
326
  effectFn();
254
327
  return () => {
255
- effectDeps.delete(effectFn);
328
+ effectFn.active = false;
256
329
  activeEffect = null;
257
330
  };
258
331
  }
259
332
  function signalObject(initialValues, exclude) {
333
+ if (!initialValues || !isObject2(initialValues)) {
334
+ {
335
+ warn("initialValues must be an object,will return initial value!", initialValues);
336
+ }
337
+ return initialValues;
338
+ }
260
339
  const signals = Object.entries(initialValues).reduce((acc, [key, value]) => {
261
340
  acc[key] = isExclude(key, exclude) || isSignal(value) ? value : useSignal(value);
262
341
  return acc;
@@ -271,7 +350,7 @@ function unSignal(signal, exclude) {
271
350
  if (isArray2(signal)) {
272
351
  return signal.map((value) => unSignal(value, exclude));
273
352
  }
274
- if (isObject2(signal)) {
353
+ if (isPlainObject(signal)) {
275
354
  return Object.entries(signal).reduce((acc, [key, value]) => {
276
355
  if (isExclude(key, exclude)) {
277
356
  acc[key] = value;
@@ -283,33 +362,14 @@ function unSignal(signal, exclude) {
283
362
  }
284
363
  return signal;
285
364
  }
286
- var REACTIVE_MARKER = Symbol("useReactive");
287
365
  function isReactive(obj) {
288
- return obj && obj[REACTIVE_MARKER] === true;
289
- }
290
- function createArrayProxy(initialValue) {
291
- arrayMethods.forEach((method) => {
292
- const originalMethod = Array.prototype[method];
293
- track(initialValue, "length");
294
- Object.defineProperty(initialValue, method, {
295
- value(...args) {
296
- const result = originalMethod.apply(this, args);
297
- if (["push", "pop", "shift", "unshift", "splice", "sort", "reverse"].includes(method)) {
298
- trigger(initialValue, "length");
299
- }
300
- return result;
301
- },
302
- enumerable: false,
303
- writable: true,
304
- configurable: true
305
- });
306
- });
366
+ return !!(obj && typeof obj === "object" && obj[ReactiveSymbol]);
307
367
  }
308
368
  function useReactive(initialValue, exclude) {
309
- return reactive(initialValue, exclude, false);
369
+ return reactive(initialValue, false, exclude);
310
370
  }
311
371
  function shallowReactive(initialValue, exclude) {
312
- return reactive(initialValue, exclude, true);
372
+ return reactive(initialValue, true, exclude);
313
373
  }
314
374
  function unReactive(target) {
315
375
  if (!isObject2(target)) {
@@ -318,40 +378,13 @@ function unReactive(target) {
318
378
  if (!isReactive(target)) {
319
379
  return target;
320
380
  }
321
- const unReactiveObj = Array.isArray(target) ? [] : {};
322
- for (const key in target) {
323
- if (hasOwn(target, key)) {
324
- const value = target[key];
325
- if (isReactive(value)) {
326
- unReactiveObj[key] = unReactive(value);
327
- } else if (isSignal(value)) {
328
- unReactiveObj[key] = value.peek();
329
- } else {
330
- unReactiveObj[key] = value;
331
- }
332
- }
333
- }
334
- return unReactiveObj;
381
+ return target[ReactivePeekSymbol];
335
382
  }
336
- function reactive(initialValue, exclude, shallow = false) {
337
- if (!isObject2(initialValue)) {
338
- return initialValue;
339
- }
340
- if (isReactive(initialValue)) {
341
- return initialValue;
342
- }
343
- if (reactiveMap.has(initialValue)) {
344
- return reactiveMap.get(initialValue);
345
- }
346
- if (Array.isArray(initialValue)) {
347
- createArrayProxy(initialValue);
348
- }
349
- if (isSet(initialValue) || isMap(initialValue) || isWeakSet(initialValue) || isWeakMap(initialValue)) {
350
- return initialValue;
351
- }
352
- const handler = {
383
+ var basicHandler = (shallow, exclude) => {
384
+ return {
353
385
  get(target, key, receiver) {
354
- if (key === REACTIVE_MARKER || startsWith2(key, "_")) return true;
386
+ if (key === ReactiveSymbol) return true;
387
+ if (key === ReactivePeekSymbol) return target;
355
388
  const getValue = Reflect.get(target, key, receiver);
356
389
  const value = isSignal(getValue) ? getValue.value : getValue;
357
390
  if (isExclude(key, exclude)) {
@@ -381,6 +414,7 @@ function reactive(initialValue, exclude, shallow = false) {
381
414
  }
382
415
  return obj;
383
416
  },
417
+ // handle delete
384
418
  deleteProperty(target, key) {
385
419
  const oldValue = Reflect.get(target, key);
386
420
  const result = Reflect.deleteProperty(target, key);
@@ -390,48 +424,386 @@ function reactive(initialValue, exclude, shallow = false) {
390
424
  return result;
391
425
  }
392
426
  };
427
+ };
428
+ var arrayInstrumentations = createArrayInstrumentations();
429
+ function createArrayInstrumentations() {
430
+ const instrumentations2 = {};
431
+ ["includes", "indexOf", "lastIndexOf"].forEach((key) => {
432
+ instrumentations2[key] = function(...args) {
433
+ const arr = this;
434
+ for (let i = 0, l = this.length; i < l; i++) {
435
+ track(arr, `${i}`);
436
+ }
437
+ const res = arr[key](...args);
438
+ if (res === -1 || res === false) {
439
+ return arr[key](...args);
440
+ }
441
+ return res;
442
+ };
443
+ });
444
+ ["push", "pop", "shift", "unshift", "splice", "sort", "reverse", "fill", "copyWithin"].forEach(
445
+ (key) => {
446
+ instrumentations2[key] = function(...args) {
447
+ const arr = unReactive(this);
448
+ const res = arr[key].apply(this, args);
449
+ trigger(arr, reactiveArrayKey);
450
+ return res;
451
+ };
452
+ }
453
+ );
454
+ [
455
+ "forEach",
456
+ "map",
457
+ "filter",
458
+ "reduce",
459
+ "reduceRight",
460
+ "some",
461
+ "every",
462
+ "find",
463
+ "findIndex",
464
+ "findLast",
465
+ "findLastIndex",
466
+ "entries",
467
+ "keys",
468
+ "values"
469
+ ].forEach((key) => {
470
+ instrumentations2[key] = function(...args) {
471
+ const arr = unReactive(this);
472
+ track(arr, reactiveArrayKey);
473
+ return arr[key].apply(this, args);
474
+ };
475
+ });
476
+ return instrumentations2;
477
+ }
478
+ var ArrayHandler = (shallow, exclude) => {
479
+ return {
480
+ get(target, key, receiver) {
481
+ if (key === ReactiveSymbol) return true;
482
+ if (key === ReactivePeekSymbol) return target;
483
+ if (arrayInstrumentations.hasOwnProperty(key)) {
484
+ return Reflect.get(arrayInstrumentations, key, receiver);
485
+ }
486
+ const value = Reflect.get(target, key, receiver);
487
+ if (isExclude(key, exclude)) {
488
+ return value;
489
+ }
490
+ if (isStringNumber(key)) {
491
+ track(target, key);
492
+ }
493
+ track(target, "length");
494
+ if (isObject2(value) && !shallow) {
495
+ return reactive(value);
496
+ }
497
+ return value;
498
+ },
499
+ set(target, key, value, receiver) {
500
+ const oldValue = Reflect.get(target, key, receiver);
501
+ const result = Reflect.set(target, key, value, receiver);
502
+ if (hasChanged(value, oldValue)) {
503
+ if (isStringNumber(key)) {
504
+ trigger(target, key);
505
+ }
506
+ if (key === "length") {
507
+ trigger(target, "length");
508
+ }
509
+ }
510
+ return result;
511
+ }
512
+ };
513
+ };
514
+ var collectionHandlers = {
515
+ get(target, key) {
516
+ if (key === ReactiveSymbol) return true;
517
+ if (key === ReactivePeekSymbol) return target;
518
+ if (key === Symbol.iterator || key === "size") {
519
+ track(target, ReactiveCollectionKey);
520
+ }
521
+ return Reflect.get(
522
+ hasOwn(instrumentations, key) && key in target ? instrumentations : target,
523
+ key,
524
+ target
525
+ );
526
+ }
527
+ };
528
+ var weakCollectionHandlers = {
529
+ get(target, key) {
530
+ if (key === ReactiveSymbol) return true;
531
+ if (key === ReactivePeekSymbol) return target;
532
+ return Reflect.get(
533
+ hasOwn(weakInstrumentations, key) && key in target ? weakInstrumentations : target,
534
+ key,
535
+ target
536
+ );
537
+ }
538
+ };
539
+ var instrumentations = {
540
+ get(key) {
541
+ const target = unReactive(this);
542
+ track(target, ReactiveCollectionKey);
543
+ return target.get(key);
544
+ },
545
+ set(key, value) {
546
+ const target = unReactive(this);
547
+ const result = target.set(key, value);
548
+ trigger(target, ReactiveCollectionKey);
549
+ return result;
550
+ },
551
+ add(value) {
552
+ const target = unReactive(this);
553
+ const result = target.add(value);
554
+ trigger(target, ReactiveCollectionKey);
555
+ return result;
556
+ },
557
+ has(key) {
558
+ const target = unReactive(this);
559
+ track(target, ReactiveCollectionKey);
560
+ return target.has(key);
561
+ },
562
+ delete(key) {
563
+ const target = unReactive(this);
564
+ const hadKey = target.has(key);
565
+ const result = target.delete(key);
566
+ if (hadKey) {
567
+ trigger(target, ReactiveCollectionKey);
568
+ }
569
+ return result;
570
+ },
571
+ clear() {
572
+ const target = unReactive(this);
573
+ const hadItems = target.size > 0;
574
+ const result = target.clear();
575
+ if (hadItems) {
576
+ trigger(target, ReactiveCollectionKey);
577
+ }
578
+ return result;
579
+ },
580
+ forEach(callback, thisArg) {
581
+ const target = unReactive(this);
582
+ track(target, ReactiveCollectionKey);
583
+ target.forEach((value, key) => {
584
+ callback.call(thisArg, value, key, target);
585
+ });
586
+ },
587
+ get size() {
588
+ const target = unReactive(this);
589
+ track(target, ReactiveCollectionKey);
590
+ return target.size;
591
+ },
592
+ keys() {
593
+ const target = unReactive(this);
594
+ track(target, ReactiveCollectionKey);
595
+ return target.keys();
596
+ },
597
+ values() {
598
+ const target = unReactive(this);
599
+ track(target, ReactiveCollectionKey);
600
+ return target.values();
601
+ },
602
+ entries() {
603
+ const target = unReactive(this);
604
+ track(target, ReactiveCollectionKey);
605
+ return target.entries();
606
+ },
607
+ [Symbol.iterator]() {
608
+ const target = unReactive(this);
609
+ track(target, ReactiveCollectionKey);
610
+ return target[Symbol.iterator]();
611
+ }
612
+ };
613
+ var weakInstrumentations = {
614
+ get(key) {
615
+ const target = unReactive(this);
616
+ track(target, ReactiveWeakCollectionKey);
617
+ return target.get(key);
618
+ },
619
+ set(key, value) {
620
+ const target = unReactive(this);
621
+ const result = target.set(key, value);
622
+ trigger(target, ReactiveWeakCollectionKey);
623
+ return result;
624
+ },
625
+ add(value) {
626
+ const target = unReactive(this);
627
+ const result = target.add(value);
628
+ trigger(target, ReactiveWeakCollectionKey);
629
+ return result;
630
+ },
631
+ has(key) {
632
+ const target = unReactive(this);
633
+ track(target, ReactiveWeakCollectionKey);
634
+ return target.has(key);
635
+ },
636
+ delete(key) {
637
+ const target = unReactive(this);
638
+ const result = target.delete(key);
639
+ trigger(target, ReactiveWeakCollectionKey);
640
+ return result;
641
+ }
642
+ };
643
+ function reactive(initialValue, shallow = false, exclude) {
644
+ if (!isObject2(initialValue)) {
645
+ return initialValue;
646
+ }
647
+ if (isReactive(initialValue)) {
648
+ return initialValue;
649
+ }
650
+ if (reactiveMap.has(initialValue)) {
651
+ return reactiveMap.get(initialValue);
652
+ }
653
+ let handler;
654
+ if (isArray2(initialValue)) {
655
+ track(initialValue, reactiveArrayKey);
656
+ handler = ArrayHandler(shallow, exclude);
657
+ } else if (isSet(initialValue) || isMap(initialValue)) {
658
+ track(initialValue, ReactiveCollectionKey);
659
+ handler = collectionHandlers;
660
+ } else if (isWeakSet(initialValue) || isWeakMap(initialValue)) {
661
+ track(initialValue, ReactiveWeakCollectionKey);
662
+ handler = weakCollectionHandlers;
663
+ } else {
664
+ handler = basicHandler(shallow, exclude);
665
+ }
393
666
  const proxy = new Proxy(initialValue, handler);
394
667
  reactiveMap.set(initialValue, proxy);
395
668
  return proxy;
396
669
  }
670
+ function clearReactive(reactiveObj) {
671
+ if (!isReactive(reactiveObj)) {
672
+ {
673
+ warn("clearReactive: argument must be a reactive object");
674
+ }
675
+ return;
676
+ }
677
+ if (isWeakMap(reactiveObj) || isWeakSet(reactiveObj)) {
678
+ {
679
+ warn("clearReactive: WeakMap and WeakSet are not clearable");
680
+ }
681
+ return;
682
+ }
683
+ useBatch(() => {
684
+ if (isArray2(reactiveObj)) {
685
+ reactiveObj.length = 0;
686
+ } else if (isSet(reactiveObj) || isMap(reactiveObj)) {
687
+ reactiveObj.clear();
688
+ } else if (isObject2(reactiveObj)) {
689
+ Object.keys(reactiveObj).forEach((key) => {
690
+ delete reactiveObj[key];
691
+ });
692
+ }
693
+ });
694
+ }
695
+ function useBatch(fn) {
696
+ try {
697
+ inBatch = true;
698
+ fn();
699
+ } finally {
700
+ inBatch = false;
701
+ runBatch();
702
+ }
703
+ }
704
+ function runBatch() {
705
+ if (batchQueue.size > 0) {
706
+ batchQueue.forEach((effect) => effect());
707
+ batchQueue.clear();
708
+ }
709
+ }
397
710
  function useWatch(source, cb, options) {
398
711
  return doWatch(source, cb, options);
399
712
  }
400
- function doWatch(source, cb, options) {
713
+ var INITIAL_WATCHER_VALUE = void 0;
714
+ var watcher;
715
+ var flushing = false;
716
+ function queueWatcher(fn) {
717
+ watcher = fn;
718
+ if (!flushing) {
719
+ flushing = true;
720
+ nextTick(flushWatchers);
721
+ }
722
+ }
723
+ function flushWatchers() {
724
+ watcher == null ? void 0 : watcher();
725
+ watcher = null;
726
+ flushing = false;
727
+ }
728
+ function doWatch(source, cb, { deep, immediate } = {}) {
401
729
  let getter;
730
+ const isMultiSource = isArray2(source);
402
731
  if (isSignal(source) || isComputed(source)) {
403
732
  getter = () => source.value;
404
733
  } else if (isReactive(source)) {
405
734
  getter = () => __spreadValues({}, source);
406
- } else if (isArray2(source)) {
407
- getter = () => source.map((s) => {
408
- if (isSignal(s) || isComputed(s)) return s.value;
409
- if (isReactive(s)) return __spreadValues({}, s);
410
- if (isFunction2(s)) return s();
411
- return warn("Invalid source", s);
412
- });
735
+ } else if (isMultiSource) {
736
+ getter = () => source.map((s) => resolveSource(s));
413
737
  } else if (isFunction2(source)) {
414
738
  getter = source;
415
739
  } else {
416
740
  warn("Invalid source type", source);
417
- getter = noop2;
741
+ return noop2;
418
742
  }
419
- let oldValue;
743
+ if (cb && deep) {
744
+ const baseGetter = getter;
745
+ const depth = deep === true ? Infinity : deep;
746
+ getter = () => traverse(baseGetter(), depth);
747
+ }
748
+ let oldValue = isMultiSource ? Array.from({ length: source.length }).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
749
+ let runCb = false;
420
750
  const effectFn = () => {
421
751
  const newValue = getter();
422
- if ((options == null ? void 0 : options.deep) || hasChanged(newValue, oldValue)) {
423
- cb && cb(newValue, oldValue);
424
- oldValue = newValue;
752
+ if (hasChanged(newValue, oldValue)) {
753
+ if (immediate && cb) {
754
+ cb(newValue, oldValue);
755
+ oldValue = newValue;
756
+ }
757
+ if (runCb && cb) {
758
+ queueWatcher(() => {
759
+ cb(newValue, oldValue);
760
+ oldValue = newValue;
761
+ });
762
+ }
763
+ !runCb && (oldValue = newValue);
425
764
  }
426
765
  };
427
- const stop = useEffect(effectFn);
428
- if (options == null ? void 0 : options.immediate) {
766
+ const stop = useEffect(effectFn, { flush: "sync" });
767
+ runCb = true;
768
+ if (immediate) {
429
769
  effectFn();
430
770
  }
431
771
  return stop;
432
772
  }
433
- var _id = 0;
434
- var StoreMap = /* @__PURE__ */ new Map();
773
+ function resolveSource(s) {
774
+ if (isSignal(s) || isComputed(s)) return s.value;
775
+ if (isReactive(s)) return __spreadValues({}, s);
776
+ if (isFunction2(s)) return s();
777
+ warn("Invalid source", s);
778
+ return noop2;
779
+ }
780
+ function traverse(value, depth = Infinity, seen) {
781
+ if (depth <= 0 || !isObject2(value)) {
782
+ return value;
783
+ }
784
+ seen = seen || /* @__PURE__ */ new Set();
785
+ if (seen.has(value)) {
786
+ return value;
787
+ }
788
+ seen.add(value);
789
+ depth--;
790
+ if (isSignal(value)) {
791
+ traverse(value.value, depth, seen);
792
+ } else if (isArray2(value)) {
793
+ for (const element of value) {
794
+ traverse(element, depth, seen);
795
+ }
796
+ } else if (isSet(value) || isMap(value)) {
797
+ value.forEach((v) => {
798
+ traverse(v, depth, seen);
799
+ });
800
+ } else if (isPlainObject(value)) {
801
+ for (const key in value) {
802
+ traverse(value[key], depth, seen);
803
+ }
804
+ }
805
+ return value;
806
+ }
435
807
  function createOptionsStore(options) {
436
808
  const { state, getters, actions } = options;
437
809
  const initState = __spreadValues({}, state != null ? state : {});
@@ -466,8 +838,12 @@ function createOptionsStore(options) {
466
838
  for (const key in getters) {
467
839
  const getter = getters[key];
468
840
  if (getter) {
469
- useWatch(useComputed(getter.bind(reactiveState, reactiveState)), (value) => {
470
- store[key] = value;
841
+ Object.defineProperty(store, key, {
842
+ get() {
843
+ return useComputed(getter.bind(reactiveState, reactiveState)).value;
844
+ },
845
+ enumerable: true,
846
+ configurable: true
471
847
  });
472
848
  }
473
849
  }
@@ -477,42 +853,79 @@ function createOptionsStore(options) {
477
853
  store[key] = action.bind(reactiveState);
478
854
  }
479
855
  }
480
- StoreMap.set(_id, store);
481
- ++_id;
482
856
  return store;
483
857
  }
484
858
  function createStore(options) {
485
859
  return function() {
486
- if (StoreMap.has(_id)) {
487
- return StoreMap.get(_id);
488
- }
489
860
  return createOptionsStore(options);
490
861
  };
491
862
  }
492
863
 
493
864
  // ../template/dist/template.dev.esm.js
494
- var _ComponentNode = class _ComponentNode2 {
495
- constructor(template2, props) {
865
+ var __defProp2 = Object.defineProperty;
866
+ var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
867
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
868
+ var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
869
+ var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
870
+ var __spreadValues2 = (a, b) => {
871
+ for (var prop in b || (b = {}))
872
+ if (__hasOwnProp2.call(b, prop))
873
+ __defNormalProp2(a, prop, b[prop]);
874
+ if (__getOwnPropSymbols2)
875
+ for (var prop of __getOwnPropSymbols2(b)) {
876
+ if (__propIsEnum2.call(b, prop))
877
+ __defNormalProp2(a, prop, b[prop]);
878
+ }
879
+ return a;
880
+ };
881
+ var _Hooks = class _Hooks2 {
882
+ constructor() {
883
+ this.hooks = {
884
+ mounted: /* @__PURE__ */ new Set(),
885
+ destroy: /* @__PURE__ */ new Set()
886
+ };
887
+ }
888
+ addEventListener() {
889
+ }
890
+ removeEventListener() {
891
+ }
892
+ addHook(hook, cb) {
893
+ var _a;
894
+ (_a = this.hooks[hook]) == null ? void 0 : _a.add(cb);
895
+ }
896
+ getContext(context) {
897
+ return _Hooks2.context[context];
898
+ }
899
+ setContext(context, value) {
900
+ _Hooks2.context[context] = value;
901
+ }
902
+ initRef() {
903
+ _Hooks2.ref = this;
904
+ }
905
+ removeRef() {
906
+ _Hooks2.ref = null;
907
+ }
908
+ };
909
+ _Hooks.ref = null;
910
+ _Hooks.context = {};
911
+ var Hooks = _Hooks;
912
+ var ComponentNode = class extends Hooks {
913
+ constructor(template2, props, key) {
914
+ super();
496
915
  this.template = template2;
497
916
  this.props = props;
917
+ this.key = key;
498
918
  this.proxyProps = {};
499
- this.context = {};
500
919
  this.emitter = /* @__PURE__ */ new Set();
501
920
  this.mounted = false;
502
921
  this.rootNode = null;
503
- this.hooks = {
504
- mounted: /* @__PURE__ */ new Set(),
505
- destroy: /* @__PURE__ */ new Set()
506
- };
922
+ this.context = {};
507
923
  this.trackMap = /* @__PURE__ */ new Map();
508
924
  this.proxyProps = signalObject(
509
925
  props,
510
- (key) => startsWith(key, "on") || startsWith(key, "update")
926
+ (key2) => startsWith(key2, "on") || startsWith(key2, "update")
511
927
  );
512
- }
513
- addEventListener() {
514
- }
515
- removeEventListener() {
928
+ this.key = this.key || props.key;
516
929
  }
517
930
  get firstChild() {
518
931
  var _a, _b;
@@ -522,16 +935,6 @@ var _ComponentNode = class _ComponentNode2 {
522
935
  var _a, _b;
523
936
  return (_b = (_a = this.rootNode) == null ? void 0 : _a.isConnected) != null ? _b : false;
524
937
  }
525
- addHook(hook, cb) {
526
- var _a;
527
- (_a = this.hooks[hook]) == null ? void 0 : _a.add(cb);
528
- }
529
- getContext(context) {
530
- return _ComponentNode2.context[context];
531
- }
532
- setContext(context, value) {
533
- _ComponentNode2.context[context] = value;
534
- }
535
938
  inheritNode(node) {
536
939
  this.context = node.context;
537
940
  this.hooks = node.hooks;
@@ -550,9 +953,9 @@ var _ComponentNode = class _ComponentNode2 {
550
953
  if (this.isConnected) {
551
954
  return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
552
955
  }
553
- _ComponentNode2.ref = this;
956
+ this.initRef();
554
957
  this.rootNode = this.template(useReactive(this.proxyProps, ["children"]));
555
- _ComponentNode2.ref = null;
958
+ this.removeRef();
556
959
  this.mounted = true;
557
960
  const mountedNode = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
558
961
  this.hooks.mounted.forEach((handler) => handler());
@@ -582,7 +985,7 @@ var _ComponentNode = class _ComponentNode2 {
582
985
  return track2;
583
986
  }
584
987
  patchProps(props) {
585
- var _a, _b, _c, _d, _e;
988
+ var _a, _b, _c, _d;
586
989
  for (const [key, prop] of Object.entries(props)) {
587
990
  if (startsWith(key, "on") && ((_a = this.rootNode) == null ? void 0 : _a.nodes)) {
588
991
  const event = key.slice(2).toLowerCase();
@@ -590,15 +993,11 @@ var _ComponentNode = class _ComponentNode2 {
590
993
  const cleanup = addEventListener(this.rootNode.nodes[0], event, listener);
591
994
  this.emitter.add(cleanup);
592
995
  } else if (key === "ref") {
593
- if (isSignal(prop)) {
594
- props[key].value = (_b = this.rootNode) == null ? void 0 : _b.nodes[0];
595
- } else if (isFunction(prop)) {
596
- props[key]((_c = this.rootNode) == null ? void 0 : _c.nodes[0]);
597
- }
996
+ props[key].value = (_b = this.rootNode) == null ? void 0 : _b.nodes[0];
598
997
  } else if (startsWith(key, "update")) {
599
998
  props[key] = isSignal(prop) ? prop.value : prop;
600
999
  } else if (key !== "children") {
601
- const newValue = (_e = (_d = this.proxyProps)[key]) != null ? _e : _d[key] = useSignal(prop);
1000
+ const newValue = (_d = (_c = this.proxyProps)[key]) != null ? _d : _c[key] = useSignal(prop);
602
1001
  const track2 = this.getNodeTrack(key);
603
1002
  track2.cleanup = useEffect(() => {
604
1003
  newValue.value = isFunction(prop) ? prop() : prop;
@@ -608,10 +1007,7 @@ var _ComponentNode = class _ComponentNode2 {
608
1007
  this.props = props;
609
1008
  }
610
1009
  };
611
- _ComponentNode.ref = null;
612
- _ComponentNode.context = {};
613
- var ComponentNode = _ComponentNode;
614
- function h(_template, props) {
1010
+ function h(_template, props, key) {
615
1011
  if (isString(_template)) {
616
1012
  if (isHtmlTagName(_template)) {
617
1013
  _template = convertToHtmlTag(_template);
@@ -626,15 +1022,17 @@ function h(_template, props) {
626
1022
  }
627
1023
  _template = template(_template);
628
1024
  }
629
- return isFunction(_template) ? new ComponentNode(_template, props) : new TemplateNode(_template, props);
1025
+ return isFunction(_template) ? new ComponentNode(_template, props, key) : new TemplateNode(_template, props, key);
1026
+ }
1027
+ function isComponent(node) {
1028
+ return node instanceof ComponentNode;
630
1029
  }
631
1030
  function isJsxElement(node) {
632
1031
  return node instanceof ComponentNode || node instanceof TemplateNode;
633
1032
  }
634
1033
  function template(html) {
635
- html = closeHtmlTags(html);
636
1034
  const template2 = document.createElement("template");
637
- template2.innerHTML = html;
1035
+ template2.innerHTML = closeHtmlTags(html);
638
1036
  return template2;
639
1037
  }
640
1038
  function Fragment(props) {
@@ -750,9 +1148,9 @@ function binNode(node, setter) {
750
1148
  });
751
1149
  }
752
1150
  }
753
- var p = Promise.resolve();
754
- function nextTick(fn) {
755
- return fn ? p.then(fn) : p;
1151
+ var p2 = Promise.resolve();
1152
+ function nextTick2(fn) {
1153
+ return fn ? p2.then(fn) : p2;
756
1154
  }
757
1155
  function addEventListener(node, eventName, handler) {
758
1156
  node.addEventListener(eventName, handler);
@@ -807,15 +1205,6 @@ function convertToHtmlTag(tagName) {
807
1205
  return `<${tagName}></${tagName}>`;
808
1206
  }
809
1207
  }
810
- function generateShortId() {
811
- const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
812
- let result = "";
813
- const charactersLength = characters.length;
814
- for (let i = 0; i < 8; i++) {
815
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
816
- }
817
- return result;
818
- }
819
1208
  function patchChildren(parent, childrenMap, nextChildren, before) {
820
1209
  const result = /* @__PURE__ */ new Map();
821
1210
  const children = Array.from(childrenMap.values());
@@ -928,6 +1317,7 @@ var TemplateNode = class _TemplateNode {
928
1317
  this.provides = {};
929
1318
  this.trackMap = /* @__PURE__ */ new Map();
930
1319
  this.parent = null;
1320
+ this.key = this.key || props.key;
931
1321
  }
932
1322
  get firstChild() {
933
1323
  var _a;
@@ -947,7 +1337,6 @@ var TemplateNode = class _TemplateNode {
947
1337
  this.nodes.forEach((node) => insertChild(parent, node, before));
948
1338
  return this.nodes;
949
1339
  }
950
- this.key = this.key || generateShortId();
951
1340
  const cloneNode = this.template.content.cloneNode(true);
952
1341
  const firstChild = cloneNode.firstChild;
953
1342
  if ((_a = firstChild == null ? void 0 : firstChild.hasAttribute) == null ? void 0 : _a.call(firstChild, "_svg_")) {
@@ -1051,11 +1440,7 @@ var TemplateNode = class _TemplateNode {
1051
1440
  });
1052
1441
  }
1053
1442
  } else if (attr === "ref") {
1054
- if (isSignal(props[attr])) {
1055
- props[attr].value = node;
1056
- } else if (isFunction(props[attr])) {
1057
- props[attr](node);
1058
- }
1443
+ props[attr].value = node;
1059
1444
  } else if (startsWith(attr, "on")) {
1060
1445
  const eventName = attr.slice(2).toLocaleLowerCase();
1061
1446
  const track2 = this.getNodeTrack(`${key}:${attr}`);
@@ -1115,80 +1500,148 @@ function patchChild(track2, parent, child, before) {
1115
1500
  function onMount(cb) {
1116
1501
  var _a;
1117
1502
  throwIfOutsideComponent("onMounted");
1118
- (_a = ComponentNode.ref) == null ? void 0 : _a.addHook("mounted", cb);
1503
+ (_a = Hooks.ref) == null ? void 0 : _a.addHook("mounted", cb);
1119
1504
  }
1120
1505
  function onDestroy(cb) {
1121
1506
  var _a;
1122
1507
  throwIfOutsideComponent("onDestroy");
1123
- (_a = ComponentNode.ref) == null ? void 0 : _a.addHook("destroy", cb);
1508
+ (_a = Hooks.ref) == null ? void 0 : _a.addHook("destroy", cb);
1124
1509
  }
1125
- function throwIfOutsideComponent(hook) {
1126
- if (!ComponentNode.ref) {
1510
+ function throwIfOutsideComponent(hook, key) {
1511
+ if (!Hooks.ref) {
1127
1512
  console.error(
1128
- `"${hook}" can only be called within the component function body
1513
+ `"${hook}"(key: ${isSymbol(key) ? key.toString() : key}) can only be called within the component function body
1129
1514
  and cannot be used in asynchronous or deferred calls.`
1130
1515
  );
1131
1516
  }
1132
1517
  }
1133
1518
  function useProvide(key, value) {
1134
1519
  var _a;
1135
- throwIfOutsideComponent("useProvide");
1136
- (_a = ComponentNode.ref) == null ? void 0 : _a.setContext(key, value);
1520
+ throwIfOutsideComponent("useProvide", key);
1521
+ (_a = Hooks.ref) == null ? void 0 : _a.setContext(key, value);
1137
1522
  }
1138
1523
  function useInject(key, defaultValue) {
1139
1524
  var _a;
1140
- throwIfOutsideComponent("useInject");
1141
- return ((_a = ComponentNode.ref) == null ? void 0 : _a.getContext(key)) || defaultValue;
1525
+ throwIfOutsideComponent("useInject", key);
1526
+ return ((_a = Hooks.ref) == null ? void 0 : _a.getContext(key)) || defaultValue;
1527
+ }
1528
+ function useRef() {
1529
+ const ref = shallowSignal(null);
1530
+ return ref;
1531
+ }
1532
+ function generateAttributes(props) {
1533
+ return Object.entries(props).map(([key, value]) => {
1534
+ if (key === "children" || isFunction(value)) return "";
1535
+ return `${key}="${escape(String(value))}"`;
1536
+ }).filter(Boolean).join(" ");
1537
+ }
1538
+ function normalizeProps(props) {
1539
+ Object.keys(props).forEach((propKey) => {
1540
+ if (isFunction(props[propKey])) {
1541
+ delete props[propKey];
1542
+ }
1543
+ if (isSignal(props[propKey])) {
1544
+ props[propKey] = props[propKey].value;
1545
+ }
1546
+ });
1142
1547
  }
1143
- function convertJsonToAttributes(json) {
1144
- return Object.entries(json).map(([key, value]) => `${key}=${JSON.stringify(escape(String(value)))}`).join(" ");
1548
+ function handleChildResult(result, prop, key, tmpl, childNodesMap, path) {
1549
+ if (isSignal(result)) {
1550
+ tmpl.template += result.value;
1551
+ } else if (result instanceof ServerNode) {
1552
+ const mapKey = path ? String(path) : `${key}`;
1553
+ if (!childNodesMap[mapKey]) childNodesMap[mapKey] = [];
1554
+ const childResult = result.mount();
1555
+ childNodesMap[mapKey].push(
1556
+ isFunction(childResult) ? childResult(prop) : isSignal(childResult) ? childResult.value : childResult
1557
+ );
1558
+ } else {
1559
+ tmpl.template += isFunction(result) ? result(prop) : String(result);
1560
+ }
1145
1561
  }
1146
- function renderTemplate(template2, props) {
1147
- if (isFunction(template2)) {
1148
- return template2(props);
1562
+ var ServerNode = class _ServerNode extends Hooks {
1563
+ constructor(template2, props = {}, key) {
1564
+ super();
1565
+ this.template = template2;
1566
+ this.props = props;
1567
+ this.key = key;
1568
+ this.childNodesMap = {};
1569
+ this.processedTemplates = {};
1149
1570
  }
1150
- const templateCollection = Array.isArray(template2) ? template2.reduce((acc, tmpl, index) => {
1151
- acc[index + 1] = { template: tmpl };
1152
- return acc;
1153
- }, {}) : template2;
1154
- const childNodesMap = {};
1155
- const processedTemplates = {};
1156
- if (isObject(templateCollection)) {
1157
- for (const [key, tmpl] of Object.entries(templateCollection)) {
1158
- const prop = props[key];
1159
- if (prop) {
1160
- for (const propKey in prop) {
1161
- if (startsWith(propKey, "on") && isFunction(prop[propKey])) {
1162
- delete prop[propKey];
1163
- }
1164
- }
1571
+ /**
1572
+ * Mount and render the component
1573
+ */
1574
+ mount() {
1575
+ this.initRef();
1576
+ const output = this.render();
1577
+ this.removeRef();
1578
+ return output;
1579
+ }
1580
+ /**
1581
+ * Initialize template entries and props
1582
+ */
1583
+ initTemplates() {
1584
+ const templateCollection = Array.isArray(this.template) ? this.template.reduce((acc, tmpl, index) => {
1585
+ acc[index + 1] = { template: tmpl };
1586
+ return acc;
1587
+ }, {}) : this.template;
1588
+ if (isObject(templateCollection)) {
1589
+ Object.entries(templateCollection).forEach(([key, tmpl]) => {
1590
+ const prop = __spreadValues2({}, this.props[key]);
1591
+ normalizeProps(prop);
1165
1592
  if (prop.children) {
1166
- for (const [child, idx] of prop.children) {
1167
- if (!childNodesMap[idx]) childNodesMap[idx] = [];
1168
- childNodesMap[idx].push(child);
1169
- }
1170
- delete prop.children;
1593
+ prop.children.forEach((item) => {
1594
+ const [child, path] = isArray(item) ? item : [item, null];
1595
+ if (isFunction(child)) {
1596
+ const result = child(prop);
1597
+ handleChildResult(result, prop, key, tmpl, this.childNodesMap, path);
1598
+ } else {
1599
+ tmpl.template += isSignal(child) ? child.value : String(child);
1600
+ }
1601
+ });
1171
1602
  }
1172
- }
1173
- processedTemplates[key] = { template: tmpl.template, props: prop };
1603
+ this.processedTemplates[key] = {
1604
+ template: tmpl.template,
1605
+ props: prop
1606
+ };
1607
+ });
1174
1608
  }
1175
1609
  }
1176
- return Object.entries(processedTemplates).map(([key, { template: tmpl, props: prop }]) => {
1177
- let renderedString = tmpl;
1178
- if (prop) {
1179
- renderedString += ` ${convertJsonToAttributes(prop)}`;
1610
+ /**
1611
+ * Render component and its children into a string
1612
+ */
1613
+ render() {
1614
+ if (isFunction(this.template)) {
1615
+ const root = this.template(this.props);
1616
+ return root instanceof _ServerNode ? root.mount() : String(root);
1180
1617
  }
1181
- if (childNodesMap[key]) {
1182
- renderedString += childNodesMap[key].map((child) => renderTemplate(child, prop)).join("");
1618
+ if (this.template instanceof _ServerNode) {
1619
+ return this.template.mount();
1183
1620
  }
1184
- return renderedString;
1185
- }).join("");
1621
+ this.initTemplates();
1622
+ return Object.entries(this.processedTemplates).map(([key, { template: template2, props }]) => {
1623
+ let content = template2;
1624
+ if (props && Object.keys(props).length > 0) {
1625
+ content += ` ${generateAttributes(props)}`;
1626
+ }
1627
+ if (this.childNodesMap[key]) {
1628
+ content = content.replace("<!>", this.renderChildren(this.childNodesMap[key]));
1629
+ }
1630
+ return content;
1631
+ }).join("");
1632
+ }
1633
+ /**
1634
+ * Render child nodes into a string
1635
+ */
1636
+ renderChildren(children) {
1637
+ return coerceArray(children).map(String).join("");
1638
+ }
1639
+ };
1640
+ function ssg(component, props) {
1641
+ return new ServerNode(component, props);
1186
1642
  }
1187
1643
  function renderToString(component, props) {
1188
- return renderTemplate(component, props);
1189
- }
1190
- function renderSSG(component, root, props = {}) {
1191
- root.innerHTML = renderTemplate(component, props);
1644
+ return ssg(component, props).mount();
1192
1645
  }
1193
1646
 
1194
1647
  // src/index.ts
@@ -1196,19 +1649,19 @@ if (globalThis) {
1196
1649
  globalThis.__essor_version__ = essor_version;
1197
1650
  }
1198
1651
  /**
1199
- * @estjs/shared v0.0.11
1652
+ * @estjs/shared v0.0.12
1200
1653
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
1201
1654
  * @license MIT
1202
1655
  **/
1203
1656
  /**
1204
- * @estjs/signal v0.0.11
1657
+ * @estjs/signal v0.0.12
1205
1658
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
1206
1659
  * @license MIT
1207
1660
  **/
1208
1661
  /**
1209
- * @estjs/template v0.0.11
1662
+ * @estjs/template v0.0.12
1210
1663
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
1211
1664
  * @license MIT
1212
1665
  **/
1213
1666
 
1214
- export { ComponentNode, Fragment, TemplateNode, createStore, essor_version, h, isComputed, isJsxElement, isReactive, isSignal, nextTick, onDestroy, onMount, renderSSG, renderTemplate, renderToString, shallowReactive, shallowSignal, signalObject, template, unReactive, unSignal, useComputed, useEffect, useInject, useProvide, useReactive, useSignal, useWatch };
1667
+ export { ComponentNode, Fragment, TemplateNode, clearReactive, createStore, essor_version, h, isComponent, isComputed, isJsxElement, isReactive, isSignal, nextTick2 as nextTick, onDestroy, onMount, renderToString, shallowReactive, shallowSignal, signalObject, ssg, template, unReactive, unSignal, useBatch, useComputed, useEffect, useInject, useProvide, useReactive, useRef, useSignal, useWatch };