@univerjs/engine-formula 0.12.4-experimental.20251208-c4b8a44 → 0.12.4-experimental.20251210-8d33c6e

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,13 +1,13 @@
1
- import { FBase as p, FUniver as d } from "@univerjs/core/facade";
2
- import { Inject as c, ICommandService as C, Injector as f, IConfigService as S } from "@univerjs/core";
3
- import { LexerTreeBuilder as _, SetFormulaCalculationStartMutation as u, SetFormulaCalculationStopMutation as v, SetFormulaCalculationNotificationMutation as m, GlobalComputingStatusService as h, ENGINE_FORMULA_CYCLE_REFERENCE_COUNT as x, SetFormulaStringBatchCalculationMutation as g, SetFormulaStringBatchCalculationResultMutation as F, SetFormulaDependencyCalculationMutation as E, SetFormulaDependencyCalculationResultMutation as y, SetCellFormulaDependencyCalculationMutation as M, SetCellFormulaDependencyCalculationResultMutation as B } from "@univerjs/engine-formula";
4
- import { firstValueFrom as T, race as I, filter as D, timer as R, map as L } from "rxjs";
5
- var N = Object.getOwnPropertyDescriptor, O = (e, t, i, r) => {
6
- for (var n = r > 1 ? void 0 : r ? N(t, i) : t, a = e.length - 1, l; a >= 0; a--)
7
- (l = e[a]) && (n = l(n) || n);
1
+ import { FBase as _, FUniver as C } from "@univerjs/core/facade";
2
+ import { Inject as u, ICommandService as v, Injector as h, IConfigService as x } from "@univerjs/core";
3
+ import { LexerTreeBuilder as g, SetFormulaCalculationStartMutation as m, SetFormulaCalculationStopMutation as F, SetFormulaCalculationNotificationMutation as d, GlobalComputingStatusService as E, ENGINE_FORMULA_CYCLE_REFERENCE_COUNT as y, SetFormulaCalculationResultMutation as T, SetFormulaStringBatchCalculationMutation as R, SetFormulaStringBatchCalculationResultMutation as M, SetFormulaDependencyCalculationMutation as D, SetFormulaDependencyCalculationResultMutation as I, SetCellFormulaDependencyCalculationMutation as b, SetCellFormulaDependencyCalculationResultMutation as B, SetQueryFormulaDependencyMutation as p, SetQueryFormulaDependencyResultMutation as f } from "@univerjs/engine-formula";
4
+ import { firstValueFrom as L, race as P, filter as w, timer as N, map as O } from "rxjs";
5
+ var j = Object.getOwnPropertyDescriptor, A = (e, t, i, r) => {
6
+ for (var n = r > 1 ? void 0 : r ? j(t, i) : t, a = e.length - 1, o; a >= 0; a--)
7
+ (o = e[a]) && (n = o(n) || n);
8
8
  return n;
9
- }, o = (e, t) => (i, r) => t(i, r, e);
10
- let s = class extends p {
9
+ }, s = (e, t) => (i, r) => t(i, r, e);
10
+ let c = class extends _ {
11
11
  constructor(e, t, i, r) {
12
12
  super(), this._commandService = e, this._injector = t, this._lexerTreeBuilder = i, this._configService = r, this._initialize();
13
13
  }
@@ -66,7 +66,7 @@ let s = class extends p {
66
66
  * ```
67
67
  */
68
68
  executeCalculation() {
69
- this._commandService.executeCommand(u.id, { commands: [], forceCalculation: !0 }, { onlyLocal: !0 });
69
+ this._commandService.executeCommand(m.id, { commands: [], forceCalculation: !0 }, { onlyLocal: !0 });
70
70
  }
71
71
  /**
72
72
  * Stop the calculation of the formula.
@@ -78,7 +78,7 @@ let s = class extends p {
78
78
  * ```
79
79
  */
80
80
  stopCalculation() {
81
- this._commandService.executeCommand(v.id, {});
81
+ this._commandService.executeCommand(F.id, {});
82
82
  }
83
83
  /**
84
84
  * Listening calculation starts.
@@ -95,7 +95,7 @@ let s = class extends p {
95
95
  */
96
96
  calculationStart(e) {
97
97
  return this._commandService.onCommandExecuted((t) => {
98
- if (t.id === u.id) {
98
+ if (t.id === m.id) {
99
99
  const i = t.params;
100
100
  e(i.forceCalculation);
101
101
  }
@@ -116,7 +116,7 @@ let s = class extends p {
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
121
  const i = t.params;
122
122
  i.functionsExecutedState !== void 0 && e(i.functionsExecutedState);
@@ -139,10 +139,10 @@ let s = class extends p {
139
139
  * ```
140
140
  */
141
141
  whenComputingCompleteAsync(e) {
142
- const t = this._injector.get(h);
143
- return t.computingStatus ? Promise.resolve(!0) : T(I(
144
- t.computingStatus$.pipe(D((i) => i)),
145
- R(e != null ? e : 3e4).pipe(L(() => !1))
142
+ const t = this._injector.get(E);
143
+ return t.computingStatus ? Promise.resolve(!0) : L(P(
144
+ t.computingStatus$.pipe(w((i) => i)),
145
+ N(e != null ? e : 3e4).pipe(O(() => !1))
146
146
  ));
147
147
  }
148
148
  /**
@@ -173,7 +173,7 @@ let s = class extends p {
173
173
  */
174
174
  calculationProcessing(e) {
175
175
  return this._commandService.onCommandExecuted((t) => {
176
- if (t.id !== m.id)
176
+ if (t.id !== d.id)
177
177
  return;
178
178
  const i = t.params;
179
179
  i.stageInfo !== void 0 && e(i.stageInfo);
@@ -192,7 +192,85 @@ let s = class extends p {
192
192
  * ```
193
193
  */
194
194
  setMaxIteration(e) {
195
- this._configService.setConfig(x, e);
195
+ this._configService.setConfig(y, e);
196
+ }
197
+ /**
198
+ * Listens for the moment when formula-calculation results are applied.
199
+ *
200
+ * This event fires after the engine completes a calculation cycle and
201
+ * dispatches a `SetFormulaCalculationResultMutation`.
202
+ * The callback is invoked during an idle frame to avoid blocking UI updates.
203
+ *
204
+ * @param {Function} callback - A function called with the calculation result payload
205
+ * once the result-application mutation is emitted.
206
+ * @returns {IDisposable} A disposable used to unsubscribe from the event.
207
+ *
208
+ * @example
209
+ * ```ts
210
+ * const formulaEngine = univerAPI.getFormula();
211
+ *
212
+ * const dispose = formulaEngine.calculationResultApplied((result) => {
213
+ * console.log('Calculation results applied:', result);
214
+ * });
215
+ *
216
+ * // Later…
217
+ * dispose.dispose();
218
+ * ```
219
+ */
220
+ calculationResultApplied(e) {
221
+ return this._commandService.onCommandExecuted((t) => {
222
+ if (t.id !== T.id)
223
+ return;
224
+ const i = t.params;
225
+ i !== void 0 && requestIdleCallback(() => {
226
+ e(i);
227
+ });
228
+ });
229
+ }
230
+ /**
231
+ * Waits for formula-calculation results to be applied.
232
+ *
233
+ * This method resolves under three conditions:
234
+ * 1. A real calculation runs and the engine emits a "calculation started" signal,
235
+ * followed by a "calculation result applied" signal.
236
+ * 2. No calculation actually starts within 500 ms — the method assumes there is
237
+ * nothing to wait for and resolves automatically.
238
+ * 3. A global 30 s timeout triggers, in which case the promise rejects.
239
+ *
240
+ * The API internally listens to both “calculation in progress” events and
241
+ * “calculation result applied” events, ensuring it behaves correctly whether
242
+ * formulas are recalculated or skipped due to cache/state.
243
+ *
244
+ * @returns {Promise<void>} A promise that resolves when calculation results are applied
245
+ * or when no calculation occurs within the start-detection window.
246
+ *
247
+ * @example
248
+ * ```ts
249
+ * const formulaEngine = univerAPI.getFormula();
250
+ *
251
+ * // Wait for formula updates to apply before reading values.
252
+ * await formulaEngine.onCalculationResultApplied();
253
+ *
254
+ * const value = sheet.getRange("C24").getValue();
255
+ * console.log("Updated value:", value);
256
+ * ```
257
+ */
258
+ onCalculationResultApplied() {
259
+ return new Promise((e, t) => {
260
+ let i = !1, r = !1;
261
+ const n = setTimeout(() => {
262
+ l(), t(new Error("Calculation end timeout"));
263
+ }, 3e4), a = setTimeout(() => {
264
+ i || (l(), e());
265
+ }, 500), o = this.calculationProcessing(() => {
266
+ i || (i = !0, clearTimeout(a));
267
+ }), S = this.calculationResultApplied(() => {
268
+ r || (r = !0, l(), e());
269
+ });
270
+ function l() {
271
+ clearTimeout(n), clearTimeout(a), o.dispose(), S.dispose();
272
+ }
273
+ });
196
274
  }
197
275
  /**
198
276
  * Execute a batch of formulas asynchronously and receive computed results.
@@ -264,9 +342,9 @@ let s = class extends p {
264
342
  * ```
265
343
  */
266
344
  executeFormulas(e, t) {
267
- this._commandService.executeCommand(g.id, { formulas: e }, { onlyLocal: !0 });
345
+ this._commandService.executeCommand(R.id, { formulas: e }, { onlyLocal: !0 });
268
346
  const i = this._commandService.onCommandExecuted((r) => {
269
- if (r.id !== F.id)
347
+ if (r.id !== M.id)
270
348
  return;
271
349
  const n = r.params;
272
350
  n.result != null && t(n.result), i.dispose();
@@ -293,9 +371,9 @@ let s = class extends p {
293
371
  * ```
294
372
  */
295
373
  getAllDependencyTrees(e) {
296
- this._commandService.executeCommand(E.id, void 0, { onlyLocal: !0 });
374
+ this._commandService.executeCommand(D.id, void 0, { onlyLocal: !0 });
297
375
  const t = this._commandService.onCommandExecuted((i) => {
298
- if (i.id !== y.id)
376
+ if (i.id !== I.id)
299
377
  return;
300
378
  const r = i.params;
301
379
  r.result != null && e(r.result), t.dispose();
@@ -332,7 +410,7 @@ let s = class extends p {
332
410
  * ```
333
411
  */
334
412
  getCellDependencyTree(e, t) {
335
- this._commandService.executeCommand(M.id, e, { onlyLocal: !0 });
413
+ this._commandService.executeCommand(b.id, e, { onlyLocal: !0 });
336
414
  const i = this._commandService.onCommandExecuted((r) => {
337
415
  if (r.id !== B.id)
338
416
  return;
@@ -340,19 +418,96 @@ let s = class extends p {
340
418
  n.result !== void 0 && t(n.result), i.dispose();
341
419
  });
342
420
  }
421
+ /**
422
+ * Retrieve the full dependency trees for all formulas that *depend on* the
423
+ * specified ranges. This triggers a local dependency-calculation command and
424
+ * invokes the callback once the calculation completes.
425
+ *
426
+ * @param unitRanges An array of workbook/sheet ranges to query. Each range
427
+ * includes:
428
+ * - `unitId` The workbook ID.
429
+ * - `sheetId` The sheet ID.
430
+ * - `range` The row/column boundaries.
431
+ *
432
+ * @param callback A function invoked with an array of `IFormulaDependencyTreeJson`
433
+ * results. Each entry represents a formula node and its parent/child
434
+ * relationships within the dependency graph.
435
+ *
436
+ * @example
437
+ * ```ts
438
+ * const formulaEngine = univerAPI.getFormula();
439
+ *
440
+ * // Query all formulas that depend on A1:B10 in Sheet1.
441
+ * formulaEngine.getRangeDependents(
442
+ * [{ unitId: 'workbook1', sheetId: 'sheet1', range: { startRow: 0, endRow: 9, startColumn: 0, endColumn: 1 } }],
443
+ * (result) => {
444
+ * console.log('Dependent formulas:', result);
445
+ * }
446
+ * );
447
+ * ```
448
+ */
449
+ getRangeDependents(e, t) {
450
+ this._commandService.executeCommand(p.id, { unitRanges: e }, { onlyLocal: !0 });
451
+ const i = this._commandService.onCommandExecuted((r) => {
452
+ if (r.id !== f.id)
453
+ return;
454
+ const n = r.params;
455
+ n.result != null && t(n.result), i.dispose();
456
+ });
457
+ }
458
+ /**
459
+ * Retrieve the dependency trees of all formulas *inside* the specified ranges.
460
+ * Unlike `getRangeDependents`, this API only returns formulas whose definitions
461
+ * physically reside within the queried ranges.
462
+ *
463
+ * Internally this triggers the same dependency-calculation command but with
464
+ * `isInRange = true`, and the callback is invoked when the results are ready.
465
+ *
466
+ * @param unitRanges An array of workbook/sheet ranges defining the lookup
467
+ * boundaries:
468
+ * - `unitId` The workbook ID.
469
+ * - `sheetId` The sheet ID.
470
+ * - `range` The zero-based grid range.
471
+ *
472
+ * @param callback Receives an array of `IFormulaDependencyTreeJson` describing
473
+ * every formula found in the provided ranges along with their parent/child
474
+ * relationships.
475
+ *
476
+ * @example
477
+ * ```ts
478
+ * const formulaEngine = univerAPI.getFormula();
479
+ *
480
+ * // Query all formulas that lie within A1:D20 in Sheet1.
481
+ * formulaEngine.getInRangeFormulas(
482
+ * [{ unitId: 'workbook1', sheetId: 'sheet1', range: { startRow: 0, endRow: 19, startColumn: 0, endColumn: 3 } }],
483
+ * (result) => {
484
+ * console.log('Formulas inside range:', result);
485
+ * }
486
+ * );
487
+ * ```
488
+ */
489
+ getInRangeFormulas(e, t) {
490
+ this._commandService.executeCommand(p.id, { unitRanges: e, isInRange: !0 }, { onlyLocal: !0 });
491
+ const i = this._commandService.onCommandExecuted((r) => {
492
+ if (r.id !== f.id)
493
+ return;
494
+ const n = r.params;
495
+ n.result != null && t(n.result), i.dispose();
496
+ });
497
+ }
343
498
  };
344
- s = O([
345
- o(0, c(C)),
346
- o(1, c(f)),
347
- o(2, c(_)),
348
- o(3, S)
349
- ], s);
350
- class b extends d {
499
+ c = A([
500
+ s(0, u(v)),
501
+ s(1, u(h)),
502
+ s(2, u(g)),
503
+ s(3, x)
504
+ ], c);
505
+ class U extends C {
351
506
  getFormula() {
352
- return this._injector.createInstance(s);
507
+ return this._injector.createInstance(c);
353
508
  }
354
509
  }
355
- d.extend(b);
510
+ C.extend(U);
356
511
  export {
357
- s as FFormula
512
+ c as FFormula
358
513
  };