@stream-mdx/react 0.5.0 → 1.0.0

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.
Files changed (99) hide show
  1. package/CHANGELOG.md +371 -0
  2. package/README.md +79 -4
  3. package/dist/blocks-renderer-DAOBFBfi.d.ts +29 -0
  4. package/dist/blocks-renderer-DQEL1kJW.d.cts +29 -0
  5. package/dist/components/bottom-stick-scroll-area.cjs +117 -193
  6. package/dist/components/bottom-stick-scroll-area.mjs +112 -183
  7. package/dist/components/diff-renderer.cjs +86 -0
  8. package/dist/components/diff-renderer.d.cts +11 -0
  9. package/dist/components/diff-renderer.d.ts +11 -0
  10. package/dist/components/diff-renderer.mjs +66 -0
  11. package/dist/components/index.cjs +2761 -1508
  12. package/dist/components/index.d.cts +7 -2
  13. package/dist/components/index.d.ts +7 -2
  14. package/dist/components/index.mjs +2730 -1493
  15. package/dist/components/katex-math.cjs +220 -0
  16. package/dist/components/katex-math.d.cts +10 -0
  17. package/dist/components/katex-math.d.ts +10 -0
  18. package/dist/components/katex-math.mjs +188 -0
  19. package/dist/contexts/math-tracker.cjs +240 -521
  20. package/dist/contexts/math-tracker.mjs +235 -513
  21. package/dist/index-Cmne1yUK.d.cts +723 -0
  22. package/dist/index-D7dyyu3q.d.ts +723 -0
  23. package/dist/index.cjs +13029 -6475
  24. package/dist/index.d.cts +69 -39
  25. package/dist/index.d.ts +69 -39
  26. package/dist/index.mjs +13007 -6442
  27. package/dist/math/display-wrapper.cjs +7 -15
  28. package/dist/math/display-wrapper.d.cts +2 -2
  29. package/dist/math/display-wrapper.d.ts +2 -2
  30. package/dist/math/display-wrapper.mjs +2 -6
  31. package/dist/node-views-Bpc6Gkgl.d.cts +13 -0
  32. package/dist/node-views-DpnYdaAS.d.ts +13 -0
  33. package/dist/renderer/hooks.cjs +35 -20
  34. package/dist/renderer/hooks.d.cts +22 -1
  35. package/dist/renderer/hooks.d.ts +22 -1
  36. package/dist/renderer/hooks.mjs +27 -12
  37. package/dist/renderer/patch-batching.cjs +5 -8
  38. package/dist/renderer/patch-coalescing.cjs +5 -8
  39. package/dist/renderer/patch-commit-scheduler.cjs +138 -356
  40. package/dist/renderer/patch-commit-scheduler.d.cts +13 -1
  41. package/dist/renderer/patch-commit-scheduler.d.ts +13 -1
  42. package/dist/renderer/patch-commit-scheduler.mjs +133 -348
  43. package/dist/renderer/store.cjs +3775 -1373
  44. package/dist/renderer/store.d.cts +108 -3
  45. package/dist/renderer/store.d.ts +108 -3
  46. package/dist/renderer/store.mjs +3778 -1362
  47. package/dist/renderer/virtualized-code.cjs +415 -71
  48. package/dist/renderer/virtualized-code.d.cts +24 -23
  49. package/dist/renderer/virtualized-code.d.ts +24 -23
  50. package/dist/renderer/virtualized-code.mjs +409 -62
  51. package/dist/renderer.cjs +10655 -4334
  52. package/dist/renderer.d.cts +98 -29
  53. package/dist/renderer.d.ts +98 -29
  54. package/dist/renderer.mjs +10582 -4212
  55. package/dist/server-regions-CPLHGli0.d.cts +32 -0
  56. package/dist/server-regions-mUhyoKi7.d.ts +32 -0
  57. package/dist/server.cjs +10195 -3872
  58. package/dist/server.d.cts +17 -252
  59. package/dist/server.d.ts +17 -252
  60. package/dist/server.mjs +9997 -3649
  61. package/dist/{streaming-markdown-CEI9eDZm.d.ts → streaming-markdown-yELbJxyH.d.ts} +49 -55
  62. package/dist/{streaming-markdown-DRisClXb.d.cts → streaming-markdown-zVwFuaJG.d.cts} +49 -55
  63. package/dist/streaming-markdown.cjs +11660 -5378
  64. package/dist/streaming-markdown.d.cts +7 -2
  65. package/dist/streaming-markdown.d.ts +7 -2
  66. package/dist/streaming-markdown.mjs +11679 -5347
  67. package/dist/trusted-mdx/index.cjs +833 -0
  68. package/dist/trusted-mdx/index.d.cts +11 -0
  69. package/dist/trusted-mdx/index.d.ts +11 -0
  70. package/dist/trusted-mdx/index.mjs +807 -0
  71. package/dist/trusted-mdx/native-children-slot.cjs +45 -0
  72. package/dist/trusted-mdx/native-children-slot.d.cts +11 -0
  73. package/dist/trusted-mdx/native-children-slot.d.ts +11 -0
  74. package/dist/trusted-mdx/native-children-slot.mjs +24 -0
  75. package/dist/trusted-mdx/server.cjs +752 -0
  76. package/dist/trusted-mdx/server.d.cts +24 -0
  77. package/dist/trusted-mdx/server.d.ts +24 -0
  78. package/dist/trusted-mdx/server.mjs +724 -0
  79. package/dist/trusted-mdx-contract-ElKpkDI5.d.cts +113 -0
  80. package/dist/trusted-mdx-contract-ElKpkDI5.d.ts +113 -0
  81. package/dist/utils/inline-html.cjs +246 -147
  82. package/dist/utils/inline-html.d.cts +37 -6
  83. package/dist/utils/inline-html.d.ts +37 -6
  84. package/dist/utils/inline-html.mjs +237 -134
  85. package/package.json +41 -18
  86. package/dist/index-BVhp_pcF.d.ts +0 -251
  87. package/dist/index-EaeouYp2.d.cts +0 -251
  88. package/dist/mdx-client.cjs +0 -408
  89. package/dist/mdx-client.d.cts +0 -113
  90. package/dist/mdx-client.d.ts +0 -113
  91. package/dist/mdx-client.mjs +0 -369
  92. package/dist/mdx-coordinator.cjs +0 -584
  93. package/dist/mdx-coordinator.d.cts +0 -21
  94. package/dist/mdx-coordinator.d.ts +0 -21
  95. package/dist/mdx-coordinator.mjs +0 -550
  96. package/dist/renderer/node-views.cjs +0 -1887
  97. package/dist/renderer/node-views.d.cts +0 -14
  98. package/dist/renderer/node-views.d.ts +0 -14
  99. package/dist/renderer/node-views.mjs +0 -1861
