Principle:AFK surf Open agent One Time Effect Initialization
| Knowledge Sources | |
|---|---|
| Domains | Extensions, Plugin_System, BlockSuite |
| Last Updated | 2026-10-25 00:00 GMT |
Overview
Pattern of running initialization exactly once per provider class (regardless of how many instances or scopes are created) via an effect() hook, used to register Lit elements and global state.
Description
View providers may override effect() to run one-time initialization that should happen before any editor instance renders. The ext-loader guarantees effect runs only once per provider class, even if the manager creates multiple instances across scopes. Typical uses are registering custom Lit elements (which must be in the global CustomElementRegistry before any DOM using them is parsed), initializing global state, and setting up shared event listeners. effect is view-provider-only; store providers have no effect hook. Keep effect idempotent since it may run across multiple manager instances.
Usage
Apply this principle when a view extension needs global, one-time setup that must precede editor rendering — registering Lit custom elements, seeding a global store, attaching window-level listeners.
Theoretical Basis
effect() is a per-class singleton initializer: effect: () → void, runs ≤ once per class. The ext-loader memoizes execution per class, so the registration side effects are deduplicated. This complements setup, which runs per (scope, options) and may execute many times.
Pseudo-code:
class MyViewProvider extends ViewExtensionProvider {
override effect() {
// Runs once per class, even across multiple manager instances
registerLitElements();
initializeGlobalState();
}
}