Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(engine-formula): add whenComputingCompleteAsync facade (#4610)
Wenzhao Hu7db54746e0ed
diff --git a/packages/engine-formula/src/facade/f-formula.ts b/packages/engine-formula/src/facade/f-formula.tsindex 292715dc4f..5a3b160f63 100644--- a/packages/engine-formula/src/facade/f-formula.ts+++ b/packages/engine-formula/src/facade/f-formula.ts@@ -17,7 +17,8 @@import type { ICommandInfo, IDisposable } from '@univerjs/core';import type { FormulaExecutedStateType, IExecutionInProgressParams, ISequenceNode, ISetFormulaCalculationNotificationMutation, ISetFormulaCalculationStartMutation } from '@univerjs/engine-formula';import { FBase, ICommandService, IConfigService, Inject, Injector } from '@univerjs/core';-import { ENGINE_FORMULA_CYCLE_REFERENCE_COUNT, LexerTreeBuilder, SetFormulaCalculationNotificationMutation, SetFormulaCalculationStartMutation, SetFormulaCalculationStopMutation } from '@univerjs/engine-formula';+import { ENGINE_FORMULA_CYCLE_REFERENCE_COUNT, GlobalComputingStatusService, LexerTreeBuilder, SetFormulaCalculationNotificationMutation, SetFormulaCalculationStartMutation, SetFormulaCalculationStopMutation } from '@univerjs/engine-formula';+import { filter, firstValueFrom, map, race, timer } from 'rxjs';/*** This interface class provides methods to modify the behavior of the operation formula.@@ -118,6 +119,28 @@ export class FFormula extends FBase {});}+ /**+ * Wait for computing in the Univer instance to complete. Please note that this does not only include formula calculation,+ * but also other computing tasks, e.g. pivot table calculation.+ * @param {number} [timeout] The maximum time to wait for the computing to complete, in milliseconds. The default+ * value is 30,000 milliseconds.+ * @returns {Promise<boolean>} This method returns `true` if the computing is complete. If the timeout is reached, this+ * method returns `false`.+ */+ whenComputingCompleteAsync(timeout?: number): Promise<boolean> {+ const gcss = this._injector.get(GlobalComputingStatusService);+ if (gcss.computingStatus) return Promise.resolve(true);++ return firstValueFrom(race(+ gcss.computingStatus$.pipe(filter((computing) => computing)),+ timer(timeout ?? 30_000).pipe(map(() => false))+ ));+ }++ /**+ * @deprecated Use `whenComputingCompleteAsync` instead.+ * @returns {Promise<void>} This method returns a promise that resolves when the calculation is complete.+ */onCalculationEnd(): Promise<void> {return new Promise((resolve, reject) => {const timer = setTimeout(() => {diff --git a/packages/engine-formula/src/services/global-computing-status.service.ts b/packages/engine-formula/src/services/global-computing-status.service.tsindex 224ae299ef..c6ae465a65 100644--- a/packages/engine-formula/src/services/global-computing-status.service.ts+++ b/packages/engine-formula/src/services/global-computing-status.service.ts@@ -27,6 +27,8 @@ export class GlobalComputingStatusService extends Disposable {private readonly _computingStatus$ = new BehaviorSubject<ComputingStatus>(true);readonly computingStatus$ = this._computingStatus$.pipe(distinctUntilChanged());+ get computingStatus(): ComputingStatus { return this._computingStatus$.getValue(); }+private _computingSubscription: Nullable<Subscription>;override dispose(): void {