UNPKG

imng-ngrx-utils

Version:

This library was generated with [Nx](https://nx.dev).

126 lines (111 loc) 3.82 kB
import { createAction } from '@ngrx/store'; import { catchError, of, filter, map } from 'rxjs'; import { concatLatestFrom } from '@ngrx/operators'; // tslint:disable-next-line: typedef , doing otherwise would involve having deep imports on the @ngrx library function createPayloadAction(actionType) { return createAction(actionType, (payload) => ({ payload })); } // tslint:disable-next-line: typedef , doing otherwise would involve having deep imports on the @ngrx library function createModalAction(actionType) { return createAction(actionType, (payload) => ({ payload })); } function isTruthy(value) { return !!value; } function isFalsy(value) { return !value; } function findAndModify(data, lookupId, modificationLogic) { return [...data.map((m) => (m.id === lookupId ? applyChanges(m, modificationLogic) : m))]; } function applyChanges(record, modificationLogic) { const spreadRecord = { ...record }; modificationLogic(spreadRecord); return spreadRecord; } function findAndMerge(record, data) { return { ...data.find((f) => f.id === record.id), ...record, }; } /** * Spreads the objects in the dataset1, with a matching id object from the dataset2. * @param dataset1 The original dataset, these are the records you can expect to get back. * @param dataset2 This dataset will be used to augment the individuals records in dataSet1, records will be matched based on 'id'. * @returns dataset1 */ function matchAndSpread(dataset1, dataset2) { return dataset1.map((d1) => findAndMerge(d1, dataset2)); } function removeById(source, id) { if (Array.isArray(source)) { const result = source; return result?.filter((f) => f.id !== id); } else if (source) { const data = source.data?.filter((f) => f.id !== id); return { total: source.total + (data?.length - source.data?.length) || 0, data, }; } return null; } class Subscriptions { constructor(...items) { this._subscriptions = items; } get length() { return this._subscriptions.length; } push(...items) { this._subscriptions.push(...items.filter((t) => t)); } forEach(callbackfn, thisArg) { this._subscriptions .map((t) => t) .forEach(callbackfn, thisArg); } unsubscribeAll() { while (this._subscriptions.length > 0) { const val = this._subscriptions.pop(); val?.unsubscribe(); } } } const imngEffectError = createPayloadAction('[IMNG] Error Occured'); /** * Will handle effect error and dispatch an imngEffectError action * @param action * @returns */ function handleEffectError(action) { return catchError((error) => of(imngEffectError({ action, error }))); } function imngEffectErrorReducer(state, effectError) { return { ...state, loading: false, error: effectError.payload.error, }; } function getById(source, id) { if (Array.isArray(source)) { return source.find((f) => f.id === id); } return source.data?.find((f) => f.id === id); } function sleep(milliSeconds) { return new Promise(resolve => setTimeout(resolve, milliSeconds)); } function useCacheIfExists(store, selector) { return function (source) { return source.pipe(concatLatestFrom(() => store.select(selector)), filter(([, cache]) => !cache || cache.length === 0), map(([action]) => action)); }; } /** * Generated bundle index. Do not edit. */ export { Subscriptions, createModalAction, createPayloadAction, findAndMerge, findAndModify, getById, handleEffectError, imngEffectError, imngEffectErrorReducer, isFalsy, isTruthy, matchAndSpread, removeById, sleep, useCacheIfExists }; //# sourceMappingURL=imng-ngrx-utils.mjs.map