effect
functionDeclare background behavior that starts when the mod activates. The body runs once at activation, then again whenever any state or signal it read has changed. Dependencies are tracked automatically, so there is nothing to subscribe to and nothing to unsubscribe from. Every listener, interceptor, and subscription the body registers through `ctx` belongs to that one run and is torn down before the next one. Reach for an effect when behavior should stay live for as long as the mod is loaded, instead of firing when someone triggers it. A `command` runs when the user picks it. A `system.on` listener answers one host event source. An effect is the standing one, and it is also the only place `ctx.hooks.beforeAction` interceptors can be registered. The body must be synchronous. Returning a promise throws. Put asynchronous work inside an event handler or an action call. Return a cleanup function or a `Disposable` for anything the SDK does not already own.
Import
import { effect } from '@modular/sdk';Notes
- example
- ```ts effect({ id: 'my-mod.selection' }, ctx => { const selection = ctx.state('editor.selection'); ctx.logger.info('selection changed', selection.value); const timer = setInterval(() => ctx.logger.info('still watching'), 60_000); return () => clearInterval(timer); }); ```
- category
- Authoring