From 8e76e5c867649ca535efe5505f2871463c111d83 Mon Sep 17 00:00:00 2001 From: earthspacon Date: Fri, 27 Sep 2024 13:05:43 +0500 Subject: [PATCH] feat(splitMap): added jsdoc --- src/split-map/index.ts | 70 ++++++++++++++++++++++++++++++++++++++++- src/split-map/readme.md | 4 ++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/split-map/index.ts b/src/split-map/index.ts index ff0a2868..7fa3266f 100644 --- a/src/split-map/index.ts +++ b/src/split-map/index.ts @@ -9,6 +9,74 @@ const hasPropBase = {}.hasOwnProperty; const hasOwnProp = (object: O, key: string) => hasPropBase.call(object, key); +/** + * Split `source` unit into multiple events based on the provided `cases`. + * + * @param source - Source unit, data from this unit is passed to each function in cases object and `__` event in shape as is + * @param cases - Object of functions. Function receives one argument is a payload from `source`, should return any value or `undefined`. + * If `undefined` is returned from the case function, update will be skipped (the event will not be triggered). + * + * @param targets (optional) - Object of units to trigger on corresponding event from cases object + * @returns Object of events, with the same structure as `cases`, but with the default event `__`, that will be triggered when each other function returns `undefined` + * + * @example + * ```ts + * const dataFetched = createEvent() + * + * const dataReceived = splitMap({ + * source: dataFetched, + * cases: { + * isString: (payload) => { + * if (typeof payload === 'string') return payload + * }, + * isNumber: (payload) => { + * if (typeof payload === 'number') return payload + * }, + * } + * }) + + * dataReceived.isString // Event + * dataReceived.isNumber // Event + * dataReceived.__ // Event + * ``` + * + * @example + * ```ts + * const dataFetched = createEvent() + * const stringReceived = createEvent() + * const numberReceived = createEvent() + * const unknownReceived = createEvent() + * const notifyError = createEvent() + * + * const dataReceived = splitMap({ + * source: dataFetched, + * cases: { + * isString: (payload) => { + * if (typeof payload === 'string') return payload + * }, + * isNumber: (payload) => { + * if (typeof payload === 'number') return payload + * }, + * }, + * targets: { + * isString: stringReceived, + * isNumber: numberReceived, + * __: [unknownReceived, notifyError], + * }, + * }) + * + * dataFetched('string') + * // => stringReceived('string') + * + * dataFetched(42) + * // => numberReceived(42) + * + * dataFetched(null) + * // => unknownReceived(null) + * // => notifyError() + * ``` + */ + export function splitMap< S, Cases extends Record any | undefined>, @@ -64,7 +132,7 @@ export function splitMap< // eslint-disable-next-line no-underscore-dangle result.__ = current; - if (targets && '__' in targets) { + if (targets && hasOwnProp(targets, '__')) { const defaultCaseTarget = targets.__; sample({ diff --git a/src/split-map/readme.md b/src/split-map/readme.md index abd1de12..689d1451 100644 --- a/src/split-map/readme.md +++ b/src/split-map/readme.md @@ -142,7 +142,7 @@ splitMap({ source, cases, targets }); ### Returns -- `shape` (`{ [key: string]: Event; __: Event }`) — Object of events, with the same structure as `cases`, but with the _default_ event `__`, that triggered when each other function returns `undefined` +- `shape` (`{ [key: string]: Event; __: Event }`) — Object of events, with the same structure as `cases`, but with the _default_ event `__`, that will be triggered when each other function returns `undefined` [_`event`_]: https://effector.dev/docs/api/effector/event [_`effect`_]: https://effector.dev/docs/api/effector/effect @@ -163,6 +163,7 @@ type WSEvent = | { type: 'reset' }; export const websocketEventReceived = createEvent(); +const notifyError = createEvent(); const $isInitialized = createStore(false); const $count = createStore(null); @@ -187,6 +188,7 @@ splitMap({ init: spread({ init: $isInitialized, dataId: getInitialDataFx }), increment: $count, reset: [$count.reinit, $isInitialized.reinit], + __: notifyError, }, });