@mittwald/react-tunnel 1.5.45 → 1.5.46

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.js CHANGED
@@ -10,6 +10,11 @@ var m = "default", h = "default", g = class e {
10
10
  committedChildren = d.map({}, { deep: !1 });
11
11
  renderPhaseChildren = /* @__PURE__ */ new Map();
12
12
  nextIndex = 0;
13
+ indexGeneration = 0;
14
+ orderGeneration = 0;
15
+ orderListeners = /* @__PURE__ */ new Set();
16
+ committedIndices = /* @__PURE__ */ new Map();
17
+ isCommittedIndicesResetScheduled = !1;
13
18
  constructor(e = h, t = h) {
14
19
  this.id = e, this.instanceId = t, u(this, {
15
20
  id: !1,
@@ -18,15 +23,41 @@ var m = "default", h = "default", g = class e {
18
23
  });
19
24
  }
20
25
  static useNew(t) {
21
- let n = r(), i = s(() => new e(t, n))[0];
22
- return i.resetIndex(), i;
26
+ let n = r();
27
+ return s(() => new e(t, n))[0];
23
28
  }
24
- resetIndex() {
25
- this.nextIndex = 0;
29
+ subscribeToOrder = (e) => (this.orderListeners.add(e), () => {
30
+ this.orderListeners.delete(e);
31
+ });
32
+ getOrderGeneration = () => this.orderGeneration;
33
+ takeIndex(e) {
34
+ return this.indexGeneration !== e && (this.indexGeneration = e, this.nextIndex = 0), this.nextIndex++;
35
+ }
36
+ isCommittedOutOfOrder(e = m, t, n) {
37
+ if (n !== this.orderGeneration) return !1;
38
+ let r = this.committedIndices.get(e);
39
+ return this.committedIndices.set(e, t), this.isCommittedIndicesResetScheduled || (this.isCommittedIndicesResetScheduled = !0, queueMicrotask(() => {
40
+ this.committedIndices.clear(), this.isCommittedIndicesResetScheduled = !1;
41
+ })), r !== void 0 && t < r;
26
42
  }
27
- useEntryIndex() {
28
- let e = o(this.instanceId), t = o(null);
29
- return (t.current === null || e.current !== this.instanceId) && (e.current = this.instanceId, t.current = this.nextIndex++), t.current;
43
+ useEntryIndex(e = m) {
44
+ let t = c(this.subscribeToOrder, this.getOrderGeneration, this.getOrderGeneration), n = o(null);
45
+ return (n.current?.instanceId !== this.instanceId || n.current.generation !== t) && (n.current = {
46
+ instanceId: this.instanceId,
47
+ generation: t,
48
+ index: this.takeIndex(t),
49
+ mountsNextToCommittedEntries: n.current === null && (this.committedChildren.get(e)?.size ?? 0) > 0
50
+ }), {
51
+ index: n.current.index,
52
+ generation: t,
53
+ needsReorder: n.current.mountsNextToCommittedEntries
54
+ };
55
+ }
56
+ reorderEntries(e) {
57
+ if (e === this.orderGeneration) {
58
+ this.orderGeneration++, this.committedIndices.clear();
59
+ for (let e of this.orderListeners) e();
60
+ }
30
61
  }
31
62
  buildEntryState(e, t, n, r, i) {
32
63
  let a = new Set(e?.owners);
@@ -85,9 +116,9 @@ var m = "default", h = "default", g = class e {
85
116
  children: n
86
117
  });
87
118
  }, b = (e) => {
88
- let { children: t, id: a, staticEntryId: s, providerId: c } = e, l = v(c), u = r(), d = s ?? u, f = l.useEntryIndex(), p = o(!1);
89
- return p.current || l.setRenderPhaseChildren(a, d, u, f, t), i(() => {
90
- p.current = !0, l.setChildren(a, d, u, f, t);
119
+ let { children: t, id: a, staticEntryId: s, providerId: c } = e, l = v(c), u = r(), d = s ?? u, { index: f, generation: p, needsReorder: m } = l.useEntryIndex(a), h = o(!1);
120
+ return h.current || l.setRenderPhaseChildren(a, d, u, f, t), i(() => {
121
+ h.current = !0, l.setChildren(a, d, u, f, t);
91
122
  }, [
92
123
  t,
93
124
  a,
@@ -95,7 +126,10 @@ var m = "default", h = "default", g = class e {
95
126
  u,
96
127
  f,
97
128
  c
98
- ]), n(() => () => {
129
+ ]), i(() => {
130
+ let e = l.isCommittedOutOfOrder(a, f, p);
131
+ (m || e) && l.reorderEntries(p);
132
+ }), n(() => () => {
99
133
  l.deleteChildren(a, d, u);
100
134
  }, [
101
135
  a,
@@ -26,10 +26,42 @@ export declare class TunnelState {
26
26
  readonly committedChildren: ObservableMap<string, ObservableMap<string, TunnelEntryState>>;
27
27
  private readonly renderPhaseChildren;
28
28
  private nextIndex;
29
+ private indexGeneration;
30
+ private orderGeneration;
31
+ private readonly orderListeners;
32
+ private readonly committedIndices;
33
+ private isCommittedIndicesResetScheduled;
29
34
  constructor(id?: string, instanceId?: string);
30
35
  static useNew(id?: string): TunnelState;
31
- resetIndex(): void;
32
- useEntryIndex(): number;
36
+ private readonly subscribeToOrder;
37
+ private readonly getOrderGeneration;
38
+ private takeIndex;
39
+ /**
40
+ * Whether `index` commits after a higher index in the same commit. Called
41
+ * from a layout effect: unlike a concurrent render, a commit does not yield,
42
+ * so the microtask clears the indices only between commits.
43
+ */
44
+ isCommittedOutOfOrder(tunnelId: string | undefined, index: number, generation: number): boolean;
45
+ /**
46
+ * An entry takes its index from a counter while it renders, so entries that
47
+ * render together are ordered like the tree. An entry that mounts next to
48
+ * committed ones only knows the count so far (it returns `needsReorder`), and
49
+ * a keyed reorder keeps the indices of the moved entries
50
+ * (`isCommittedOutOfOrder`). Either way the entry calls `reorderEntries` in a
51
+ * layout effect: every entry of this state renders again in one pass, in tree
52
+ * order, and takes its index anew.
53
+ */
54
+ useEntryIndex(tunnelId?: string): {
55
+ index: number;
56
+ generation: number;
57
+ needsReorder: boolean;
58
+ };
59
+ /**
60
+ * Re-renders every entry so it takes its index anew. `generation` is the one
61
+ * the calling entry rendered with: when a reorder is already pending, the
62
+ * call does nothing, so entries mounting in one commit reorder once.
63
+ */
64
+ reorderEntries(generation: number): void;
33
65
  private buildEntryState;
34
66
  setChildren(tunnelId: string | undefined, entryId: string, ownerId: string, index: number, children: TunnelChildren): void;
35
67
  setRenderPhaseChildren(tunnelId: string | undefined, entryId: string, ownerId: string, index: number, children: TunnelChildren): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mittwald/react-tunnel",
3
- "version": "1.5.45",
3
+ "version": "1.5.46",
4
4
  "type": "module",
5
5
  "description": "It's like a Portal – but with React components",
6
6
  "keywords": [
@@ -39,8 +39,8 @@
39
39
  "mobx-react-lite": "^4.1.1"
40
40
  },
41
41
  "devDependencies": {
42
- "@mittwald/flow-core": "1.5.45",
43
- "@mittwald/typescript-config": "1.5.45",
42
+ "@mittwald/flow-core": "1.5.46",
43
+ "@mittwald/typescript-config": "1.5.46",
44
44
  "@types/node": "^25.9.3",
45
45
  "@types/react": "^19.2",
46
46
  "@types/react-dom": "^19.2",
@@ -62,5 +62,5 @@
62
62
  "react": "^19.2.0",
63
63
  "react-dom": "^19.2.0"
64
64
  },
65
- "gitHead": "be042090497de4dea27dd777bb49869f3c6599ca"
65
+ "gitHead": "4c6d9e48b1ccd4b7851396ce3ed3dde098dde8ec"
66
66
  }