Skip to content

Build your personal knowledge base with Trilium Notes

fix(spreadsheet): defer formula result subscription until Univer registers it

Elian Doran0469eb4529b8
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
index 9a564fc8a2..51b616ec5f 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
@@ -239,17 +239,47 @@ export default function usePersistence(note: FNote, noteContext: NoteContext | n
// save that fires well after the recalc already settled serializes immediately
// instead of waiting on onCalculationResultApplied's idle timeout.
useEffect(() => {
- const formula = apiRef.current?.getFormula?.();
- if (!formula) {
- // The eager-clear optimization is disabled without it; getData() still falls back
- // to onCalculationResultApplied's timeout, so saves stay correct (just slower).
- console.warn("Spreadsheet formula service unavailable; recalc-pending flag will not be cleared eagerly.");
- return;
+ const univerAPI = apiRef.current;
+ if (!univerAPI) return;
+
+ let resultSubscription: { dispose?(): void } | null = null;
+ let lifecycleDisposable: { dispose(): void } | null = null;
+
+ // Univer 0.25+ implements calculationResultApplied by resolving
+ // FormulaCalculationSessionService from the injector, which the formula plugin only
+ // registers once its lifecycle reaches the Starting stage. This effect can run before
+ // createUniver's plugins have started, so subscribing immediately would throw a redi
+ // "dependency not registered" error. Subscribe at the earliest lifecycle change at which
+ // the service exists; also try right away in case the lifecycle already advanced (it can
+ // complete synchronously while the Univer instance is created).
+ const trySubscribe = () => {
+ if (resultSubscription) return true;
+ const formula = univerAPI.getFormula?.();
+ if (!formula) return false;
+ try {
+ resultSubscription = formula.calculationResultApplied(() => {
+ recalcPending.current = false;
+ });
+ return true;
+ } catch {
+ // Formula services not registered yet; a later lifecycle change will retry.
+ return false;
+ }
+ };
+
+ if (!trySubscribe()) {
+ lifecycleDisposable = univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, () => {
+ if (trySubscribe()) {
+ lifecycleDisposable?.dispose();
+ lifecycleDisposable = null;
+ }
+ });
}
- const disposable = formula.calculationResultApplied(() => {
- recalcPending.current = false;
- });
- return () => disposable?.dispose?.();
+
+ return () => {
+ resultSubscription?.dispose?.();
+ lifecycleDisposable?.dispose();
+ };
}, [ apiRef ]);
}