Skip to content

@@trigger

Protocol that allows start watching some external trigger and react on it with universal API.

Packages that use @@trigger

Known @@trigger

Formulae

Trigger is an any object with the field @@trigger that a function that returns an object with fields:

  • fired: Event, external consumers will listen it to determine when trigger was activated
  • setup: EventCallable, external consumers will call it to set up trigger
  • teardown: EventCallable, external consumers will call it to stop trigger

TIP

Events setup and teardown are presented in protocol, because it is better to provide explicit start of the application.

Single fired

Since @@trigger supports only one fired Event, any operator that supports @@trigger protocol has to choose reasonable Event to use it as fired.

E.g., trackPageVisibility returns Events visible and hidden, but visible seems more reasonable fired Event.

Example

Let's create simple trigger that will be activated every second after starting:

ts
import {
  createEvent,
  createStore,
  createEffect,
  scopeBind,
  sample,
} from 'effector';

const intervalTrigger = {
  '@@trigger': () => {
    const setup = createEvent();
    const fired = createEvent();
    const teardown = createEvent();

    const $interval = createStore(null);

    const startInternalFx = createEffect(() => {
      const boundFired = scopeBind(fired, { safe: true });

      return setInterval(boundFired, 1000);
    });

    const stopIntervalFx = createEffect(clearInterval);

    sample({ clock: setup, target: startInternalFx });
    sample({ clock: startIntervalFx.doneData, target: $interval });
    sample({ clock: teardown, source: $interval, target: stopIntervalFx });
    sample({ clock: stopIntervalFx.done, target: $interval.reinit });

    return { setup, fired };
  },
};

That is it, we can use intervalTrigger everywhere as a trigger!

Released under the MIT License.