effector / patronum

☄️ Effector operators library delivering modularity and convenience ✨
https://patronum.effector.dev
MIT License
297 stars 43 forks source link

splitMap - added targets field #336

Open earthspacon opened 4 months ago

earthspacon commented 4 months ago

Description

This PR partially resolves this issue.

Often we need the same behavior as in split — react to derived events received from cases and trigger the corresponding units. In targets field we can pass object with the same keys as in cases and values with units to trigger on the corresponding event.

Maybe we should think about targets naming

type WSEvent =
  | { type: 'init'; key: string }
  | { type: 'increment'; count: number }
  | { type: 'reset' };

export const websocketEventReceived = createEvent<WSEvent>();

const $isInitialized = createStore(false);
const $count = createStore<number | null>(null);

const getInitialDataFx = createEffect<string, void>();

splitMap({
  source: websocketEventReceived,
  cases: {
    init: (event) => {
      if (event.type === 'init') return { init: true, dataId: event.key };
    },
    increment: (payload) => {
      if (payload.type === 'increment') return payload.count;
    },
    reset: ({ type }) => {
      if (type === 'reset') return null;
    },
  },
  targets: {
    // EventCallable<{ init?: boolean; dataId?: string }>
    init: spread({ init: $isInitialized, dataId: getInitialDataFx }),
    increment: $count,
    reset: [$count.reinit, $isInitialized.reinit],
  },
});

Checklist

earthspacon commented 3 months ago

@sergeysova @AlexandrHoroshih 😃

sergeysova commented 1 month ago

Added new section to a checklist to help integrating with new documentation website

earthspacon commented 1 month ago

@sergeysova can you help plz with tests in src/split-map/split-map.fork.test.ts, old tests are failing, maybe because new ones added and snapshot updated? image