Skip to content

effect

effect(effect): object

Defined in: behavior.types.action.ts:203

Performs a side effect.

Use effect for logging, analytics, async operations, or other side effects.

Note: Using effect alone (without forward) will stop event propagation. To perform a side effect while allowing the default Behavior to continue, combine effect with forward.

The effect callback receives a send function that can be used to send events back to the editor asynchronously.

(payload) => void

object

effect: (payload) => void

(event) => void

Send a Behavior Event back into the Editor.

Example

defineBehavior({
on: '...',
actions: [
() => [
effect(({send}) => {
doSomethingAsync()
.then(() => {
send({
type: '...',
})
})
})
],
],
})

void

type: "effect"

// Log events while preserving default Behavior
defineBehavior({
on: 'insert.text',
actions: [({event}) => [effect(() => console.log(event)), forward(event)]],
})
// Effect alone stops propagation (native event is cancelled)
defineBehavior({
on: 'keyboard.keydown',
actions: [() => [effect(() => console.log('key pressed'))]],
})
// Async effect that sends an event later
defineBehavior({
on: 'custom.save',
actions: [
() => [
effect(async ({send}) => {
await saveDocument()
send({type: 'custom.saved'})
}),
],
],
})