micra.js 2.2.1 → 2.3.1

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/micra.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- /* Micra.js v2.2.1 — https://github.com/micra-js/micra — MIT */
1
+ /* Micra.js v2.3.1 — https://github.com/micra-js/micra — MIT */
2
2
 
3
3
  // src/utils/fetch.ts
4
4
  function getCSRF() {
@@ -192,8 +192,9 @@ function off(event, handler) {
192
192
  set.delete(handler);
193
193
  if (set.size === 0) _bus.delete(event);
194
194
  }
195
- function emit(event, payload) {
195
+ function emit(event, ...args) {
196
196
  var _a;
197
+ const payload = args[0];
197
198
  (_a = _bus.get(event)) == null ? void 0 : _a.forEach((h) => {
198
199
  try {
199
200
  h(payload);
@@ -217,13 +218,14 @@ function createReactiveState(obj, schedule, onKey) {
217
218
  }
218
219
  function createScheduler(render) {
219
220
  let pending = false;
221
+ const flush = () => {
222
+ pending = false;
223
+ render();
224
+ };
220
225
  return function schedule() {
221
226
  if (pending) return;
222
227
  pending = true;
223
- Promise.resolve().then(() => {
224
- pending = false;
225
- render();
226
- });
228
+ queueMicrotask(flush);
227
229
  };
228
230
  }
229
231
 
@@ -289,7 +291,7 @@ function applyModel(el, key, rawState) {
289
291
  const desired = stateVal == null ? "" : String(stateVal);
290
292
  if (html.value !== desired) html.value = desired;
291
293
  }
292
- function applyDirectives(scan, state, rawState, _instance) {
294
+ function applyDirectives(scan, state, rawState) {
293
295
  for (const b of scan.if) applyIf(b, state);
294
296
  for (const b of scan.text) applyText(b.el, b.expr, state);
295
297
  for (const b of scan.html) applyHtml(b.el, b.expr, state);
@@ -500,20 +502,6 @@ function scanComponent(root) {
500
502
  }
501
503
  return scan;
502
504
  }
503
- function scanFragment(frag) {
504
- const scan = emptyScan();
505
- const walker = document.createTreeWalker(
506
- frag,
507
- NodeFilter.SHOW_ELEMENT,
508
- NESTED_COMPONENT_FILTER
509
- );
510
- let node = walker.nextNode();
511
- while (node) {
512
- classify(node, scan);
513
- node = walker.nextNode();
514
- }
515
- return scan;
516
- }
517
505
 
518
506
  // src/dom/each.ts
519
507
  function renderList(templates, state, rawState, instance, triggerKey) {
@@ -544,10 +532,28 @@ function renderList(templates, state, rawState, instance, triggerKey) {
544
532
  if (keyAttr) {
545
533
  renderKeyed(tmpl, items, keyAttr, marker, keyMap, state, rawState, instance, canSkipUnchanged);
546
534
  } else {
547
- renderNoKey(tmpl, items, marker, state, rawState, instance);
535
+ renderNoKey(tmpl, items, marker, state, rawState, instance, canSkipUnchanged);
548
536
  }
549
537
  }
550
538
  }
539
+ function createRowNode(tmpl, state, instance) {
540
+ const frag = tmpl.content.cloneNode(true);
541
+ let node;
542
+ if (frag.childNodes.length === 1) {
543
+ node = frag.firstElementChild;
544
+ } else {
545
+ node = document.createElement("micra-each-item");
546
+ node.style.display = "contents";
547
+ node.append(frag);
548
+ }
549
+ const rowScan = scanComponent(node);
550
+ node.__micraScan = rowScan;
551
+ node._itemState = Object.create(state);
552
+ bindDataOn(rowScan.on, instance);
553
+ bindAtEvents(rowScan.atEvents, instance);
554
+ bindModels(rowScan.model, instance);
555
+ return node;
556
+ }
551
557
  function renderKeyed(tmpl, items, keyAttr, marker, keyMap, state, rawState, instance, canSkipUnchanged) {
552
558
  var _a;
553
559
  const nextKeys = /* @__PURE__ */ new Set();
@@ -567,22 +573,8 @@ function renderKeyed(tmpl, items, keyAttr, marker, keyMap, state, rawState, inst
567
573
  nextKeys.add(key);
568
574
  let node = keyMap.get(key);
569
575
  if (!node) {
570
- const frag = tmpl.content.cloneNode(true);
571
- if (frag.childNodes.length === 1) {
572
- node = frag.firstElementChild;
573
- } else {
574
- node = document.createElement("micra-each-item");
575
- node.style.display = "contents";
576
- node.append(frag);
577
- }
578
- node.__micraKey = key;
576
+ node = createRowNode(tmpl, state, instance);
579
577
  keyMap.set(key, node);
580
- const rowScan2 = scanComponent(node);
581
- node.__micraScan = rowScan2;
582
- bindDataOn(rowScan2.on, instance);
583
- bindAtEvents(rowScan2.atEvents, instance);
584
- bindModels(rowScan2.model, instance);
585
- node._itemState = Object.create(state);
586
578
  } else if (canSkipUnchanged && node.__micraItem === item && node.__micraIndex === index) {
587
579
  nextNodes.push(node);
588
580
  continue;
@@ -594,7 +586,7 @@ function renderKeyed(tmpl, items, keyAttr, marker, keyMap, state, rawState, inst
594
586
  itemState.index = index;
595
587
  itemState.$index = index;
596
588
  const rowScan = (_a = node.__micraScan) != null ? _a : node.__micraScan = scanComponent(node);
597
- applyDirectives(rowScan, itemState, rawState, instance);
589
+ applyDirectives(rowScan, itemState, rawState);
598
590
  nextNodes.push(node);
599
591
  }
600
592
  for (const [key, node] of keyMap) {
@@ -660,29 +652,50 @@ function reorderKeyed(nextNodes, prevList, marker) {
660
652
  anchor = node;
661
653
  }
662
654
  }
663
- function renderNoKey(tmpl, items, marker, state, rawState, instance) {
664
- tmpl.__micraList.forEach((n) => n.remove());
665
- tmpl.__micraList = [];
666
- const frag = document.createDocumentFragment();
667
- for (const [index, item] of items.entries()) {
668
- const clone = tmpl.content.cloneNode(true);
669
- const itemState = Object.assign(
670
- Object.create(state),
671
- { item, index, $index: index }
672
- );
673
- const fragScan = scanFragment(clone);
674
- applyDirectives(fragScan, itemState, rawState, instance);
675
- bindDataOn(fragScan.on, instance);
676
- bindAtEvents(fragScan.atEvents, instance);
677
- bindModels(fragScan.model, instance);
678
- const nodes = Array.from(clone.childNodes);
679
- nodes.forEach((n) => {
680
- n.__micraEach = true;
681
- frag.append(n);
682
- });
683
- tmpl.__micraList.push(...nodes);
684
- }
685
- marker.after(frag);
655
+ function renderNoKey(tmpl, items, marker, state, rawState, instance, canSkipUnchanged) {
656
+ const prevList = tmpl.__micraList;
657
+ const prevLen = prevList.length;
658
+ const nextLen = items.length;
659
+ const reuseLen = nextLen < prevLen ? nextLen : prevLen;
660
+ const nextList = new Array(nextLen);
661
+ for (let i = 0; i < reuseLen; i++) {
662
+ const node = prevList[i];
663
+ const item = items[i];
664
+ if (canSkipUnchanged && node.__micraItem === item && node.__micraIndex === i) {
665
+ nextList[i] = node;
666
+ continue;
667
+ }
668
+ node.__micraItem = item;
669
+ node.__micraIndex = i;
670
+ const itemState = node._itemState;
671
+ itemState.item = item;
672
+ itemState.index = i;
673
+ itemState.$index = i;
674
+ applyDirectives(node.__micraScan, itemState, rawState);
675
+ nextList[i] = node;
676
+ }
677
+ for (let i = nextLen; i < prevLen; i++) {
678
+ prevList[i].remove();
679
+ }
680
+ if (nextLen > prevLen) {
681
+ const frag = document.createDocumentFragment();
682
+ for (let i = prevLen; i < nextLen; i++) {
683
+ const node = createRowNode(tmpl, state, instance);
684
+ const item = items[i];
685
+ const itemState = node._itemState;
686
+ itemState.item = item;
687
+ itemState.index = i;
688
+ itemState.$index = i;
689
+ node.__micraItem = item;
690
+ node.__micraIndex = i;
691
+ applyDirectives(node.__micraScan, itemState, rawState);
692
+ nextList[i] = node;
693
+ frag.append(node);
694
+ }
695
+ const anchor = prevLen > 0 ? nextList[prevLen - 1] : marker;
696
+ anchor.after(frag);
697
+ }
698
+ tmpl.__micraList = nextList;
686
699
  }
687
700
 
688
701
  // src/dom/refs.ts
@@ -774,7 +787,7 @@ function mount(selector, definition) {
774
787
  try {
775
788
  const mRoot2 = root;
776
789
  const scan = (_a2 = mRoot2.__micraScan) != null ? _a2 : mRoot2.__micraScan = scanComponent(root);
777
- applyDirectives(scan, exprState, rawState, instance);
790
+ applyDirectives(scan, exprState, rawState);
778
791
  renderList(scan.each, exprState, rawState, instance, triggerKey);
779
792
  bindDataOn(scan.on, instance);
780
793
  bindAtEvents(scan.atEvents, instance);