diff --git a/src/renderer/shared/effector/helpers/series.test.ts b/src/renderer/shared/effector/helpers/series.test.ts new file mode 100644 index 000000000..383818014 --- /dev/null +++ b/src/renderer/shared/effector/helpers/series.test.ts @@ -0,0 +1,21 @@ +import { createEvent, createWatch } from 'effector'; + +import { series } from './series'; + +describe('series', () => { + it('should spread array into events', () => { + const spy = jest.fn(); + const targetEvent = createEvent(); + const wrappedEvent = series(targetEvent); + + createWatch({ + unit: targetEvent, + fn: spy, + }); + + wrappedEvent([1, 2, 2, 3, 3, 3]); + + expect(spy).toHaveBeenCalledTimes(6); + expect(spy.mock.calls).toEqual([[1], [2], [2], [3], [3], [3]]); + }); +}); diff --git a/src/renderer/shared/effector/helpers/series.ts b/src/renderer/shared/effector/helpers/series.ts index f2358fa7d..61f692847 100644 --- a/src/renderer/shared/effector/helpers/series.ts +++ b/src/renderer/shared/effector/helpers/series.ts @@ -1,6 +1,6 @@ -import { type Effect, type Event, type EventCallable, createEvent, createStore, sample } from 'effector'; +import { type Effect, type EventCallable, createEvent, createStore, sample } from 'effector'; -import { nonNullable } from '@/shared/lib/utils'; +import { nonNullable, nullable } from '@/shared/lib/utils'; /** * Triggers target unit on each element of the input list. @@ -24,13 +24,19 @@ export const series = (target: EventCallable | Effect) => { const push = createEvent | ArrayLike>(); const $queue = createStore([]) - .on(push, (state, payload) => state.concat(Array.from(payload))) + .on(push, (state, payload) => state.concat(Array.isArray(payload) ? payload : Array.from(payload))) .on(pop, ([, ...rest]) => rest); - const $head = $queue.map((queue) => queue.at(0) ?? null); - const nextHeadRetrieved = $head.updates.filter({ fn: nonNullable }) as Event; + const $head = $queue.map((queue) => { + const value = queue.at(0); + if (nullable(value)) return null; + + return { value }; + }); + const nextHeadRetrieved = $head.updates.filter({ fn: nonNullable }); sample({ clock: nextHeadRetrieved, + fn: ({ value }) => value, target: [target, pop], });