@@ -13,36 +13,56 @@ import {
13
13
  } from "@stream-mdx/core";
14
14
 
15
15
  // src/renderer/patch-commit-scheduler.ts
16
- var DEFAULT_TIMEOUT_MS = 16;
17
- var COALESCING_DURATION_SAMPLE_LIMIT = 60;
18
- var COALESCING_DURATION_ACTIVATE_MS = 6;
19
- var COALESCING_DURATION_DEACTIVATE_MS = 4;
20
- var RETAIN_TOUCHED_IN_HISTORY = typeof process !== "undefined" && process.env ? process.env.NODE_ENV !== "production" : true;
21
- var EMPTY_TOUCHED_SET = /* @__PURE__ */ new Set();
16
+ var DEFAULT_TIMEOUT_MS = 16, COALESCING_DURATION_SAMPLE_LIMIT = 60, COALESCING_DURATION_ACTIVATE_MS = 6, COALESCING_DURATION_DEACTIVATE_MS = 4, RETAIN_TOUCHED_IN_HISTORY = typeof process < "u" && process.env ? process.env.NODE_ENV !== "production" : !0, EMPTY_TOUCHED_SET = /* @__PURE__ */ new Set();
17
+ function semanticEnvelopeKeyForPatch(patch) {
18
+ if (getPatchKind(patch) !== "semantic") return;
19
+ let meta = patch.op === "setHTML" ? patch.patchMeta : patch.meta, streamSeq = meta?.streamSeq, parseEpoch = meta?.parseEpoch, tx = meta?.tx;
20
+ if (!(typeof streamSeq != "number" || !Number.isFinite(streamSeq) || streamSeq < 0 || typeof parseEpoch != "number" || !Number.isFinite(parseEpoch) || parseEpoch < 0 || typeof tx != "number" || !Number.isFinite(tx) || tx < 0))
21
+ return `${streamSeq}/${parseEpoch}/${tx}`;
22
+ }
22
23
  function shouldSkipCoalescing(patches) {
23
- return patches.length <= 1;
24
+ if (patches.length <= 1) return !0;
25
+ let firstEnvelopeKey, hasEnvelopeKey = !1, hasMultipleEnvelopes = !1, hasUnsequencedSemantic = !1, hasEnrichment = !1, observePatch = (patch) => {
26
+ if (getPatchKind(patch) === "enrichment") {
27
+ hasEnrichment = !0;
28
+ return;
29
+ }
30
+ let key = semanticEnvelopeKeyForPatch(patch);
31
+ if (key === void 0) {
32
+ hasUnsequencedSemantic = !0;
33
+ return;
34
+ }
35
+ hasEnvelopeKey && firstEnvelopeKey !== key && (hasMultipleEnvelopes = !0), hasEnvelopeKey || (firstEnvelopeKey = key, hasEnvelopeKey = !0);
36
+ };
37
+ for (let patch of patches)
38
+ if (patch.op === "setPropsBatch")
39
+ for (let entry of patch.entries ?? [])
40
+ !entry || !entry.at || observePatch({
41
+ op: "setProps",
42
+ at: entry.at,
43
+ props: entry.props,
44
+ meta: { ...patch.meta ?? {}, ...entry.meta ?? {} }
45
+ });
46
+ else
47
+ observePatch(patch);
48
+ return hasEnvelopeKey && (hasMultipleEnvelopes || hasUnsequencedSemantic || hasEnrichment);
24
49
  }
25
50
  function getDefaultNow() {
26
- if (typeof performance !== "undefined" && typeof performance.now === "function") {
27
- return () => performance.now();
28
- }
29
- return () => Date.now();
51
+ return typeof performance < "u" && typeof performance.now == "function" ? () => performance.now() : () => Date.now();
30
52
  }
