@univerjs/engine-formula 0.15.3-insiders.20260131-b9b8805 → 0.15.3

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/lib/facade.js CHANGED
@@ -1,15 +1,15 @@
1
- import { FBase as _, FUniver as S } from "@univerjs/core/facade";
2
- import { Inject as c, ICommandService as f, Injector as C, IConfigService as h } from "@univerjs/core";
3
- import { LexerTreeBuilder as v, SetTriggerFormulaCalculationStartMutation as g, SetFormulaCalculationStopMutation as E, SetFormulaCalculationStartMutation as x, SetFormulaCalculationNotificationMutation as m, GlobalComputingStatusService as F, ENGINE_FORMULA_CYCLE_REFERENCE_COUNT as T, SetFormulaStringBatchCalculationResultMutation as y, SetFormulaStringBatchCalculationMutation as R, SetFormulaDependencyCalculationResultMutation as b, SetFormulaDependencyCalculationMutation as w, SetCellFormulaDependencyCalculationResultMutation as D, SetCellFormulaDependencyCalculationMutation as M, SetQueryFormulaDependencyResultMutation as d, SetQueryFormulaDependencyMutation as p, ENGINE_FORMULA_RETURN_DEPENDENCY_TREE as I, SetQueryFormulaDependencyAllResultMutation as N, SetQueryFormulaDependencyAllMutation as B, IFunctionService as P, IDefinedNamesService as L, ISuperTableService as O } from "@univerjs/engine-formula";
4
- import { firstValueFrom as A, race as U, filter as j, timer as Q, map as G } from "rxjs";
5
- var q = Object.getOwnPropertyDescriptor, z = (e, t, r, i) => {
6
- for (var n = i > 1 ? void 0 : i ? q(t, r) : t, a = e.length - 1, o; a >= 0; a--)
1
+ import { FBase as C, FUniver as f } from "@univerjs/core/facade";
2
+ import { Inject as c, ICommandService as _, Injector as h, IConfigService as v } from "@univerjs/core";
3
+ import { LexerTreeBuilder as g, IFunctionService as E, IDefinedNamesService as T, ISuperTableService as x, SetTriggerFormulaCalculationStartMutation as F, SetFormulaCalculationStopMutation as y, SetFormulaCalculationStartMutation as R, SetFormulaCalculationNotificationMutation as d, GlobalComputingStatusService as b, ENGINE_FORMULA_CYCLE_REFERENCE_COUNT as w, SetFormulaCalculationResultMutation as D, SetFormulaStringBatchCalculationResultMutation as M, SetFormulaStringBatchCalculationMutation as I, SetFormulaDependencyCalculationResultMutation as N, SetFormulaDependencyCalculationMutation as P, SetCellFormulaDependencyCalculationResultMutation as B, SetCellFormulaDependencyCalculationMutation as L, SetQueryFormulaDependencyResultMutation as p, SetQueryFormulaDependencyMutation as S, ENGINE_FORMULA_RETURN_DEPENDENCY_TREE as A, SetQueryFormulaDependencyAllResultMutation as O, SetQueryFormulaDependencyAllMutation as U } from "@univerjs/engine-formula";
4
+ import { firstValueFrom as j, race as Q, filter as q, timer as G, map as z } from "rxjs";
5
+ var V = Object.getOwnPropertyDescriptor, Y = (e, t, i, r) => {
6
+ for (var n = r > 1 ? void 0 : r ? V(t, i) : t, a = e.length - 1, o; a >= 0; a--)
7
7
  (o = e[a]) && (n = o(n) || n);
8
8
  return n;
9
- }, u = (e, t) => (r, i) => t(r, i, e);
10
- let l = class extends _ {
11
- constructor(e, t, r, i, n, a, o) {
12
- super(), this._commandService = e, this._injector = t, this._lexerTreeBuilder = r, this._configService = i, this._functionService = n, this._definedNamesService = a, this._superTableService = o, this._initialize();
9
+ }, u = (e, t) => (i, r) => t(i, r, e);
10
+ let m = class extends C {
11
+ constructor(e, t, i, r, n, a, o) {
12
+ super(), this._commandService = e, this._injector = t, this._lexerTreeBuilder = i, this._configService = r, this._functionService = n, this._definedNamesService = a, this._superTableService = o, this._initialize();
13
13
  }
14
14
  /**
15
15
  * @ignore
@@ -38,8 +38,8 @@ let l = class extends _ {
38
38
  * console.log(result);
39
39
  * ```
40
40
  */
41
- moveFormulaRefOffset(e, t, r, i) {
42
- return this._lexerTreeBuilder.moveFormulaRefOffset(e, t, r, i);
41
+ moveFormulaRefOffset(e, t, i, r) {
42
+ return this._lexerTreeBuilder.moveFormulaRefOffset(e, t, i, r);
43
43
  }
44
44
  /**
45
45
  * Resolves the formula string to a 'node' node
@@ -66,7 +66,7 @@ let l = class extends _ {
66
66
  * ```
67
67
  */
68
68
  executeCalculation() {
69
- this._commandService.executeCommand(g.id, { commands: [], forceCalculation: !0 }, { onlyLocal: !0 });
69
+ this._commandService.executeCommand(F.id, { commands: [], forceCalculation: !0 }, { onlyLocal: !0 });
70
70
  }
71
71
  /**
72
72
  * Stop the calculation of the formula.
@@ -78,7 +78,7 @@ let l = class extends _ {
78
78
  * ```
79
79
  */
80
80
  stopCalculation() {
81
- this._commandService.executeCommand(E.id, {});
81
+ this._commandService.executeCommand(y.id, {});
82
82
  }
83
83
  /**
84
84
  * Listening calculation starts.
@@ -95,9 +95,9 @@ let l = class extends _ {
95
95
  */
96
96
  calculationStart(e) {
97
97
  return this._commandService.onCommandExecuted((t) => {
98
- if (t.id === x.id) {
99
- const r = t.params;
100
- e(r.forceCalculation);
98
+ if (t.id === R.id) {
99
+ const i = t.params;
100
+ e(i.forceCalculation);
101
101
  }
102
102
  });
103
103
  }
@@ -116,20 +116,20 @@ let l = class extends _ {
116
116
  */
117
117
  calculationEnd(e) {
118
118
  return this._commandService.onCommandExecuted((t) => {
119
- if (t.id !== m.id)
119
+ if (t.id !== d.id)
120
120
  return;
121
- const r = t.params;
122
- r.functionsExecutedState !== void 0 && e(r.functionsExecutedState);
121
+ const i = t.params;
122
+ i.functionsExecutedState !== void 0 && e(i.functionsExecutedState);
123
123
  });
124
124
  }
125
125
  /**
126
126
  * @deprecated Use `onCalculationEnd` instead.
127
127
  */
128
128
  whenComputingCompleteAsync(e) {
129
- const t = this._injector.get(F);
130
- return t.computingStatus ? Promise.resolve(!0) : A(U(
131
- t.computingStatus$.pipe(j((r) => r)),
132
- Q(e != null ? e : 3e4).pipe(G(() => !1))
129
+ const t = this._injector.get(b);
130
+ return t.computingStatus ? Promise.resolve(!0) : j(Q(
131
+ t.computingStatus$.pipe(q((i) => i)),
132
+ G(e != null ? e : 3e4).pipe(z(() => !1))
133
133
  ));
134
134
  }
135
135
  /**
@@ -138,10 +138,10 @@ let l = class extends _ {
138
138
  */
139
139
  onCalculationEnd() {
140
140
  return new Promise((e, t) => {
141
- const r = setTimeout(() => {
141
+ const i = setTimeout(() => {
142
142
  t(new Error("Calculation end timeout"));
143
- }, 3e4), i = this.calculationEnd(() => {
144
- clearTimeout(r), i.dispose(), e();
143
+ }, 3e4), r = this.calculationEnd(() => {
144
+ clearTimeout(i), r.dispose(), e();
145
145
  });
146
146
  });
147
147
  }
@@ -160,10 +160,10 @@ let l = class extends _ {
160
160
  */
161
161
  calculationProcessing(e) {
162
162
  return this._commandService.onCommandExecuted((t) => {
163
- if (t.id !== m.id)
163
+ if (t.id !== d.id)
164
164
  return;
165
- const r = t.params;
166
- r.stageInfo !== void 0 && e(r.stageInfo);
165
+ const i = t.params;
166
+ i.stageInfo !== void 0 && e(i.stageInfo);
167
167
  });
168
168
  }
169
169
  /**
@@ -179,7 +179,85 @@ let l = class extends _ {
179
179
  * ```
180
180
  */
181
181
  setMaxIteration(e) {
182
- this._configService.setConfig(T, e);
182
+ this._configService.setConfig(w, e);
183
+ }
184
+ /**
185
+ * Listens for the moment when formula-calculation results are applied.
186
+ *
187
+ * This event fires after the engine completes a calculation cycle and
188
+ * dispatches a `SetFormulaCalculationResultMutation`.
189
+ * The callback is invoked during an idle frame to avoid blocking UI updates.
190
+ *
191
+ * @param {Function} callback - A function called with the calculation result payload
192
+ * once the result-application mutation is emitted.
193
+ * @returns {IDisposable} A disposable used to unsubscribe from the event.
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * const formulaEngine = univerAPI.getFormula();
198
+ *
199
+ * const dispose = formulaEngine.calculationResultApplied((result) => {
200
+ * console.log('Calculation results applied:', result);
201
+ * });
202
+ *
203
+ * // Later…
204
+ * dispose.dispose();
205
+ * ```
206
+ */
207
+ calculationResultApplied(e) {
208
+ return this._commandService.onCommandExecuted((t) => {
209
+ if (t.id !== D.id)
210
+ return;
211
+ const i = t.params;
212
+ i !== void 0 && requestIdleCallback(() => {
213
+ e(i);
214
+ });
215
+ });
216
+ }
217
+ /**
218
+ * Waits for formula-calculation results to be applied.
219
+ *
220
+ * This method resolves under three conditions:
221
+ * 1. A real calculation runs and the engine emits a "calculation started" signal,
222
+ * followed by a "calculation result applied" signal.
223
+ * 2. No calculation actually starts within 500 ms — the method assumes there is
224
+ * nothing to wait for and resolves automatically.
225
+ * 3. A global 30 s timeout triggers, in which case the promise rejects.
226
+ *
227
+ * The API internally listens to both “calculation in progress” events and
228
+ * “calculation result applied” events, ensuring it behaves correctly whether
229
+ * formulas are recalculated or skipped due to cache/state.
230
+ *
231
+ * @returns {Promise<void>} A promise that resolves when calculation results are applied
232
+ * or when no calculation occurs within the start-detection window.
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * const formulaEngine = univerAPI.getFormula();
237
+ *
238
+ * // Wait for formula updates to apply before reading values.
239
+ * await formulaEngine.onCalculationResultApplied();
240
+ *
241
+ * const value = sheet.getRange("C24").getValue();
242
+ * console.log("Updated value:", value);
243
+ * ```
244
+ */
245
+ onCalculationResultApplied() {
246
+ return new Promise((e, t) => {
247
+ let i = !1, r = !1;
248
+ const n = setTimeout(() => {
249
+ l(), t(new Error("Calculation end timeout"));
250
+ }, 3e4), a = setTimeout(() => {
251
+ i || (l(), e());
252
+ }, 500), o = this.calculationProcessing(() => {
253
+ i || (i = !0, clearTimeout(a));
254
+ }), s = this.calculationResultApplied(() => {
255
+ r || (r = !0, l(), e());
256
+ });
257
+ function l() {
258
+ clearTimeout(n), clearTimeout(a), o.dispose(), s.dispose();
259
+ }
260
+ });
183
261
  }
184
262
  /**
185
263
  * Execute a batch of formulas asynchronously and receive computed results.
@@ -251,17 +329,17 @@ let l = class extends _ {
251
329
  * ```
252
330
  */
253
331
  executeFormulas(e, t = 3e4) {
254
- return new Promise((r, i) => {
332
+ return new Promise((i, r) => {
255
333
  const n = this._commandService.onCommandExecuted((o) => {
256
- if (o.id !== y.id)
334
+ if (o.id !== M.id)
257
335
  return;
258
336
  const s = o.params;
259
- clearTimeout(a), n.dispose(), s.result != null ? r(s.result) : i(new Error("Formula batch calculation returned no result"));
337
+ clearTimeout(a), n.dispose(), s.result != null ? i(s.result) : r(new Error("Formula batch calculation returned no result"));
260
338
  }), a = setTimeout(() => {
261
- n.dispose(), i(new Error("Formula batch calculation timeout"));
339
+ n.dispose(), r(new Error("Formula batch calculation timeout"));
262
340
  }, t);
263
341
  this._commandService.executeCommand(
264
- R.id,
342
+ I.id,
265
343
  { formulas: e },
266
344
  { onlyLocal: !0 }
267
345
  );
@@ -289,17 +367,17 @@ let l = class extends _ {
289
367
  * ```
290
368
  */
291
369
  getAllDependencyTrees(e = 3e4) {
292
- return new Promise((t, r) => {
293
- const i = this._commandService.onCommandExecuted((a) => {
294
- if (a.id !== b.id)
370
+ return new Promise((t, i) => {
371
+ const r = this._commandService.onCommandExecuted((a) => {
372
+ if (a.id !== N.id)
295
373
  return;
296
374
  const o = a.params;
297
- clearTimeout(n), i.dispose(), o.result != null ? t(o.result) : t([]);
375
+ clearTimeout(n), r.dispose(), o.result != null ? t(o.result) : t([]);
298
376
  }), n = setTimeout(() => {
299
- i.dispose(), r(new Error("Formula dependency calculation timeout"));
377
+ r.dispose(), i(new Error("Formula dependency calculation timeout"));
300
378
  }, e);
301
379
  this._commandService.executeCommand(
302
- w.id,
380
+ P.id,
303
381
  void 0,
304
382
  { onlyLocal: !0 }
305
383
  );
@@ -340,17 +418,17 @@ let l = class extends _ {
340
418
  * ```
341
419
  */
342
420
  getCellDependencyTree(e, t = 3e4) {
343
- return new Promise((r, i) => {
421
+ return new Promise((i, r) => {
344
422
  const n = this._commandService.onCommandExecuted((o) => {
345
- if (o.id !== D.id)
423
+ if (o.id !== B.id)
346
424
  return;
347
425
  const s = o.params;
348
- clearTimeout(a), n.dispose(), r(s.result);
426
+ clearTimeout(a), n.dispose(), i(s.result);
349
427
  }), a = setTimeout(() => {
350
- n.dispose(), i(new Error("Cell dependency calculation timeout"));
428
+ n.dispose(), r(new Error("Cell dependency calculation timeout"));
351
429
  }, t);
352
430
  this._commandService.executeCommand(
353
- M.id,
431
+ L.id,
354
432
  e,
355
433
  { onlyLocal: !0 }
356
434
  );
@@ -388,17 +466,17 @@ let l = class extends _ {
388
466
  * ```
389
467
  */
390
468
  getRangeDependents(e, t = 3e4) {
391
- return new Promise((r, i) => {
469
+ return new Promise((i, r) => {
392
470
  const n = this._commandService.onCommandExecuted((o) => {
393
- if (o.id !== d.id)
471
+ if (o.id !== p.id)
394
472
  return;
395
473
  const s = o.params;
396
- clearTimeout(a), n.dispose(), s.result != null ? r(s.result) : r([]);
474
+ clearTimeout(a), n.dispose(), s.result != null ? i(s.result) : i([]);
397
475
  }), a = setTimeout(() => {
398
- n.dispose(), i(new Error("Range dependents calculation timeout"));
476
+ n.dispose(), r(new Error("Range dependents calculation timeout"));
399
477
  }, t);
400
478
  this._commandService.executeCommand(
401
- p.id,
479
+ S.id,
402
480
  { unitRanges: e },
403
481
  { onlyLocal: !0 }
404
482
  );
@@ -440,17 +518,17 @@ let l = class extends _ {
440
518
  * ```
441
519
  */
442
520
  getInRangeFormulas(e, t = 3e4) {
443
- return new Promise((r, i) => {
521
+ return new Promise((i, r) => {
444
522
  const n = this._commandService.onCommandExecuted((o) => {
445
- if (o.id !== d.id)
523
+ if (o.id !== p.id)
446
524
  return;
447
525
  const s = o.params;
448
- clearTimeout(a), n.dispose(), s.result != null ? r(s.result) : r([]);
526
+ clearTimeout(a), n.dispose(), s.result != null ? i(s.result) : i([]);
449
527
  }), a = setTimeout(() => {
450
- n.dispose(), i(new Error("In-range formulas calculation timeout"));
528
+ n.dispose(), r(new Error("In-range formulas calculation timeout"));
451
529
  }, t);
452
530
  this._commandService.executeCommand(
453
- p.id,
531
+ S.id,
454
532
  { unitRanges: e, isInRange: !0 },
455
533
  { onlyLocal: !0 }
456
534
  );
@@ -505,7 +583,7 @@ let l = class extends _ {
505
583
  * ```
506
584
  */
507
585
  setFormulaReturnDependencyTree(e) {
508
- this._configService.setConfig(I, e);
586
+ this._configService.setConfig(A, e);
509
587
  }
510
588
  /**
511
589
  * Parse a formula string and return its **formula expression tree**.
@@ -619,38 +697,38 @@ let l = class extends _ {
619
697
  * ```
620
698
  */
621
699
  getRangeDependentsAndInRangeFormulas(e, t = 3e4) {
622
- return new Promise((r, i) => {
700
+ return new Promise((i, r) => {
623
701
  const n = this._commandService.onCommandExecuted((o) => {
624
- if (o.id !== N.id)
702
+ if (o.id !== O.id)
625
703
  return;
626
704
  const s = o.params;
627
- clearTimeout(a), n.dispose(), s.result != null ? r(s.result) : r({ dependents: [], inRanges: [] });
705
+ clearTimeout(a), n.dispose(), s.result != null ? i(s.result) : i({ dependents: [], inRanges: [] });
628
706
  }), a = setTimeout(() => {
629
- n.dispose(), i(new Error("Range dependents calculation timeout"));
707
+ n.dispose(), r(new Error("Range dependents calculation timeout"));
630
708
  }, t);
631
709
  this._commandService.executeCommand(
632
- B.id,
710
+ U.id,
633
711
  { unitRanges: e },
634
712
  { onlyLocal: !0 }
635
713
  );
636
714
  });
637
715
  }
638
716
  };
639
- l = z([
640
- u(0, c(f)),
641
- u(1, c(C)),
642
- u(2, c(v)),
643
- u(3, h),
644
- u(4, P),
645
- u(5, L),
646
- u(6, O)
647
- ], l);
648
- class V extends S {
717
+ m = Y([
718
+ u(0, c(_)),
719
+ u(1, c(h)),
720
+ u(2, c(g)),
721
+ u(3, v),
722
+ u(4, E),
723
+ u(5, T),
724
+ u(6, x)
725
+ ], m);
726
+ class W extends f {
649
727
  getFormula() {
650
- return this._injector.createInstance(l);
728
+ return this._injector.createInstance(m);
651
729
  }
652
730
  }
653
- S.extend(V);
731
+ f.extend(W);
654
732
  export {
655
- l as FFormula
733
+ m as FFormula
656
734
  };