Skip to content

Commit

Permalink
feat(splitMap): added jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
earthspacon committed Sep 27, 2024
1 parent 9e2340e commit 8e76e5c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
70 changes: 69 additions & 1 deletion src/split-map/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,74 @@ const hasPropBase = {}.hasOwnProperty;
const hasOwnProp = <O extends { [k: string]: unknown }>(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<unknown>()
*
* 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<string>
* dataReceived.isNumber // Event<number>
* dataReceived.__ // Event<unknown>
* ```
*
* @example
* ```ts
* const dataFetched = createEvent<unknown>()
* const stringReceived = createEvent<string>()
* const numberReceived = createEvent<number>()
* const unknownReceived = createEvent<unknown>()
* 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<string, (payload: S) => any | undefined>,
Expand Down Expand Up @@ -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({
Expand Down
4 changes: 3 additions & 1 deletion src/split-map/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ splitMap({ source, cases, targets });

### Returns

- `shape` (`{ [key: string]: Event<any>; __: Event<T> }`) — 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<any>; __: Event<T> }`) — 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
Expand All @@ -163,6 +163,7 @@ type WSEvent =
| { type: 'reset' };

export const websocketEventReceived = createEvent<WSEvent>();
const notifyError = createEvent();

const $isInitialized = createStore(false);
const $count = createStore<number | null>(null);
Expand All @@ -187,6 +188,7 @@ splitMap({
init: spread({ init: $isInitialized, dataId: getInitialDataFx }),
increment: $count,
reset: [$count.reinit, $isInitialized.reinit],
__: notifyError,
},
});

Expand Down

0 comments on commit 8e76e5c

Please sign in to comment.