31
53
  function getDefaultRaf() {
32
- if (typeof globalThis !== "undefined") {
33
- const candidate = globalThis.requestAnimationFrame;
34
- if (typeof candidate === "function") {
54
+ if (typeof globalThis < "u") {
55
+ let candidate = globalThis.requestAnimationFrame;
56
+ if (typeof candidate == "function")
35
57
  return candidate.bind(globalThis);
36
- }
37
58
  }
38
59
  return null;
39
60
  }
40
61
  function getDefaultCancelRaf() {
41
- if (typeof globalThis !== "undefined") {
42
- const candidate = globalThis.cancelAnimationFrame;
43
- if (typeof candidate === "function") {
62
+ if (typeof globalThis < "u") {
63
+ let candidate = globalThis.cancelAnimationFrame;
64
+ if (typeof candidate == "function")
44
65
  return candidate.bind(globalThis);
45
- }
46
66
  }
47
67
  return null;
48
68
  }
@@ -51,97 +71,52 @@ var PatchCommitScheduler = class {
51
71
  this.semanticQueue = [];
52
72
  this.enrichmentHighQueue = [];
53
73
  this.enrichmentLowQueue = [];
54
- this.scheduled = false;
55
- this.flushing = false;
74
+ this.scheduled = !1;
75
+ this.flushing = !1;
56
76
  this.scheduledHandle = null;
57
- this.scheduledViaRaf = false;
77
+ this.scheduledViaRaf = !1;
58
78
  this.sequence = 0;
59
79
  this.idleResolvers = [];
60
- this.paused = false;
80
+ this.paused = !1;
61
81
  this.history = [];
62
82
  this.scheduleToken = 0;
63
83
  this.coalescingDurationSamples = [];
64
- this.adaptiveBudgetActive = false;
84
+ this.adaptiveBudgetActive = !1;
65
85
  this.flushCount = 0;
66
- this.store = params.store;
67
- this.onFlush = params.onFlush;
68
- this.frameBudgetMs = Math.max(1, params.options?.frameBudgetMs ?? 10);
69
- this.maxBatchesPerFlush = params.options?.maxBatchesPerFlush ?? 12;
70
- this.now = params.options?.now ?? getDefaultNow();
71
- this.raf = params.options?.raf ?? getDefaultRaf();
72
- this.cancelRaf = params.options?.cancelRaf ?? getDefaultCancelRaf();
73
- this.timeoutMs = Math.max(1, params.options?.timeoutMs ?? DEFAULT_TIMEOUT_MS);
74
- this.lowPriorityFrameBudgetMs = Math.max(
86
+ this.store = params.store, this.onFlush = params.onFlush, this.isGenerationCurrent = params.isGenerationCurrent, this.frameBudgetMs = Math.max(1, params.options?.frameBudgetMs ?? 4), this.maxBatchesPerFlush = params.options?.maxBatchesPerFlush ?? 12, this.now = params.options?.now ?? getDefaultNow(), this.raf = params.options?.raf ?? getDefaultRaf(), this.cancelRaf = params.options?.cancelRaf ?? getDefaultCancelRaf(), this.timeoutMs = Math.max(1, params.options?.timeoutMs ?? DEFAULT_TIMEOUT_MS), this.lowPriorityFrameBudgetMs = Math.max(
75
87
  1,
76
88
  params.options?.lowPriorityFrameBudgetMs ?? Math.max(2, Math.floor(this.frameBudgetMs * 0.6))
77
- );
78
- this.maxLowPriorityBatchesPerFlush = params.options?.maxLowPriorityBatchesPerFlush ?? 2;
79
- this.urgentQueueThreshold = Math.max(1, params.options?.urgentQueueThreshold ?? 4);
80
- this.historyLimit = Math.max(1, params.options?.historyLimit ?? 200);
81
- this.startupMicrotaskFlushes = Math.max(0, Math.floor(params.options?.startupMicrotaskFlushes ?? 0));
82
- this.adaptiveBudgeting = params.options?.adaptiveBudgeting ?? true;
83
- const requestedBatch = params.options?.batch;
84
- if (requestedBatch === "microtask") {
85
- this.batchStrategy = "microtask";
86
- } else if (requestedBatch === "timeout") {
87
- this.batchStrategy = "timeout";
88
- } else if (requestedBatch === "rAF") {
89
- this.batchStrategy = this.raf ? "rAF" : "timeout";
90
- } else {
91
- this.batchStrategy = "microtask";
92
- }
93
- if (this.batchStrategy === "microtask" && typeof queueMicrotask !== "function") {
94
- this.batchStrategy = this.raf ? "rAF" : "timeout";
95
- }
89
+ ), this.maxLowPriorityBatchesPerFlush = params.options?.maxLowPriorityBatchesPerFlush ?? 2, this.urgentQueueThreshold = Math.max(1, params.options?.urgentQueueThreshold ?? 4), this.historyLimit = Math.max(1, params.options?.historyLimit ?? 200), this.startupMicrotaskFlushes = Math.max(0, Math.floor(params.options?.startupMicrotaskFlushes ?? 0)), this.adaptiveBudgeting = params.options?.adaptiveBudgeting ?? !0;
90
+ let requestedBatch = params.options?.batch;
91
+ requestedBatch === "microtask" ? this.batchStrategy = "microtask" : requestedBatch === "timeout" ? this.batchStrategy = "timeout" : requestedBatch === "rAF" ? this.batchStrategy = this.raf ? "rAF" : "timeout" : this.batchStrategy = this.raf ? "rAF" : "timeout", this.batchStrategy === "microtask" && typeof queueMicrotask != "function" && (this.batchStrategy = this.raf ? "rAF" : "timeout");
96
92
  }
97
93
  enqueue(input) {
98
- if (!input || !input.patches || input.patches.length === 0) {
94
+ if (!input || !input.patches || input.patches.length === 0)
99
95
  return;
100
- }
101
- if (!this.paused && !this.flushing && this.getPendingCount() >= this.urgentQueueThreshold) {
102
- this.runFlush(this.frameBudgetMs, false);
103
- }
104
- const batch = {
96
+ this.batchStrategy !== "rAF" && !this.paused && !this.flushing && this.getPendingCount() >= this.urgentQueueThreshold && this.runFlush(this.frameBudgetMs, !1);
97
+ let explicitKind = input.meta?.kind, hasExplicitKind = explicitKind === "semantic" || explicitKind === "enrichment", hasSemanticPatch = !1, hasNonInputSourcePatch = !1;
98
+ for (let patch of input.patches)
99
+ patch.op !== "appendInputSource" && (hasNonInputSourcePatch = !0), !hasExplicitKind && !hasSemanticPatch && getPatchKind(patch) === "semantic" && (hasSemanticPatch = !0);
100
+ let batch = {
105
101
  seq: ++this.sequence,
106
102
  patches: input.patches,
107
103
  meta: input.meta ?? {},
108
104
  receivedAt: input.meta?.receivedAt ?? this.now(),
109
- kind: input.meta?.kind === "semantic" || input.meta?.kind === "enrichment" ? input.meta.kind : input.patches.some((patch) => getPatchKind(patch) === "semantic") ? "semantic" : "enrichment",
105
+ kind: explicitKind === "semantic" || explicitKind === "enrichment" ? explicitKind : hasSemanticPatch ? "semantic" : "enrichment",
106
+ hasNonInputSourcePatch,
110
107
  priority: input.meta?.priority === "low" ? "low" : "high"
111
108
  };
112
- if (batch.kind === "semantic") {
113
- batch.priority = "high";
114
- this.semanticQueue.push(batch);
115
- } else if (batch.priority === "low") {
116
- this.enrichmentLowQueue.push(batch);
117
- } else {
118
- this.enrichmentHighQueue.push(batch);
119
- }
120
- if (!this.paused) {
121
- this.schedule();
122
- }
109
+ batch.kind === "semantic" ? (batch.priority = "high", this.semanticQueue.push(batch)) : batch.priority === "low" ? this.enrichmentLowQueue.push(batch) : this.enrichmentHighQueue.push(batch), this.paused || this.schedule();
123
110
  }
124
111
  flushAll() {
125
- if (this.semanticQueue.length === 0 && this.enrichmentHighQueue.length === 0 && this.enrichmentLowQueue.length === 0 && !this.flushing) {
112
+ if (this.semanticQueue.length === 0 && this.enrichmentHighQueue.length === 0 && this.enrichmentLowQueue.length === 0 && !this.flushing)
126
113
  return null;
127
- }
128
114
  this.cancelScheduled();
129
- const result = this.runFlush(Number.POSITIVE_INFINITY, true);
130
- this.maybeResolveIdle();
131
- return result;
115
+ let result = this.runFlush(Number.POSITIVE_INFINITY, !0);
116
+ return this.maybeResolveIdle(), result;
132
117
  }
133
118
  clear() {
134
- this.cancelScheduled();
135
- this.semanticQueue = [];
136
- this.enrichmentHighQueue = [];
137
- this.enrichmentLowQueue = [];
138
- this.flushing = false;
139
- this.flushCount = 0;
140
- this.coalescingDurationSamples = [];
141
- this.adaptiveBudgetActive = false;
142
- this.adaptiveHighBatchCap = void 0;
143
- this.adaptiveLowBatchCap = void 0;
144
- this.maybeResolveIdle();
119
+ this.cancelScheduled(), this.semanticQueue = [], this.enrichmentHighQueue = [], this.enrichmentLowQueue = [], this.flushing = !1, this.flushCount = 0, this.coalescingDurationSamples = [], this.adaptiveBudgetActive = !1, this.adaptiveHighBatchCap = void 0, this.adaptiveLowBatchCap = void 0, this.maybeResolveIdle();
145
120
  }
146
121
  getPendingCount() {
147
122
  return this.semanticQueue.length + this.enrichmentHighQueue.length + this.enrichmentLowQueue.length;
@@ -150,114 +125,54 @@ var PatchCommitScheduler = class {
150
125
  return !this.flushing && !this.scheduled && this.semanticQueue.length === 0 && this.enrichmentHighQueue.length === 0 && this.enrichmentLowQueue.length === 0;
151
126
  }
152
127
  awaitIdle() {
153
- if (this.isIdle()) {
154
- return Promise.resolve();
155
- }
156
- return new Promise((resolve) => {
128
+ return this.isIdle() ? Promise.resolve() : new Promise((resolve) => {
157
129
  this.idleResolvers.push(resolve);
158
130
  });
159
131
  }
160
132
  schedule() {
161
- if (this.paused) {
162
- return;
163
- }
164
- if (this.scheduled || this.flushing) {
133
+ if (this.paused || this.scheduled || this.flushing)
165
134
  return;
166
- }
167
- const token = ++this.scheduleToken;
168
- this.scheduled = true;
169
- const execute = () => {
135
+ let token = ++this.scheduleToken;
136
+ this.scheduled = !0;
137
+ let execute = () => {
170
138
  if (token !== this.scheduleToken) {
171
- this.scheduled = false;
172
- this.scheduledHandle = null;
173
- this.scheduledViaRaf = false;
174
- this.maybeResolveIdle();
139
+ this.scheduled = !1, this.scheduledHandle = null, this.scheduledViaRaf = !1, this.maybeResolveIdle();
175
140
  return;
176
141
  }
177
142
  if (this.paused) {
178
- this.scheduled = false;
179
- this.scheduledHandle = null;
180
- this.scheduledViaRaf = false;
181
- this.maybeResolveIdle();
143
+ this.scheduled = !1, this.scheduledHandle = null, this.scheduledViaRaf = !1, this.maybeResolveIdle();
182
144
  return;
183
145
  }
184
- this.scheduled = false;
185
- this.scheduledHandle = null;
186
- this.scheduledViaRaf = false;
187
- this.runFlush(this.frameBudgetMs, false);
188
- this.maybeResolveIdle();
189
- };
190
- const useStartupMicrotask = this.batchStrategy !== "microtask" && this.startupMicrotaskFlushes > 0 && this.flushCount < this.startupMicrotaskFlushes && typeof queueMicrotask === "function";
191
- if ((this.batchStrategy === "microtask" || useStartupMicrotask) && typeof queueMicrotask === "function") {
192
- this.scheduledViaRaf = false;
193
- this.scheduledHandle = token;
194
- queueMicrotask(execute);
195
- } else if (this.batchStrategy === "rAF" && this.raf) {
196
- this.scheduledViaRaf = true;
197
- this.scheduledHandle = this.raf(() => execute());
198
- } else {
199
- this.scheduledViaRaf = false;
200
- this.scheduledHandle = setTimeout(() => execute(), this.timeoutMs);
201
- }
146
+ this.scheduled = !1, this.scheduledHandle = null, this.scheduledViaRaf = !1, this.runFlush(this.frameBudgetMs, !1), this.maybeResolveIdle();
147
+ }, useStartupMicrotask = this.batchStrategy !== "microtask" && this.startupMicrotaskFlushes > 0 && this.flushCount < this.startupMicrotaskFlushes && typeof queueMicrotask == "function";
148
+ (this.batchStrategy === "microtask" || useStartupMicrotask) && typeof queueMicrotask == "function" ? (this.scheduledViaRaf = !1, this.scheduledHandle = token, queueMicrotask(execute)) : this.batchStrategy === "rAF" && this.raf ? (this.scheduledViaRaf = !0, this.scheduledHandle = this.raf(() => execute())) : (this.scheduledViaRaf = !1, this.scheduledHandle = setTimeout(() => execute(), this.timeoutMs));
202
149
  }
203
150
  cancelScheduled() {
204
- if (!this.scheduled) return;
205
- this.scheduleToken++;
206
- if (this.scheduledHandle !== null) {
207
- if (this.scheduledViaRaf && this.cancelRaf) {
208
- this.cancelRaf(this.scheduledHandle);
209
- } else {
210
- clearTimeout(this.scheduledHandle);
211
- }
212
- }
213
- this.scheduled = false;
214
- this.scheduledHandle = null;
215
- this.scheduledViaRaf = false;
151
+ this.scheduled && (this.scheduleToken++, this.scheduledHandle !== null && (this.scheduledViaRaf && this.cancelRaf ? this.cancelRaf(this.scheduledHandle) : clearTimeout(this.scheduledHandle)), this.scheduled = !1, this.scheduledHandle = null, this.scheduledViaRaf = !1);
216
152
  }
217
153
  runFlush(budgetMs, manual) {
218
- if (this.paused && !manual) {
219
- return null;
220
- }
221
- if (this.semanticQueue.length === 0 && this.enrichmentHighQueue.length === 0 && this.enrichmentLowQueue.length === 0) {
154
+ if (this.paused && !manual || this.semanticQueue.length === 0 && this.enrichmentHighQueue.length === 0 && this.enrichmentLowQueue.length === 0)
222
155
  return null;
223
- }
224
- this.flushing = true;
225
- const startedAt = this.now();
226
- const initialSemanticQueueSize = this.semanticQueue.length;
227
- const initialEnrichmentQueueSize = this.enrichmentHighQueue.length + this.enrichmentLowQueue.length;
228
- const initialQueueSize = initialSemanticQueueSize + initialEnrichmentQueueSize;
229
- const batches = [];
230
- let totalInputPatches = 0;
231
- let totalAppliedPatches = 0;
232
- let elapsed = 0;
233
- const processQueue = (queue, maxBatches, budgetLimit) => {
234
- const effectiveBudget = manual ? Number.POSITIVE_INFINITY : Math.max(0, budgetLimit ?? budgetMs);
235
- const startingBatchCount = batches.length;
236
- let consumed = 0;
237
- while (consumed < queue.length) {
238
- const next = queue[consumed];
156
+ this.flushing = !0;
157
+ let startedAt = this.now(), elapsed = 0, initialSemanticQueueSize = this.semanticQueue.length, initialEnrichmentQueueSize = this.enrichmentHighQueue.length + this.enrichmentLowQueue.length, initialQueueSize = initialSemanticQueueSize + initialEnrichmentQueueSize, batches = [], totalInputPatches = 0, totalAppliedPatches = 0, hasNonInputSourcePatch = !1, processQueue = (queue, maxBatches, budgetLimit) => {
158
+ let effectiveBudget = manual ? Number.POSITIVE_INFINITY : Math.max(0, budgetLimit ?? budgetMs), startingBatchCount = batches.length, consumed = 0;
159
+ for (; consumed < queue.length; ) {
160
+ let next = queue[consumed];
239
161
  if (!next) break;
240
- consumed += 1;
241
- const applyStart = this.now();
242
- const queueDelayMs = Math.max(0, applyStart - next.receivedAt);
243
- let coalesced;
244
- let coalescingMetrics;
245
- if (shouldSkipCoalescing(next.patches)) {
162
+ if (consumed += 1, next.meta.generation !== void 0 && this.isGenerationCurrent && !this.isGenerationCurrent(next.meta.generation)) continue;
163
+ hasNonInputSourcePatch || (hasNonInputSourcePatch = next.hasNonInputSourcePatch);
164
+ let applyStart = this.now(), queueDelayMs = Math.max(0, applyStart - next.receivedAt), coalesced, coalescingMetrics;
165
+ if (shouldSkipCoalescing(next.patches))
246
166
  coalesced = next.patches;
247
- } else {
248
- const result = coalescePatchesWithMetrics(next.patches, DEFAULT_COALESCE_CONFIG);
249
- coalesced = result.patches;
250
- coalescingMetrics = result.metrics;
167
+ else {
168
+ let result = coalescePatchesWithMetrics(next.patches, DEFAULT_COALESCE_CONFIG);
169
+ coalesced = result.patches, coalescingMetrics = result.metrics;
251
170
  }
252
- const touched = this.store.applyPatches(coalesced, {
253
- coalesced: true,
171
+ let touched = this.store.applyPatches(coalesced, {
172
+ coalesced: !0,
254
173
  metrics: coalescingMetrics
255
- });
256
- const durationMs = this.now() - applyStart;
257
- this.recordCoalescingDuration(coalescingMetrics?.durationMs);
258
- totalInputPatches += next.patches.length;
259
- totalAppliedPatches += coalesced.length;
260
- batches.push({
174
+ }), durationMs = this.now() - applyStart;
175
+ if (this.recordCoalescingDuration(coalescingMetrics?.durationMs), totalInputPatches += next.patches.length, totalAppliedPatches += coalesced.length, batches.push({
261
176
  tx: next.meta.tx,
262
177
  metrics: next.meta.metrics,
263
178
  patchCount: next.patches.length,
@@ -270,45 +185,26 @@ var PatchCommitScheduler = class {
270
185
  priority: next.priority,
271
186
  kind: next.kind,
272
187
  coalescing: coalescingMetrics
273
- });
274
- if (maxBatches && batches.length - startingBatchCount >= maxBatches) {
188
+ }), maxBatches && batches.length - startingBatchCount >= maxBatches || !manual && (elapsed = this.now() - startedAt, elapsed >= effectiveBudget))
275
189
  break;
276
- }
277
- if (!manual) {
278
- elapsed = this.now() - startedAt;
279
- if (elapsed >= effectiveBudget) {
280
- break;
281
- }
282
- }
283
- }
284
- if (consumed > 0) {
285
- queue.splice(0, consumed);
286
190
  }
191
+ consumed > 0 && queue.splice(0, consumed);
287
192
  };
288
- processQueue(this.semanticQueue, void 0, Number.POSITIVE_INFINITY);
289
- const initialHighQueueSize = this.enrichmentHighQueue.length;
290
- const configuredHighLimit = manual || !this.maxBatchesPerFlush ? void 0 : initialHighQueueSize > this.maxBatchesPerFlush ? void 0 : this.maxBatchesPerFlush;
291
- const highBatchLimit = this.adaptiveBudgeting ? this.adaptiveHighBatchCap ?? configuredHighLimit : configuredHighLimit;
292
- processQueue(this.enrichmentHighQueue, highBatchLimit, budgetMs);
293
- if (manual) {
294
- processQueue(this.enrichmentLowQueue, void 0, Number.POSITIVE_INFINITY);
295
- } else if (this.enrichmentLowQueue.length > 0) {
296
- elapsed = this.now() - startedAt;
297
- const budgetForLow = Math.max(0, this.lowPriorityFrameBudgetMs - elapsed);
298
- const processedHigherPriorityWork = batches.length > 0;
299
- if (budgetForLow > 0 || !processedHigherPriorityWork) {
300
- const initialLowQueueSize = this.enrichmentLowQueue.length;
301
- const configuredLowLimit = manual || !this.maxLowPriorityBatchesPerFlush ? void 0 : initialLowQueueSize > this.maxLowPriorityBatchesPerFlush ? void 0 : this.maxLowPriorityBatchesPerFlush;
302
- const lowBatchLimit = this.adaptiveBudgeting ? this.adaptiveLowBatchCap ?? configuredLowLimit : configuredLowLimit;
303
- processQueue(this.enrichmentLowQueue, lowBatchLimit, budgetForLow);
193
+ if (processQueue(this.semanticQueue, void 0, budgetMs), this.semanticQueue.length === 0) {
194
+ let initialHighQueueSize = this.enrichmentHighQueue.length, configuredHighLimit = manual || !this.maxBatchesPerFlush || initialHighQueueSize > this.maxBatchesPerFlush ? void 0 : this.maxBatchesPerFlush, highBatchLimit = this.adaptiveBudgeting ? this.adaptiveHighBatchCap ?? configuredHighLimit : configuredHighLimit;
195
+ if (processQueue(this.enrichmentHighQueue, highBatchLimit, budgetMs), manual)
196
+ processQueue(this.enrichmentLowQueue, void 0, Number.POSITIVE_INFINITY);
197
+ else if (this.enrichmentLowQueue.length > 0) {
198
+ elapsed = this.now() - startedAt;
199
+ let budgetForLow = Math.max(0, this.lowPriorityFrameBudgetMs - elapsed), processedHigherPriorityWork = batches.length > 0;
200
+ if (budgetForLow > 0 || !processedHigherPriorityWork) {
201
+ let initialLowQueueSize = this.enrichmentLowQueue.length, configuredLowLimit = this.maxLowPriorityBatchesPerFlush ? initialLowQueueSize > this.maxLowPriorityBatchesPerFlush ? void 0 : this.maxLowPriorityBatchesPerFlush : void 0, lowBatchLimit = this.adaptiveBudgeting ? this.adaptiveLowBatchCap ?? configuredLowLimit : configuredLowLimit;
202
+ processQueue(this.enrichmentLowQueue, lowBatchLimit, budgetForLow);
203
+ }
304
204
  }
305
205
  }
306
- this.flushing = false;
307
- const completedAt = this.now();
308
- const totalDurationMs = completedAt - startedAt;
309
- const remainingSemantic = this.semanticQueue.length;
310
- const remainingEnrichment = this.enrichmentHighQueue.length + this.enrichmentLowQueue.length;
311
- const remaining = remainingSemantic + remainingEnrichment;
206
+ this.flushing = !1;
207
+ let completedAt = this.now(), totalDurationMs = completedAt - startedAt, remainingSemantic = this.semanticQueue.length, remainingEnrichment = this.enrichmentHighQueue.length + this.enrichmentLowQueue.length, remaining = remainingSemantic + remainingEnrichment;
312
208
  if (batches.length > 0) {
313
209
  this.flushCount += 1;
314
210
  try {
@@ -325,20 +221,15 @@ var PatchCommitScheduler = class {
325
221
  enrichmentQueueDepthBefore: initialEnrichmentQueueSize,
326
222
  flushStartedAt: startedAt,
327
223
  flushCompletedAt: completedAt
328
- });
224
+ }, hasNonInputSourcePatch);
329
225
  } catch (error) {
330
226
  console.error("[patch-scheduler] onFlush callback failed", error);
331
227
  }
332
228
  }
333
- if (remaining > 0 && !this.paused) {
334
- this.schedule();
335
- }
336
- const coalescingStats = this.adaptiveBudgeting ? this.computeCoalescingDurationStats() : { sampleCount: this.coalescingDurationSamples.length, p95: null };
337
- if (this.adaptiveBudgeting) {
338
- this.updateAdaptiveBudget(coalescingStats.p95);
339
- }
340
- const adaptiveState = this.adaptiveBudgeting ? this.snapshotAdaptiveBudget(coalescingStats.p95 ?? null) : void 0;
341
- const flushResult = batches.length > 0 ? {
229
+ remaining > 0 && !this.paused && this.schedule();
230
+ let coalescingStats = this.adaptiveBudgeting ? this.computeCoalescingDurationStats() : { sampleCount: this.coalescingDurationSamples.length, p95: null };
231
+ this.adaptiveBudgeting && this.updateAdaptiveBudget(coalescingStats.p95);
232
+ let adaptiveState = this.adaptiveBudgeting ? this.snapshotAdaptiveBudget(coalescingStats.p95 ?? null) : void 0, flushResult = batches.length > 0 ? {
342
233
  batches,
343
234
  totalPatches: totalInputPatches,
344
235
  totalAppliedPatches,
@@ -356,26 +247,15 @@ var PatchCommitScheduler = class {
356
247
  adaptiveBudgetActive: adaptiveState?.active,
357
248
  adaptiveBudgetState: adaptiveState
358
249
  } : null;
359
- if (flushResult) {
360
- this.recordHistory(flushResult);
361
- }
362
- return flushResult;
250
+ return flushResult && this.recordHistory(flushResult), flushResult;
363
251
  }
364
252
  recordCoalescingDuration(value) {
365
- if (typeof value !== "number" || !Number.isFinite(value)) {
366
- return;
367
- }
368
- this.coalescingDurationSamples.push(value);
369
- if (this.coalescingDurationSamples.length > COALESCING_DURATION_SAMPLE_LIMIT) {
370
- this.coalescingDurationSamples.splice(0, this.coalescingDurationSamples.length - COALESCING_DURATION_SAMPLE_LIMIT);
371
- }
253
+ typeof value != "number" || !Number.isFinite(value) || (this.coalescingDurationSamples.push(value), this.coalescingDurationSamples.length > COALESCING_DURATION_SAMPLE_LIMIT && this.coalescingDurationSamples.splice(0, this.coalescingDurationSamples.length - COALESCING_DURATION_SAMPLE_LIMIT));
372
254
  }
373
255
  computeCoalescingDurationStats() {
374
- if (this.coalescingDurationSamples.length === 0) {
256
+ if (this.coalescingDurationSamples.length === 0)
375
257
  return { sampleCount: 0, p95: null };
376
- }
377
- const sorted = [...this.coalescingDurationSamples].sort((a, b) => a - b);
378
- const index = Math.min(sorted.length - 1, Math.max(0, Math.floor(0.95 * (sorted.length - 1))));
258
+ let sorted = [...this.coalescingDurationSamples].sort((a, b) => a - b), index = Math.min(sorted.length - 1, Math.max(0, Math.floor(0.95 * (sorted.length - 1))));
379
259
  return { sampleCount: this.coalescingDurationSamples.length, p95: sorted[index] ?? null };
380
260
  }
381
261
  snapshotAdaptiveBudget(latestP95) {
@@ -385,53 +265,32 @@ var PatchCommitScheduler = class {
385
265
  lowBatchCap: this.adaptiveLowBatchCap,
386
266
  activateThresholdMs: COALESCING_DURATION_ACTIVATE_MS,
387
267
  deactivateThresholdMs: COALESCING_DURATION_DEACTIVATE_MS,
388
- lastObservedP95: typeof latestP95 === "number" ? latestP95 : null
268
+ lastObservedP95: typeof latestP95 == "number" ? latestP95 : null
389
269
  };
390
270
  }
391
271
  updateAdaptiveBudget(p95) {
392
- if (!this.adaptiveBudgeting) {
393
- return;
394
- }
395
- if (p95 !== null && p95 > COALESCING_DURATION_ACTIVATE_MS) {
396
- if (!this.adaptiveBudgetActive) {
397
- this.adaptiveBudgetActive = true;
398
- this.adaptiveHighBatchCap = this.computeAdaptiveCap(this.maxBatchesPerFlush, 2);
399
- this.adaptiveLowBatchCap = this.computeAdaptiveCap(this.maxLowPriorityBatchesPerFlush, 1);
400
- }
401
- } else if (this.adaptiveBudgetActive && (p95 === null || p95 < COALESCING_DURATION_DEACTIVATE_MS)) {
402
- this.adaptiveBudgetActive = false;
403
- this.adaptiveHighBatchCap = void 0;
404
- this.adaptiveLowBatchCap = void 0;
405
- }
272
+ this.adaptiveBudgeting && (p95 !== null && p95 > COALESCING_DURATION_ACTIVATE_MS ? this.adaptiveBudgetActive || (this.adaptiveBudgetActive = !0, this.adaptiveHighBatchCap = this.computeAdaptiveCap(this.maxBatchesPerFlush, 2), this.adaptiveLowBatchCap = this.computeAdaptiveCap(this.maxLowPriorityBatchesPerFlush, 1)) : this.adaptiveBudgetActive && (p95 === null || p95 < COALESCING_DURATION_DEACTIVATE_MS) && (this.adaptiveBudgetActive = !1, this.adaptiveHighBatchCap = void 0, this.adaptiveLowBatchCap = void 0));
406
273
  }
407
274
  computeAdaptiveCap(configured, fallback = 1) {
408
- if (configured === void 0 || configured <= 0) {
409
- return Math.max(1, fallback);
410
- }
411
- return Math.max(1, Math.floor(configured / 2));
275
+ return configured === void 0 || configured <= 0 ? Math.max(1, fallback) : Math.max(1, Math.floor(configured / 2));
412
276
  }
413
277
  maybeResolveIdle() {
414
- if (!this.isIdle()) {
278
+ if (!this.isIdle() || this.idleResolvers.length === 0)
415
279
  return;
416
- }
417
- if (this.idleResolvers.length === 0) {
418
- return;
419
- }
420
- const pending = this.idleResolvers.slice();
280
+ let pending = this.idleResolvers.slice();
421
281
  this.idleResolvers = [];
422
- for (const resolve of pending) {
282
+ for (let resolve of pending)
423
283
  try {
424
284
  resolve();
425
285
  } catch (error) {
426
286
  console.error("[patch-scheduler] idle resolver failed", error);
427
287
  }
428
- }
429
288
  }
430
289
  recordHistory(result) {
431
- if (RETAIN_TOUCHED_IN_HISTORY) {
290
+ if (RETAIN_TOUCHED_IN_HISTORY)
432
291
  this.history.push(result);
433
- } else {
434
- const trimmed = {
292
+ else {
293
+ let trimmed = {
435
294
  ...result,
436
295
  batches: result.batches.map((batch) => ({
437
296
  ...batch,
@@ -440,108 +299,34 @@ var PatchCommitScheduler = class {
440
299
  };
441
300
  this.history.push(trimmed);
442
301
  }
443
- if (this.history.length > this.historyLimit) {
444
- this.history.splice(0, this.history.length - this.historyLimit);
445
- }
302
+ this.history.length > this.historyLimit && this.history.splice(0, this.history.length - this.historyLimit);
446
303
  }
447
304
  pause() {
448
- if (this.paused) {
449
- return;
450
- }
451
- this.paused = true;
452
- this.cancelScheduled();
305
+ this.paused || (this.paused = !0, this.cancelScheduled());
453
306
  }
454
307
  resume() {
455
- if (!this.paused) {
456
- return;
457
- }
458
- this.paused = false;
459
- if (this.getPendingCount() > 0) {
460
- this.schedule();
461
- }
308
+ this.paused && (this.paused = !1, this.getPendingCount() > 0 && this.schedule());
462
309
  }
463
310
  isPaused() {
464
311
  return this.paused;
465
312
  }
466
313
  restart() {
467
- this.cancelScheduled();
468
- this.semanticQueue = [];
469
- this.enrichmentHighQueue = [];
470
- this.enrichmentLowQueue = [];
471
- this.flushing = false;
472
- this.sequence = 0;
473
- this.history = [];
474
- this.flushCount = 0;
475
- this.scheduleToken++;
476
- this.paused = false;
477
- this.coalescingDurationSamples = [];
478
- this.adaptiveBudgetActive = false;
479
- this.adaptiveHighBatchCap = void 0;
480
- this.adaptiveLowBatchCap = void 0;
481
- this.maybeResolveIdle();
314
+ this.cancelScheduled(), this.semanticQueue = [], this.enrichmentHighQueue = [], this.enrichmentLowQueue = [], this.flushing = !1, this.sequence = 0, this.history = [], this.flushCount = 0, this.scheduleToken++, this.paused = !1, this.coalescingDurationSamples = [], this.adaptiveBudgetActive = !1, this.adaptiveHighBatchCap = void 0, this.adaptiveLowBatchCap = void 0, this.maybeResolveIdle();
482
315
  }
483
316
  getHistory(limit) {
484
- if (limit === void 0 || limit >= this.history.length) {
485
- return [...this.history];
486
- }
487
- return this.history.slice(this.history.length - limit);
317
+ return limit === void 0 || limit >= this.history.length ? [...this.history] : this.history.slice(this.history.length - limit);
488
318
  }
489
319
  setHistoryLimit(limit) {
490
- this.historyLimit = Math.max(1, limit);
491
- if (this.history.length > this.historyLimit) {
492
- this.history.splice(0, this.history.length - this.historyLimit);
493
- }
320
+ this.historyLimit = Math.max(1, limit), this.history.length > this.historyLimit && this.history.splice(0, this.history.length - this.historyLimit);
494
321
  }
495
322
  updateOptions(options) {
496
323
  if (!options) return;
497
- let shouldReschedule = false;
498
- if (typeof options.frameBudgetMs !== "undefined") {
499
- this.frameBudgetMs = Math.max(1, options.frameBudgetMs);
500
- shouldReschedule = true;
501
- }
502
- if (typeof options.maxBatchesPerFlush !== "undefined") {
503
- this.maxBatchesPerFlush = options.maxBatchesPerFlush;
504
- shouldReschedule = true;
505
- }
506
- if (typeof options.lowPriorityFrameBudgetMs !== "undefined") {
507
- this.lowPriorityFrameBudgetMs = Math.max(1, options.lowPriorityFrameBudgetMs);
508
- shouldReschedule = true;
509
- }
510
- if (typeof options.maxLowPriorityBatchesPerFlush !== "undefined") {
511
- this.maxLowPriorityBatchesPerFlush = options.maxLowPriorityBatchesPerFlush;
512
- shouldReschedule = true;
513
- }
514
- if (typeof options.urgentQueueThreshold !== "undefined") {
515
- this.urgentQueueThreshold = Math.max(1, options.urgentQueueThreshold);
516
- shouldReschedule = true;
517
- }
518
- if (typeof options.historyLimit !== "undefined") {
519
- this.setHistoryLimit(options.historyLimit);
520
- }
521
- if (typeof options.startupMicrotaskFlushes !== "undefined") {
522
- this.startupMicrotaskFlushes = Math.max(0, Math.floor(options.startupMicrotaskFlushes));
523
- }
524
- if (typeof options.adaptiveBudgeting !== "undefined") {
525
- this.adaptiveBudgeting = options.adaptiveBudgeting;
526
- }
527
- if (typeof options.batch !== "undefined") {
528
- const requestedBatch = options.batch;
529
- if (requestedBatch === "microtask") {
530
- this.batchStrategy = "microtask";
531
- } else if (requestedBatch === "timeout") {
532
- this.batchStrategy = "timeout";
533
- } else if (requestedBatch === "rAF") {
534
- this.batchStrategy = this.raf ? "rAF" : "timeout";
535
- }
536
- if (this.batchStrategy === "microtask" && typeof queueMicrotask !== "function") {
537
- this.batchStrategy = this.raf ? "rAF" : "timeout";
538
- }
539
- shouldReschedule = true;
540
- }
541
- if (shouldReschedule && !this.paused && !this.flushing && this.getPendingCount() > 0) {
542
- this.cancelScheduled();
543
- this.schedule();
324
+ let shouldReschedule = !1;
325
+ if (typeof options.frameBudgetMs < "u" && (this.frameBudgetMs = Math.max(1, options.frameBudgetMs), shouldReschedule = !0), typeof options.maxBatchesPerFlush < "u" && (this.maxBatchesPerFlush = options.maxBatchesPerFlush, shouldReschedule = !0), typeof options.lowPriorityFrameBudgetMs < "u" && (this.lowPriorityFrameBudgetMs = Math.max(1, options.lowPriorityFrameBudgetMs), shouldReschedule = !0), typeof options.maxLowPriorityBatchesPerFlush < "u" && (this.maxLowPriorityBatchesPerFlush = options.maxLowPriorityBatchesPerFlush, shouldReschedule = !0), typeof options.urgentQueueThreshold < "u" && (this.urgentQueueThreshold = Math.max(1, options.urgentQueueThreshold), shouldReschedule = !0), typeof options.historyLimit < "u" && this.setHistoryLimit(options.historyLimit), typeof options.startupMicrotaskFlushes < "u" && (this.startupMicrotaskFlushes = Math.max(0, Math.floor(options.startupMicrotaskFlushes))), typeof options.adaptiveBudgeting < "u" && (this.adaptiveBudgeting = options.adaptiveBudgeting), typeof options.batch < "u") {
326
+ let requestedBatch = options.batch;
327
+ requestedBatch === "microtask" ? this.batchStrategy = "microtask" : requestedBatch === "timeout" ? this.batchStrategy = "timeout" : requestedBatch === "rAF" && (this.batchStrategy = this.raf ? "rAF" : "timeout"), this.batchStrategy === "microtask" && typeof queueMicrotask != "function" && (this.batchStrategy = this.raf ? "rAF" : "timeout"), shouldReschedule = !0;
544
328
  }
329
+ shouldReschedule && !this.paused && !this.flushing && this.getPendingCount() > 0 && (this.cancelScheduled(), this.schedule());
545
330
  }
546
331
  clearHistory() {
547
332
  this.history = [];