@mittwald/react-tunnel 1.6.0-next.5 → 1.6.0-next.50

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
@@ -1,32 +1,63 @@
1
1
 
2
- import { createContext as e, useContext as t, useEffect as n, useId as r, useLayoutEffect as i, useReducer as a, useRef as o, useState as s, useSyncExternalStore as c } from "react";
3
- import { action as l, makeObservable as u, observable as d } from "mobx";
4
- import { jsx as f } from "react/jsx-runtime";
5
- import { observer as p } from "mobx-react-lite";
2
+ import { createContext as e, useContext as t, useEffect as n, useId as r, useLayoutEffect as i, useMemo as a, useReducer as o, useRef as s, useState as c, useSyncExternalStore as l } from "react";
3
+ import { action as u, makeObservable as d, observable as f } from "mobx";
4
+ import { jsx as p } from "react/jsx-runtime";
5
+ import { observer as m } from "mobx-react-lite";
6
6
  //#region src/TunnelState.ts
7
- var m = "default", h = "default", g = class e {
7
+ var h = "default", g = "default", _ = class e {
8
8
  id;
9
9
  instanceId;
10
- committedChildren = d.map({}, { deep: !1 });
10
+ committedChildren = f.map({}, { deep: !1 });
11
11
  renderPhaseChildren = /* @__PURE__ */ new Map();
12
12
  nextIndex = 0;
13
- constructor(e = h, t = h) {
14
- this.id = e, this.instanceId = t, u(this, {
13
+ indexGeneration = 0;
14
+ orderGeneration = 0;
15
+ orderListeners = /* @__PURE__ */ new Set();
16
+ committedIndices = /* @__PURE__ */ new Map();
17
+ isCommittedIndicesResetScheduled = !1;
18
+ constructor(e = g, t = g) {
19
+ this.id = e, this.instanceId = t, d(this, {
15
20
  id: !1,
16
- deleteChildren: l.bound,
17
- setChildren: l.bound
21
+ deleteChildren: u.bound,
22
+ setChildren: u.bound
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 c(() => 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 = h, 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 = h) {
44
+ let t = l(this.subscribeToOrder, this.getOrderGeneration, this.getOrderGeneration), n = s(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);
@@ -37,11 +68,11 @@ var m = "default", h = "default", g = class e {
37
68
  owners: a
38
69
  };
39
70
  }
40
- setChildren(e = m, t, n, r, i) {
41
- let a = this.committedChildren.get(e) ?? d.map({}, { deep: !1 });
71
+ setChildren(e = h, t, n, r, i) {
72
+ let a = this.committedChildren.get(e) ?? f.map({}, { deep: !1 });
42
73
  a.set(t, this.buildEntryState(a.get(t), t, n, r, i)), this.deleteOwnerFromMap(this.renderPhaseChildren, e, t, n), this.committedChildren.set(e, a);
43
74
  }
44
- setRenderPhaseChildren(e = m, t, n, r, i) {
75
+ setRenderPhaseChildren(e = h, t, n, r, i) {
45
76
  let a = this.renderPhaseChildren.get(e) ?? /* @__PURE__ */ new Map();
46
77
  a.set(t, this.buildEntryState(a.get(t), t, n, r, i)), this.renderPhaseChildren.set(e, a);
47
78
  }
@@ -58,36 +89,36 @@ var m = "default", h = "default", g = class e {
58
89
  }
59
90
  i.delete(n), i.size === 0 && e.delete(t);
60
91
  }
61
- deleteChildren(e = m, t, n) {
92
+ deleteChildren(e = h, t, n) {
62
93
  this.deleteOwnerFromMap(this.committedChildren, e, t, n), this.deleteOwnerFromMap(this.renderPhaseChildren, e, t, n);
63
94
  }
64
- getEntries(e = m, t = !1) {
95
+ getEntries(e = h, t = !1) {
65
96
  let n = this.committedChildren.get(e)?.values(), r = t ? this.renderPhaseChildren.get(e)?.values() : void 0, i = n ?? r;
66
97
  if (i) return {
67
98
  committed: !!n,
68
99
  entries: Array.from(i).sort((e, t) => e.index - t.index)
69
100
  };
70
101
  }
71
- }, _ = e({ state: new g() }), v = (e = h) => {
72
- let n = t(_);
102
+ }, v = e({ state: new _() }), y = (e = g) => {
103
+ let n = t(v);
73
104
  for (; n;) {
74
105
  if (n.state.id === e) return n.state;
75
106
  n = n.parentContext;
76
107
  }
77
108
  throw Error(`Could not get tunnel for provider ${e}. Please provider a TunnelProvider with this ID.`);
78
- }, y = (e) => {
79
- let { children: n, id: r } = e, i = t(_), a = g.useNew(r);
80
- return /* @__PURE__ */ f(_.Provider, {
81
- value: {
82
- state: a,
83
- parentContext: i
84
- },
109
+ }, b = (e) => {
110
+ let { children: n, id: r } = e, i = t(v), o = _.useNew(r), s = a(() => ({
111
+ state: o,
112
+ parentContext: i
113
+ }), [o, i]);
114
+ return /* @__PURE__ */ p(v.Provider, {
115
+ value: s,
85
116
  children: n
86
117
  });
87
- }, 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);
118
+ }, x = (e) => {
119
+ let { children: t, id: a, staticEntryId: o, providerId: c } = e, l = y(c), u = r(), d = o ?? u, { index: f, generation: p, needsReorder: m } = l.useEntryIndex(a), h = s(!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,
@@ -103,20 +137,20 @@ var m = "default", h = "default", g = class e {
103
137
  u,
104
138
  c
105
139
  ]), null;
106
- }, x = (e) => {
140
+ }, S = (e) => {
107
141
  let { children: t } = e;
108
142
  return typeof t == "function" ? t() : t;
109
- }, S = p((e) => {
110
- let { children: t, id: n, providerId: r } = e, s = c(() => () => null, () => !1, () => !0), l = o(!0);
143
+ }, C = m((e) => {
144
+ let { children: t, id: n, providerId: r } = e, a = l(() => () => null, () => !1, () => !0), c = s(!0);
111
145
  i(() => {
112
- l.current = !1;
146
+ c.current = !1;
113
147
  }, []);
114
- let u = v(r).getEntries(n, s || l.current), [, d] = a((e) => e + 1, 0), p = u?.committed === !0;
148
+ let u = y(r).getEntries(n, a || c.current), [, d] = o((e) => e + 1, 0), f = u?.committed === !0;
115
149
  i(() => {
116
- p || d();
117
- }, [p]);
118
- let m = u?.entries.map((e) => /* @__PURE__ */ f(x, { children: e.children }, e.id));
150
+ f || d();
151
+ }, [f]);
152
+ let m = u?.entries.map((e) => /* @__PURE__ */ p(S, { children: e.children }, e.id));
119
153
  return typeof t == "function" ? t(m) : m ?? t;
120
154
  });
121
155
  //#endregion
122
- export { b as TunnelEntry, S as TunnelExit, y as TunnelProvider };
156
+ export { x as TunnelEntry, C as TunnelExit, b as TunnelProvider };
@@ -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.6.0-next.5",
3
+ "version": "1.6.0-next.50",
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.6.0-next.5",
43
- "@mittwald/typescript-config": "1.6.0-next.5",
42
+ "@mittwald/flow-core": "1.6.0-next.50",
43
+ "@mittwald/typescript-config": "1.6.0-next.50",
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": "740150500bab2cd36aa92f13894892e4629f8f7c"
65
+ "gitHead": "a0fb5378707e92fe9a240a3c453b549cd2499734"
66
66
  }