@mittwald/react-tunnel 1.6.0-next.6 → 1.6.0-next.61

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";
6
- //#region src/TunnelState.ts
7
- var m = "default", h = "default", g = class e {
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
+ // src/TunnelState.ts
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
  }
@@ -49,74 +80,72 @@ var m = "default", h = "default", g = class e {
49
80
  let i = e.get(t), a = i?.get(n);
50
81
  if (!i || !a) return;
51
82
  let o = new Set(a.owners);
52
- if (o.delete(r), o.size > 0) {
53
- i.set(n, {
54
- ...a,
55
- owners: o
56
- });
57
- return;
58
- }
59
- i.delete(n), i.size === 0 && e.delete(t);
83
+ o.delete(r), o.size > 0 ? i.set(n, {
84
+ ...a,
85
+ owners: o
86
+ }) : (i.delete(n), i.size === 0 && e.delete(t));
60
87
  }
61
- deleteChildren(e = m, t, n) {
88
+ deleteChildren(e = h, t, n) {
62
89
  this.deleteOwnerFromMap(this.committedChildren, e, t, n), this.deleteOwnerFromMap(this.renderPhaseChildren, e, t, n);
63
90
  }
64
- getEntries(e = m, t = !1) {
91
+ getEntries(e = h, t = !1) {
65
92
  let n = this.committedChildren.get(e)?.values(), r = t ? this.renderPhaseChildren.get(e)?.values() : void 0, i = n ?? r;
66
93
  if (i) return {
67
94
  committed: !!n,
68
95
  entries: Array.from(i).sort((e, t) => e.index - t.index)
69
96
  };
70
97
  }
71
- }, _ = e({ state: new g() }), v = (e = h) => {
72
- let n = t(_);
98
+ }, v = e({ state: new _() }), y = (e = g) => {
99
+ let n = t(v);
73
100
  for (; n;) {
74
101
  if (n.state.id === e) return n.state;
75
102
  n = n.parentContext;
76
103
  }
77
104
  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
- },
105
+ }, b = (e) => {
106
+ let { children: n, id: r } = e, i = t(v), o = _.useNew(r), s = a(() => ({
107
+ state: o,
108
+ parentContext: i
109
+ }), [o, i]);
110
+ return /* @__PURE__ */ p(v.Provider, {
111
+ value: s,
85
112
  children: n
86
113
  });
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);
114
+ }, x = (e) => {
115
+ 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);
116
+ return h.current || l.setRenderPhaseChildren(a, d, u, f, t), i(() => {
117
+ h.current = !0, l.setChildren(a, d, u, f, t);
91
118
  }, [
92
119
  t,
93
120
  a,
94
121
  d,
95
122
  u,
96
123
  f,
97
- c
98
- ]), n(() => () => {
124
+ l
125
+ ]), i(() => {
126
+ let e = l.isCommittedOutOfOrder(a, f, p);
127
+ (m || e) && l.reorderEntries(p);
128
+ }), n(() => () => {
99
129
  l.deleteChildren(a, d, u);
100
130
  }, [
101
131
  a,
102
132
  d,
103
133
  u,
104
- c
134
+ l
105
135
  ]), null;
106
- }, x = (e) => {
136
+ }, S = (e) => {
107
137
  let { children: t } = e;
108
138
  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);
139
+ }, C = m((e) => {
140
+ let { children: t, id: n, providerId: r } = e, a = l(() => () => null, () => !1, () => !0), c = s(!0);
111
141
  i(() => {
112
- l.current = !1;
142
+ c.current = !1;
113
143
  }, []);
114
- let u = v(r).getEntries(n, s || l.current), [, d] = a((e) => e + 1, 0), p = u?.committed === !0;
144
+ let u = y(r).getEntries(n, a || c.current), [, d] = o((e) => e + 1, 0), f = u?.committed === !0;
115
145
  i(() => {
116
- p || d();
117
- }, [p]);
118
- let m = u?.entries.map((e) => /* @__PURE__ */ f(x, { children: e.children }, e.id));
146
+ f || d();
147
+ }, [f]);
148
+ let m = u?.entries.map((e) => /* @__PURE__ */ p(S, { children: e.children }, e.id));
119
149
  return typeof t == "function" ? t(m) : m ?? t;
120
150
  });
121
- //#endregion
122
- export { b as TunnelEntry, S as TunnelExit, y as TunnelProvider };
151
+ 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.6",
3
+ "version": "1.6.0-next.61",
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.6",
43
- "@mittwald/typescript-config": "1.6.0-next.6",
42
+ "@mittwald/flow-core": "1.6.0-next.61",
43
+ "@mittwald/typescript-config": "1.6.0-next.61",
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": "57e6b5f819583776a48636c85b2a83992b4c6427"
65
+ "gitHead": "b1f0f85be435468902a4eba166351f81cf54eef5"
66
66
  }