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

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/es/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 h, FUniver as S } from "@univerjs/core/facade";
2
+ import { Inject as c, ICommandService as _, Injector as v, IConfigService as g } from "@univerjs/core";
3
+ import { LexerTreeBuilder as x, SetFormulaCalculationStartMutation as d, SetFormulaCalculationStopMutation as E, SetFormulaCalculationNotificationMutation as p, GlobalComputingStatusService as F, ENGINE_FORMULA_CYCLE_REFERENCE_COUNT as T, SetFormulaCalculationResultMutation as y, SetFormulaStringBatchCalculationResultMutation as w, SetFormulaStringBatchCalculationMutation as R, SetFormulaDependencyCalculationResultMutation as M, SetFormulaDependencyCalculationMutation as b, SetCellFormulaDependencyCalculationResultMutation as D, SetCellFormulaDependencyCalculationMutation as I, SetQueryFormulaDependencyResultMutation as f, SetQueryFormulaDependencyMutation as C } from "@univerjs/engine-formula";
4
+ import { firstValueFrom as P, race as B, filter as L, timer as N, map as O } from "rxjs";
5
+ var A = Object.getOwnPropertyDescriptor, j = (e, t, i, r) => {
6
+ for (var n = r > 1 ? void 0 : r ? A(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
+ }, u = (e, t) => (i, r) => t(i, r, e);
10
+ let m = class extends h {
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(d.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(E.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 === d.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 !== p.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(F);
143
+ return t.computingStatus ? Promise.resolve(!0) : P(B(
144
+ t.computingStatus$.pipe(L((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 !== p.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(T, 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 !== y.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.
@@ -221,11 +299,13 @@ let s = class extends p {
221
299
  * Nested structure (unit → sheet → row → column) describing formulas and
222
300
  * their decomposed sub-expressions.
223
301
  *
224
- * @param {(result: IFormulaExecuteResultMap) => void} callback
225
- * Receives the computed value map mirroring the input structure.
302
+ * @param {number} [timeout]
303
+ * Optional timeout in milliseconds. If no result is received within this
304
+ * period, the promise will be rejected.
226
305
  *
227
- * @returns {IDisposable}
228
- * A disposer to stop listening for batch results.
306
+ * @returns {Promise<IFormulaExecuteResultMap>}
307
+ * A promise that resolves with the computed value map mirroring
308
+ * the input structure.
229
309
  *
230
310
  * @example
231
311
  * ```ts
@@ -257,19 +337,25 @@ let s = class extends p {
257
337
  * },
258
338
  * };
259
339
  *
260
- * const disposer = formulaEngine.executeFormulas(formulas, (result) => {
261
- * console.log(result);
262
- * });
263
- *
340
+ * const result = await formulaEngine.executeFormulas(formulas);
341
+ * console.log(result);
264
342
  * ```
265
343
  */
266
- executeFormulas(e, t) {
267
- this._commandService.executeCommand(g.id, { formulas: e }, { onlyLocal: !0 });
268
- const i = this._commandService.onCommandExecuted((r) => {
269
- if (r.id !== F.id)
270
- return;
271
- const n = r.params;
272
- n.result != null && t(n.result), i.dispose();
344
+ executeFormulas(e, t = 3e4) {
345
+ return new Promise((i, r) => {
346
+ const n = this._commandService.onCommandExecuted((o) => {
347
+ if (o.id !== w.id)
348
+ return;
349
+ const s = o.params;
350
+ clearTimeout(a), n.dispose(), s.result != null ? i(s.result) : r(new Error("Formula batch calculation returned no result"));
351
+ }), a = setTimeout(() => {
352
+ n.dispose(), r(new Error("Formula batch calculation timeout"));
353
+ }, t);
354
+ this._commandService.executeCommand(
355
+ R.id,
356
+ { formulas: e },
357
+ { onlyLocal: !0 }
358
+ );
273
359
  });
274
360
  }
275
361
  /**
@@ -277,28 +363,37 @@ let s = class extends p {
277
363
  * dependency-analysis run. This triggers a local dependency-calculation command
278
364
  * and returns the complete set of dependency trees once the calculation finishes.
279
365
  *
280
- * @param callback A function invoked with the resulting array of dependency trees.
366
+ * @param {number} [timeout]
367
+ * Optional timeout in milliseconds. If no result is received within this
368
+ * period, the promise will be rejected.
281
369
  *
282
- * @returns {IDisposable} An object that disposes the internal event listener.
370
+ * @returns {Promise<IFormulaDependencyTreeJson[]>}
371
+ * A promise that resolves with the array of dependency trees.
283
372
  *
284
373
  * @example
285
374
  * ```ts
286
375
  * const formulaEngine = univerAPI.getFormula();
287
376
  *
288
377
  * // Fetch all dependency trees generated for the current workbook.
289
- * const disposable = formulaEngine.getAllDependencyTrees((trees) => {
290
- * console.log('All dependency trees:', trees);
291
- * });
292
- *
378
+ * const trees = await formulaEngine.getAllDependencyTrees();
379
+ * console.log('All dependency trees:', trees);
293
380
  * ```
294
381
  */
295
- getAllDependencyTrees(e) {
296
- this._commandService.executeCommand(E.id, void 0, { onlyLocal: !0 });
297
- const t = this._commandService.onCommandExecuted((i) => {
298
- if (i.id !== y.id)
299
- return;
300
- const r = i.params;
301
- r.result != null && e(r.result), t.dispose();
382
+ getAllDependencyTrees(e = 3e4) {
383
+ return new Promise((t, i) => {
384
+ const r = this._commandService.onCommandExecuted((a) => {
385
+ if (a.id !== M.id)
386
+ return;
387
+ const o = a.params;
388
+ clearTimeout(n), r.dispose(), o.result != null ? t(o.result) : t([]);
389
+ }), n = setTimeout(() => {
390
+ r.dispose(), i(new Error("Formula dependency calculation timeout"));
391
+ }, e);
392
+ this._commandService.executeCommand(
393
+ b.id,
394
+ void 0,
395
+ { onlyLocal: !0 }
396
+ );
302
397
  });
303
398
  }
304
399
  /**
@@ -312,47 +407,159 @@ let s = class extends p {
312
407
  * - `row` The zero-based row index.
313
408
  * - `column` The zero-based column index.
314
409
  *
315
- * @param callback A function invoked with the resulting dependency tree or
316
- * `undefined` if no dependency tree exists for that cell.
410
+ * @param {number} [timeout]
411
+ * Optional timeout in milliseconds. If no result is received within this
412
+ * period, the promise will be rejected.
317
413
  *
318
- * @returns {IDisposable} An object that disposes the internal event listener.
414
+ * @returns {Promise<IFormulaDependencyTreeFullJson | undefined>}
415
+ * A promise that resolves with the dependency tree or `undefined`
416
+ * if no tree exists for that cell.
319
417
  *
320
418
  * @example
321
419
  * ```ts
322
420
  * const formulaEngine = univerAPI.getFormula();
323
421
  *
324
422
  * // Query the dependency tree for cell B2 in a specific sheet.
325
- * const disposable = formulaEngine.getCellDependencyTree(
326
- * { unitId: 'workbook1', sheetId: 'sheet1', row: 1, column: 1 },
327
- * (tree) => {
328
- * console.log('Cell dependency tree:', tree);
329
- * }
330
- * );
423
+ * const tree = await formulaEngine.getCellDependencyTree({
424
+ * unitId: 'workbook1',
425
+ * sheetId: 'sheet1',
426
+ * row: 1,
427
+ * column: 1,
428
+ * });
331
429
  *
430
+ * console.log('Cell dependency tree:', tree);
332
431
  * ```
333
432
  */
334
- getCellDependencyTree(e, t) {
335
- this._commandService.executeCommand(M.id, e, { onlyLocal: !0 });
336
- const i = this._commandService.onCommandExecuted((r) => {
337
- if (r.id !== B.id)
338
- return;
339
- const n = r.params;
340
- n.result !== void 0 && t(n.result), i.dispose();
433
+ getCellDependencyTree(e, t = 3e4) {
434
+ return new Promise((i, r) => {
435
+ const n = this._commandService.onCommandExecuted((o) => {
436
+ if (o.id !== D.id)
437
+ return;
438
+ const s = o.params;
439
+ clearTimeout(a), n.dispose(), i(s.result);
440
+ }), a = setTimeout(() => {
441
+ n.dispose(), r(new Error("Cell dependency calculation timeout"));
442
+ }, t);
443
+ this._commandService.executeCommand(
444
+ I.id,
445
+ e,
446
+ { onlyLocal: !0 }
447
+ );
448
+ });
449
+ }
450
+ /**
451
+ * Retrieve the full dependency trees for all formulas that *depend on* the
452
+ * specified ranges. This triggers a local dependency-calculation command and
453
+ * resolves once the calculation completes.
454
+ *
455
+ * @param unitRanges An array of workbook/sheet ranges to query. Each range
456
+ * includes:
457
+ * - `unitId` The workbook ID.
458
+ * - `sheetId` The sheet ID.
459
+ * - `range` The row/column boundaries.
460
+ *
461
+ * @param {number} [timeout]
462
+ * Optional timeout in milliseconds. If no result is received within this
463
+ * period, the promise will be rejected.
464
+ *
465
+ * @returns {Promise<IFormulaDependencyTreeJson[]>}
466
+ * A promise that resolves with an array of `IFormulaDependencyTreeJson`
467
+ * representing formulas and their relationships within the dependency graph.
468
+ *
469
+ * @example
470
+ * ```ts
471
+ * const formulaEngine = univerAPI.getFormula();
472
+ *
473
+ * // Query all formulas that depend on A1:B10 in Sheet1.
474
+ * const dependents = await formulaEngine.getRangeDependents([
475
+ * { unitId: 'workbook1', sheetId: 'sheet1', range: { startRow: 0, endRow: 9, startColumn: 0, endColumn: 1 } }
476
+ * ]);
477
+ *
478
+ * console.log('Dependent formulas:', dependents);
479
+ * ```
480
+ */
481
+ getRangeDependents(e, t = 3e4) {
482
+ return new Promise((i, r) => {
483
+ const n = this._commandService.onCommandExecuted((o) => {
484
+ if (o.id !== f.id)
485
+ return;
486
+ const s = o.params;
487
+ clearTimeout(a), n.dispose(), s.result != null ? i(s.result) : i([]);
488
+ }), a = setTimeout(() => {
489
+ n.dispose(), r(new Error("Range dependents calculation timeout"));
490
+ }, t);
491
+ this._commandService.executeCommand(
492
+ C.id,
493
+ { unitRanges: e },
494
+ { onlyLocal: !0 }
495
+ );
496
+ });
497
+ }
498
+ /**
499
+ * Retrieve the dependency trees of all formulas *inside* the specified ranges.
500
+ * Unlike `getRangeDependents`, this API only returns formulas whose definitions
501
+ * physically reside within the queried ranges.
502
+ *
503
+ * Internally this triggers the same dependency-calculation command but with
504
+ * `isInRange = true`, and the promise resolves when the results are ready.
505
+ *
506
+ * @param unitRanges An array of workbook/sheet ranges defining the lookup
507
+ * boundaries:
508
+ * - `unitId` The workbook ID.
509
+ * - `sheetId` The sheet ID.
510
+ * - `range` The zero-based grid range.
511
+ *
512
+ * @param {number} [timeout]
513
+ * Optional timeout in milliseconds. If no result is received within this
514
+ * period, the promise will be rejected.
515
+ *
516
+ * @returns {Promise<IFormulaDependencyTreeJson[]>}
517
+ * A promise that resolves with an array of `IFormulaDependencyTreeJson`
518
+ * describing every formula found in the provided ranges along with
519
+ * their parent/child relationships.
520
+ *
521
+ * @example
522
+ * ```ts
523
+ * const formulaEngine = univerAPI.getFormula();
524
+ *
525
+ * // Query all formulas that lie within A1:D20 in Sheet1.
526
+ * const formulasInRange = await formulaEngine.getInRangeFormulas([
527
+ * { unitId: 'workbook1', sheetId: 'sheet1', range: { startRow: 0, endRow: 19, startColumn: 0, endColumn: 3 } }
528
+ * ]);
529
+ *
530
+ * console.log('Formulas inside range:', formulasInRange);
531
+ * ```
532
+ */
533
+ getInRangeFormulas(e, t = 3e4) {
534
+ return new Promise((i, r) => {
535
+ const n = this._commandService.onCommandExecuted((o) => {
536
+ if (o.id !== f.id)
537
+ return;
538
+ const s = o.params;
539
+ clearTimeout(a), n.dispose(), s.result != null ? i(s.result) : i([]);
540
+ }), a = setTimeout(() => {
541
+ n.dispose(), r(new Error("In-range formulas calculation timeout"));
542
+ }, t);
543
+ this._commandService.executeCommand(
544
+ C.id,
545
+ { unitRanges: e, isInRange: !0 },
546
+ { onlyLocal: !0 }
547
+ );
341
548
  });
342
549
  }
343
550
  };
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 {
551
+ m = j([
552
+ u(0, c(_)),
553
+ u(1, c(v)),
554
+ u(2, c(x)),
555
+ u(3, g)
556
+ ], m);
557
+ class U extends S {
351
558
  getFormula() {
352
- return this._injector.createInstance(s);
559
+ return this._injector.createInstance(m);
353
560
  }
354
561
  }
355
- d.extend(b);
562
+ S.extend(U);
356
563
  export {
357
- s as FFormula
564
+ m as FFormula
358
565
  };