Lifecycle
Global lifecycle
Univer passes through four global stages during startup:
- Starting: initialization, before any business unit exists.
- Ready: the first business unit has been created. Later units do not trigger this stage again.
- Rendered: the standard UI has performed its first render switch, although asynchronous content may still be loading.
- Steady: the standard UI reaches this stage after a delay following Rendered. It does not wait for all asynchronous tasks.
Facade API
const currentStage = univerAPI.getCurrentLifecycleStage()const disposable = univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, ({ stage }) => { console.log(stage)})What the event reports
LifeCycleChanged reports the global stage of Univer. If plugin B loads after the global Ready stage, B still runs onReady(), but no second global Ready event occurs.
When a listener is added, its callback may receive the current stage immediately, including Starting. To determine the current stage without a listener, call univerAPI.getCurrentLifecycleStage().
To remove a listener, use the returned disposable object:
disposable.dispose()Plugin lifecycle hooks
A plugin runs onStarting() when it loads and then catches up with the current global stage. Load plugin B at different points to compare its hook calls with those of plugin A.
Slowed down to show synchronous hook calls.
General plugins load when they are registered. Business-type plugins usually load before the first unit of that type is constructed. Plugins added for a type that has already started load in a deferred batch. An explicit dependency can cause a plugin to load earlier.
Hooks in each batch run synchronously, stage by stage and in dependency order, and asynchronous work inside a hook is not awaited. Before accessing a specific unit, DOM element, or resource, confirm that it is ready.
How is this guide?