Перейти к содержимому
GitHubXDiscord

@triggery/reatom

Читай атом или computed из Reatom в условии Triggery. Pull-only: атом вызывается только при срабатывании триггера, ни один подписчик к нему не привязан.

npm bundle

pnpm add @triggery/core @triggery/react @triggery/reatom @reatom/core

Peer-зависимости: react >= 18.0.0, @reatom/core ^1000 || ^1001.

ЭкспортНазначение
useReatomCondition(trigger, name, atom, selector?)Регистрирует условие на основе Reatom. Работает с любым вызываемым примитивом Reatom — атомами, computed, производными значениями.
import { atom } from '@reatom/core';
import { createTrigger } from '@triggery/core';
import { useReatomCondition } from '@triggery/reatom';

type Settings = { sound: boolean; notifications: boolean };

const $settings = atom<Settings>({ sound: true, notifications: true });

const messageTrigger = createTrigger<{
  events: { 'new-message': { text: string } };
  conditions: { settings: Settings };
  actions: { showToast: { body: string } };
}>({
  id: 'message-received',
  events: ['new-message'],
  required: ['settings'],
  handler({ event, conditions, actions }) {
    if (!conditions.settings.notifications) return;
    actions.showToast?.({ body: event.payload.text });
  },
});

function SettingsBridge() {
  useReatomCondition(messageTrigger, 'settings', $settings);
  return null;
}

С селектором-проекцией:

useReatomCondition(messageTrigger, 'settings', $profile, (p) => p.settings);

Pull-only: атом вызывается (atom()) только при срабатывании триггера. Подписчик не регистрируется, поэтому хост-компонент никогда не ререндерится на обновлениях атома. Для компонентов, которым нужно рендерить значение, используй собственные React-биндинги Reatom (reatomComponent, useAtom).

useReatomCondition<V, S, K>(
  trigger: Trigger<S>,
  name: K,
  atom: () => V,
  selector?: (value: V) => ConditionMap<S>[K],
): void