thunderous 2.0.6 → 2.0.8

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 CHANGED
@@ -136,6 +136,14 @@ var createEffect = (fn) => {
136
136
 
137
137
  // src/utilities.ts
138
138
  var NOOP = () => void 0;
139
+ var queryComment = (node, comment) => {
140
+ for (const child of node.childNodes) {
141
+ if (child.nodeType === Node.COMMENT_NODE && child.nodeValue === comment) {
142
+ return child;
143
+ }
144
+ }
145
+ return null;
146
+ };
139
147
 
140
148
  // src/server-side.ts
141
149
  var isServer = typeof window === "undefined";
@@ -287,12 +295,12 @@ var logValueError = (value) => {
287
295
  value
288
296
  );
289
297
  };
290
- var arrayToDocumentFragment = (array, parent) => {
298
+ var arrayToDocumentFragment = (array, parent, uniqueKey) => {
291
299
  const documentFragment = new DocumentFragment();
292
300
  let count = 0;
293
301
  const keys = /* @__PURE__ */ new Set();
294
302
  for (const item of array) {
295
- const node = createNewNode(item, parent);
303
+ const node = createNewNode(item, parent, uniqueKey);
296
304
  if (node instanceof DocumentFragment) {
297
305
  const child = node.firstElementChild;
298
306
  if (node.children.length > 1) {
@@ -322,11 +330,13 @@ var arrayToDocumentFragment = (array, parent) => {
322
330
  }
323
331
  documentFragment.append(node);
324
332
  }
333
+ const comment = document.createComment(uniqueKey);
334
+ documentFragment.append(comment);
325
335
  return documentFragment;
326
336
  };
327
- var createNewNode = (value, parent) => {
337
+ var createNewNode = (value, parent, uniqueKey) => {
328
338
  if (typeof value === "string") return new Text(value);
329
- if (Array.isArray(value)) return arrayToDocumentFragment(value, parent);
339
+ if (Array.isArray(value)) return arrayToDocumentFragment(value, parent, uniqueKey);
330
340
  if (value instanceof DocumentFragment) return value;
331
341
  return new Text("");
332
342
  };
@@ -366,7 +376,8 @@ var evaluateBindings = (element, fragment) => {
366
376
  const uniqueKey = text.replace(/\{\{signal:(.+)\}\}/, "$1");
367
377
  const signal = uniqueKey !== text ? renderState.signalMap.get(uniqueKey) : void 0;
368
378
  const newValue = signal !== void 0 ? signal() : text;
369
- const newNode = createNewNode(newValue, element);
379
+ const newNode = createNewNode(newValue, element, uniqueKey);
380
+ const clone = newNode.cloneNode(true);
370
381
  if (i === 0) {
371
382
  child.replaceWith(newNode);
372
383
  } else {
@@ -380,13 +391,12 @@ var evaluateBindings = (element, fragment) => {
380
391
  let init = false;
381
392
  createEffect(() => {
382
393
  const result = signal();
383
- const nextNode = createNewNode(result, element);
394
+ const nextNode = createNewNode(result, element, uniqueKey);
384
395
  if (nextNode instanceof Text) {
385
396
  throw new TypeError(
386
397
  "Signal mismatch: expected DocumentFragment or Array<DocumentFragment>, but got Text"
387
398
  );
388
399
  }
389
- let lastSibling = element.lastChild;
390
400
  for (const child2 of element.children) {
391
401
  const key = child2.getAttribute("key");
392
402
  if (key === null) continue;
@@ -395,6 +405,7 @@ var evaluateBindings = (element, fragment) => {
395
405
  child2.remove();
396
406
  }
397
407
  }
408
+ let anchor = queryComment(element, uniqueKey);
398
409
  for (const child2 of nextNode.children) {
399
410
  const key = child2.getAttribute("key");
400
411
  const matchingNode = element.querySelector(`[key="${key}"]`);
@@ -404,10 +415,12 @@ var evaluateBindings = (element, fragment) => {
404
415
  matchingNode.setAttribute(attr.name, attr.value);
405
416
  }
406
417
  matchingNode.replaceChildren(...child2.childNodes);
407
- lastSibling = matchingNode.nextSibling;
418
+ anchor = matchingNode.nextSibling;
408
419
  child2.replaceWith(matchingNode);
409
420
  }
410
- element.insertBefore(nextNode, lastSibling);
421
+ const nextAnchor = queryComment(nextNode, uniqueKey);
422
+ nextAnchor?.remove();
423
+ element.insertBefore(nextNode, anchor);
411
424
  if (!init) init = true;
412
425
  });
413
426
  }
@@ -751,7 +764,7 @@ You must set an initial value before calling a property signal's getter.
751
764
  }
752
765
  constructor() {
753
766
  super();
754
- if (Object.prototype.hasOwnProperty.call(this, "__customCallbackFns")) {
767
+ if (!Object.prototype.hasOwnProperty.call(this, "__customCallbackFns")) {
755
768
  this.__customCallbackFns = /* @__PURE__ */ new Map();
756
769
  }
757
770
  for (const [attrName, attr] of this.#attributesAsPropertiesMap) {
package/dist/index.js CHANGED
@@ -101,6 +101,14 @@ var createEffect = (fn) => {
101
101
 
102
102
  // src/utilities.ts
103
103
  var NOOP = () => void 0;
104
+ var queryComment = (node, comment) => {
105
+ for (const child of node.childNodes) {
106
+ if (child.nodeType === Node.COMMENT_NODE && child.nodeValue === comment) {
107
+ return child;
108
+ }
109
+ }
110
+ return null;
111
+ };
104
112
 
105
113
  // src/server-side.ts
106
114
  var isServer = typeof window === "undefined";
@@ -252,12 +260,12 @@ var logValueError = (value) => {
252
260
  value
253
261
  );
254
262
  };
255
- var arrayToDocumentFragment = (array, parent) => {
263
+ var arrayToDocumentFragment = (array, parent, uniqueKey) => {
256
264
  const documentFragment = new DocumentFragment();
257
265
  let count = 0;
258
266
  const keys = /* @__PURE__ */ new Set();
259
267
  for (const item of array) {
260
- const node = createNewNode(item, parent);
268
+ const node = createNewNode(item, parent, uniqueKey);
261
269
  if (node instanceof DocumentFragment) {
262
270
  const child = node.firstElementChild;
263
271
  if (node.children.length > 1) {
@@ -287,11 +295,13 @@ var arrayToDocumentFragment = (array, parent) => {
287
295
  }
288
296
  documentFragment.append(node);
289
297
  }
298
+ const comment = document.createComment(uniqueKey);
299
+ documentFragment.append(comment);
290
300
  return documentFragment;
291
301
  };
292
- var createNewNode = (value, parent) => {
302
+ var createNewNode = (value, parent, uniqueKey) => {
293
303
  if (typeof value === "string") return new Text(value);
294
- if (Array.isArray(value)) return arrayToDocumentFragment(value, parent);
304
+ if (Array.isArray(value)) return arrayToDocumentFragment(value, parent, uniqueKey);
295
305
  if (value instanceof DocumentFragment) return value;
296
306
  return new Text("");
297
307
  };
@@ -331,7 +341,8 @@ var evaluateBindings = (element, fragment) => {
331
341
  const uniqueKey = text.replace(/\{\{signal:(.+)\}\}/, "$1");
332
342
  const signal = uniqueKey !== text ? renderState.signalMap.get(uniqueKey) : void 0;
333
343
  const newValue = signal !== void 0 ? signal() : text;
334
- const newNode = createNewNode(newValue, element);
344
+ const newNode = createNewNode(newValue, element, uniqueKey);
345
+ const clone = newNode.cloneNode(true);
335
346
  if (i === 0) {
336
347
  child.replaceWith(newNode);
337
348
  } else {
@@ -345,13 +356,12 @@ var evaluateBindings = (element, fragment) => {
345
356
  let init = false;
346
357
  createEffect(() => {
347
358
  const result = signal();
348
- const nextNode = createNewNode(result, element);
359
+ const nextNode = createNewNode(result, element, uniqueKey);
349
360
  if (nextNode instanceof Text) {
350
361
  throw new TypeError(
351
362
  "Signal mismatch: expected DocumentFragment or Array<DocumentFragment>, but got Text"
352
363
  );
353
364
  }
354
- let lastSibling = element.lastChild;
355
365
  for (const child2 of element.children) {
356
366
  const key = child2.getAttribute("key");
357
367
  if (key === null) continue;
@@ -360,6 +370,7 @@ var evaluateBindings = (element, fragment) => {
360
370
  child2.remove();
361
371
  }
362
372
  }
373
+ let anchor = queryComment(element, uniqueKey);
363
374
  for (const child2 of nextNode.children) {
364
375
  const key = child2.getAttribute("key");
365
376
  const matchingNode = element.querySelector(`[key="${key}"]`);
@@ -369,10 +380,12 @@ var evaluateBindings = (element, fragment) => {
369
380
  matchingNode.setAttribute(attr.name, attr.value);
370
381
  }
371
382
  matchingNode.replaceChildren(...child2.childNodes);
372
- lastSibling = matchingNode.nextSibling;
383
+ anchor = matchingNode.nextSibling;
373
384
  child2.replaceWith(matchingNode);
374
385
  }
375
- element.insertBefore(nextNode, lastSibling);
386
+ const nextAnchor = queryComment(nextNode, uniqueKey);
387
+ nextAnchor?.remove();
388
+ element.insertBefore(nextNode, anchor);
376
389
  if (!init) init = true;
377
390
  });
378
391
  }
@@ -716,7 +729,7 @@ You must set an initial value before calling a property signal's getter.
716
729
  }
717
730
  constructor() {
718
731
  super();
719
- if (Object.prototype.hasOwnProperty.call(this, "__customCallbackFns")) {
732
+ if (!Object.prototype.hasOwnProperty.call(this, "__customCallbackFns")) {
720
733
  this.__customCallbackFns = /* @__PURE__ */ new Map();
721
734
  }
722
735
  for (const [attrName, attr] of this.#attributesAsPropertiesMap) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thunderous",
3
- "version": "2.0.6",
3
+ "version": "2.0.8",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",