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

@triggery/jotai

Читай атом Jotai из условия Triggery. Pull-only: store.get(atom) вызывается только при срабатывании триггера, хост-компонент никогда не подписывается на обновления атома.

npm bundle

pnpm add @triggery/core @triggery/react @triggery/jotai jotai

Peer-зависимости: react >= 18.0.0, jotai ^2.

ЭкспортНазначение
useJotaiCondition(trigger, name, store, atom, selector?)Регистрирует условие на основе атома. Поддерживает опциональный селектор-проекцию.
import { atom, createStore } from 'jotai';
import { createTrigger } from '@triggery/core';
import { useJotaiCondition } from '@triggery/jotai';

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

const settingsAtom = atom<Settings>({ sound: true, notifications: true });
const store = createStore();

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() {
  useJotaiCondition(messageTrigger, 'settings', store, settingsAtom);
  return null;
}

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

useJotaiCondition(messageTrigger, 'settings', store, profileAtom, (p) => p.settings);

Pull-only: store.get(atom) вызывается только при срабатывании триггера, а не на каждое обновление атома. Хост-компонент никогда не ререндерится из-за изменений атома — это работа useAtomValue, она остаётся в тех компонентах, которые рендерят значение.

useJotaiCondition<V, S, K>(
  trigger: Trigger<S>,
  name: K,
  store: { get<V>(atom: Atom<V>): V },
  atom: Atom<V>,
  selector?: (value: V) => ConditionMap<S>[K],
): void