Skip to content

Effect Processor API

createEffectProcessor()

Create a processor that manages, tracks, and triggers effects.

An effect is an action that executes in reaction to dependency changes. However, for the effect to know about the changes, it must first be triggered via the triggerEffects method. After the effect is triggered, the processor performs shallow comparison on the dependency to determine if the effect should execute. Effects are executed in the order they are defined.

The processor immediately executes an effect when it is first registered via the trackEffect method. On top of that, the processor also by default batches and schedules effect execution to run after the call stack is empty. This behavior can be modified in the options parameter of the trackEffect method.

ts
function createEffectProcessor(
  params: {
    queue: TaskQueue;
  },
): EffectProcessor

Parameters:

ParameterTypeRequiredDescription
params.queueTaskQueueYesThe queue used for processing scheduled effects.

Returns: EffectProcessor

ts
interface EffectProcessor {
  trackEffect: TrackEffect;
  triggerEffects: () => void;
  toggleActive: (effectEntry: EffectEntry) => void;
}

processor.trackEffect()

  • Type: TrackEffect

Define and track an effect.

The effect's reactivity can be configured through its dependency specification:

  • No array (undefined): The effect always runs when triggered.
  • Empty array ([]): The effect runs only once.
  • Array with values ([a, b]): The effect runs when triggered and any listed value changes.
ts
type TrackEffect = (
  key: PropertyKey,
  effect: () => unknown,
  depsFn?: () => unknown[],
  options?: TrackEffectOptions,
) => EffectEntry
ParameterTypeRequiredDescription
keyPropertyKeyYesA unique key for the effect.
effect() => unknownYesThe function that performs the effect.
depsFn() => unknown[]NoThe function that resolves the effect's dependency values.
optionsTrackEffectOptionsNoOptions to configure the effect's execution behavior.

Returns: EffectEntry
An object describing the registered effect.

TrackEffectOptions

ts
interface TrackEffectOptions {
  runner?: (effectEntry: EffectEntry) => void;
  immediate?: boolean;
  sync?: boolean;
  once?: boolean;
}
PropertyTypeDescription
runner(effectEntry: EffectEntry) => voidThe function that defines how the registered effect function should be run. It is the function that is actually run (instead of the effect function) when dependency changes are detected. It may be used to add extra behavior around effect execution.
Default: (effectEntry) => effectEntry.effect()
immediatebooleanWhether to run the effect immediately on setup.
Default: true
syncbooleanWhether to run the effect synchronously.
Default: false
oncebooleanWhether to run the effect only once.
Default: false

processor.triggerEffects()

  • Type: () => void

Trigger all tracked effects.

When an effect is triggered, the processor performs shallow comparison, executing the effect only if dependency changes are detected.

processor.toggleActive()

  • Type: (effectEntry: EffectEntry) => void

Toggle the active state of an effect.

ParameterTypeRequiredDescription
effectEntryEffectEntryYesAn object containing the effect along with its data.

EffectEntry

Represents a registered effect, containing the data needed to define, track, and execute it.

ts
interface EffectEntry {
  key: PropertyKey;
  effect: () => unknown;
  deps?: unknown[];
  depsFn?: () => unknown[];
  cleanup?: () => void;
  run: () => void;
  active: boolean;
  sync: boolean;
  once: boolean;
  hasRun: boolean;
}

Properties:

PropertyTypeDescription
keyPropertyKeyThe unique key for the effect.
effect() => unknownThe effect function.
depsunknown[]|undefinedThe current dependency values.
depsFn(() => unknown[])|undefinedThe function to resolve the dependency values.
cleanup(() => void)|undefinedThe function to clean up the previous effect run.
run() => voidThe function that defines how the effect function should be run. It may contain extra behavior around effect execution.
activebooleanWhether the effect is active.
syncbooleanWhether the effect should run synchronously.
oncebooleanWhether the effect should run only once.
hasRunbooleanWhether the effect has run.

TaskQueue

Represents a queue for processing tasks.

ts
interface TaskQueue {
  size: () => number;
  add: (key: PropertyKey, task: () => void) => void;
  flush: () => Promise<void>;
}

Properties:

PropertyTypeDescription
size() => numberGet the size of the queue.
add(key: PropertyKey, task: () => void) => voidAdd a task to the queue.
flush() => Promise<void>Process all tasks in the queue and clear it.

Released under the MIT License.