@brandingbrand/cargo-hold
Advanced tools
Comparing version 11.67.0 to 11.68.0
169
CHANGELOG.md
@@ -5,266 +5,167 @@ # Changelog | ||
# [11.67.0](https://github.com/brandingbrand/shipyard/compare/v11.66.0...v11.67.0) (2022-08-02) | ||
# [11.68.0](https://github.com/brandingbrand/shipyard/compare/v11.67.0...v11.68.0) (2022-08-16) | ||
### Features | ||
* **cargo-hold:** devtools support ([7a4def7](https://github.com/brandingbrand/shipyard/commit/7a4def7e2803f766ce3f9354a91f014173ac8436)) | ||
# [11.67.0](https://github.com/brandingbrand/shipyard/compare/v11.66.0...v11.67.0) (2022-08-02) | ||
### Features | ||
- **cargo-hold:** devtools support ([7a4def7](https://github.com/brandingbrand/shipyard/commit/7a4def7e2803f766ce3f9354a91f014173ac8436)) | ||
# [11.66.0](https://github.com/brandingbrand/shipyard/compare/v11.65.0...v11.66.0) (2022-07-15) | ||
# [11.65.0](https://github.com/brandingbrand/shipyard/compare/v11.64.0...v11.65.0) (2022-06-29) | ||
### Features | ||
* **cargo-hold:** update return type of predict fn ([3569d42](https://github.com/brandingbrand/shipyard/commit/3569d424b9273f8921aa62152aee30e595146b31)) | ||
- **cargo-hold:** update return type of predict fn ([3569d42](https://github.com/brandingbrand/shipyard/commit/3569d424b9273f8921aa62152aee30e595146b31)) | ||
# [11.64.0](https://github.com/brandingbrand/shipyard/compare/v11.63.0...v11.64.0) (2022-06-02) | ||
### Bug Fixes | ||
* **cargo-hold:** removing complete and error from eventbus queue ([159c3ae](https://github.com/brandingbrand/shipyard/commit/159c3ae60baec69dcde06d033f2d903843f8b1a0)) | ||
- **cargo-hold:** removing complete and error from eventbus queue ([159c3ae](https://github.com/brandingbrand/shipyard/commit/159c3ae60baec69dcde06d033f2d903843f8b1a0)) | ||
### Features | ||
* **workspace:** correct eslint parserOptions settings ([feadfc3](https://github.com/brandingbrand/shipyard/commit/feadfc30bd04bf2156b6de639a9591b755254e0a)) | ||
- **workspace:** correct eslint parserOptions settings ([feadfc3](https://github.com/brandingbrand/shipyard/commit/feadfc30bd04bf2156b6de639a9591b755254e0a)) | ||
# [11.63.0](https://github.com/brandingbrand/shipyard/compare/v11.62.0...v11.63.0) (2022-05-24) | ||
### Bug Fixes | ||
* **cargo-hold:** update async.builder functions with oldPayload to have third generic ([cb56516](https://github.com/brandingbrand/shipyard/commit/cb56516feb619a22f82852a65edfe3ad2fab39c9)) | ||
- **cargo-hold:** update async.builder functions with oldPayload to have third generic ([cb56516](https://github.com/brandingbrand/shipyard/commit/cb56516feb619a22f82852a65edfe3ad2fab39c9)) | ||
### Features | ||
* **cargo-hold:** add Dispatch type definition ([9b42c5e](https://github.com/brandingbrand/shipyard/commit/9b42c5e1ad0003666f1d13650af9fdcba1a76ae9)) | ||
* **cargo-hold:** add getDefaultedEntity ([6de248f](https://github.com/brandingbrand/shipyard/commit/6de248fdc919664fa74ef7cd69533822b5250974)) | ||
* **cargo-hold:** decenteralized handler registration logic ([85c267f](https://github.com/brandingbrand/shipyard/commit/85c267feff5cbe3dbc03dfea0444804c0fa3ad8e)) | ||
- **cargo-hold:** add Dispatch type definition ([9b42c5e](https://github.com/brandingbrand/shipyard/commit/9b42c5e1ad0003666f1d13650af9fdcba1a76ae9)) | ||
- **cargo-hold:** add getDefaultedEntity ([6de248f](https://github.com/brandingbrand/shipyard/commit/6de248fdc919664fa74ef7cd69533822b5250974)) | ||
- **cargo-hold:** decenteralized handler registration logic ([85c267f](https://github.com/brandingbrand/shipyard/commit/85c267feff5cbe3dbc03dfea0444804c0fa3ad8e)) | ||
# [11.62.0](https://github.com/brandingbrand/shipyard/compare/v11.61.0...v11.62.0) (2022-05-18) | ||
# [11.61.0](https://github.com/brandingbrand/shipyard/compare/v11.60.0...v11.61.0) (2022-05-16) | ||
# [11.60.0](https://github.com/brandingbrand/shipyard/compare/v11.59.0...v11.60.0) (2022-05-12) | ||
# [11.59.0](https://github.com/brandingbrand/shipyard/compare/v11.58.0...v11.59.0) (2022-05-11) | ||
# [11.58.0](https://github.com/brandingbrand/shipyard/compare/v11.57.0...v11.58.0) (2022-05-06) | ||
# [11.57.0](https://github.com/brandingbrand/shipyard/compare/v11.56.0...v11.57.0) (2022-05-05) | ||
### Features | ||
* **cargo-hold:** add notOfType operator ([633da53](https://github.com/brandingbrand/shipyard/commit/633da53691a37bae7620ab0b661a2ffd404c6356)) | ||
* **cargo-hold:** return subscription from handler registration ([84d7477](https://github.com/brandingbrand/shipyard/commit/84d74777ca435a95b47308ad49d6a8275118e05d)) | ||
- **cargo-hold:** add notOfType operator ([633da53](https://github.com/brandingbrand/shipyard/commit/633da53691a37bae7620ab0b661a2ffd404c6356)) | ||
- **cargo-hold:** return subscription from handler registration ([84d7477](https://github.com/brandingbrand/shipyard/commit/84d74777ca435a95b47308ad49d6a8275118e05d)) | ||
# [11.56.0](https://github.com/brandingbrand/shipyard/compare/v11.55.0...v11.56.0) (2022-05-02) | ||
# [11.55.0](https://github.com/brandingbrand/shipyard/compare/v11.54.0...v11.55.0) (2022-04-28) | ||
# [11.54.0](https://github.com/brandingbrand/shipyard/compare/v11.53.0...v11.54.0) (2022-04-26) | ||
# [11.53.0](https://github.com/brandingbrand/shipyard/compare/v11.52.0...v11.53.0) (2022-04-19) | ||
# [11.52.0](https://github.com/brandingbrand/shipyard/compare/v11.51.0...v11.52.0) (2022-04-18) | ||
# [11.51.0](https://github.com/brandingbrand/shipyard/compare/v11.50.0...v11.51.0) (2022-04-13) | ||
# [11.50.0](https://github.com/brandingbrand/shipyard/compare/v11.49.0...v11.50.0) (2022-04-11) | ||
# [11.49.0](https://github.com/brandingbrand/shipyard/compare/v11.48.0...v11.49.0) (2022-04-08) | ||
### Bug Fixes | ||
* **cargo-hold:** clean up builder types ([3999e93](https://github.com/brandingbrand/shipyard/commit/3999e930b984f496d4bf21e50fd406c32e847536)) | ||
- **cargo-hold:** clean up builder types ([3999e93](https://github.com/brandingbrand/shipyard/commit/3999e930b984f496d4bf21e50fd406c32e847536)) | ||
### Features | ||
* **cargo-hold:** async builder & deprecate asyncAdaptor ([d2f8abf](https://github.com/brandingbrand/shipyard/commit/d2f8abfa380da3c7d6a789fde97973cddef6e557)) | ||
- **cargo-hold:** async builder & deprecate asyncAdaptor ([d2f8abf](https://github.com/brandingbrand/shipyard/commit/d2f8abfa380da3c7d6a789fde97973cddef6e557)) | ||
# [11.48.0](https://github.com/brandingbrand/shipyard/compare/v11.47.0...v11.48.0) (2022-04-05) | ||
# [11.47.0](https://github.com/brandingbrand/shipyard/compare/v11.46.0...v11.47.0) (2022-03-31) | ||
# [11.46.0](https://github.com/brandingbrand/shipyard/compare/v11.45.0...v11.46.0) (2022-03-28) | ||
### Features | ||
* **cargo-hold:** add filterMetadata ([a172b01](https://github.com/brandingbrand/shipyard/commit/a172b01b18b31ea56d7fc296f9ed5f80c0517451)) | ||
- **cargo-hold:** add filterMetadata ([a172b01](https://github.com/brandingbrand/shipyard/commit/a172b01b18b31ea56d7fc296f9ed5f80c0517451)) | ||
# [11.45.0](https://github.com/brandingbrand/shipyard/compare/v11.44.0...v11.45.0) (2022-03-24) | ||
# [11.44.0](https://github.com/brandingbrand/shipyard/compare/v11.43.0...v11.44.0) (2022-03-23) | ||
### Bug Fixes | ||
* **cargo-hold:** make effect emissions slightly async ([b740c0a](https://github.com/brandingbrand/shipyard/commit/b740c0a866ad129d50e1dcab36bec74d1920f783)) | ||
* **cargo-hold:** make effects always follow reducers ([515d7bb](https://github.com/brandingbrand/shipyard/commit/515d7bbec3272ca675ccd6a5bd951f842440edfe)) | ||
- **cargo-hold:** make effect emissions slightly async ([b740c0a](https://github.com/brandingbrand/shipyard/commit/b740c0a866ad129d50e1dcab36bec74d1920f783)) | ||
- **cargo-hold:** make effects always follow reducers ([515d7bb](https://github.com/brandingbrand/shipyard/commit/515d7bbec3272ca675ccd6a5bd951f842440edfe)) | ||
# [11.43.0](https://github.com/brandingbrand/shipyard/compare/v11.42.0...v11.43.0) (2022-03-22) | ||
# [11.42.0](https://github.com/brandingbrand/shipyard/compare/v11.41.0...v11.42.0) (2022-03-21) | ||
# [11.41.0](https://github.com/brandingbrand/shipyard/compare/v11.40.0...v11.41.0) (2022-03-17) | ||
# [11.40.0](https://github.com/brandingbrand/shipyard/compare/v11.39.0...v11.40.0) (2022-03-10) | ||
# [11.39.0](https://github.com/brandingbrand/shipyard/compare/v11.38.0...v11.39.0) (2022-03-08) | ||
### Features | ||
* **cargo-hold:** add ActionSpecifierOf type ([876d6c7](https://github.com/brandingbrand/shipyard/commit/876d6c758b2d5a9d6b49182b7b7abe7a88e8c5c3)) | ||
- **cargo-hold:** add ActionSpecifierOf type ([876d6c7](https://github.com/brandingbrand/shipyard/commit/876d6c758b2d5a9d6b49182b7b7abe7a88e8c5c3)) | ||
# [11.38.0](https://github.com/brandingbrand/shipyard/compare/v11.37.0...v11.38.0) (2022-03-07) | ||
# [11.37.0](https://github.com/brandingbrand/shipyard/compare/v11.36.0...v11.37.0) (2022-03-03) | ||
# [11.36.0](https://github.com/brandingbrand/shipyard/compare/v11.35.0...v11.36.0) (2022-03-02) | ||
# [11.35.0](https://github.com/brandingbrand/shipyard/compare/v11.34.0...v11.35.0) (2022-02-25) | ||
# [11.34.0](https://github.com/brandingbrand/shipyard/compare/v11.33.0...v11.34.0) (2022-02-25) | ||
# [11.33.0](https://github.com/brandingbrand/shipyard/compare/v11.32.0...v11.33.0) (2022-02-23) | ||
### Features | ||
* **cargo-hold:** open default state for IStore ([aeb0bc4](https://github.com/brandingbrand/shipyard/commit/aeb0bc47078065d275491c5fd5d4e2fd176381ee)) | ||
* **cargo-hold:** type updates & effect helpers ([badfdee](https://github.com/brandingbrand/shipyard/commit/badfdee4e409fdf8cc7879400d957f35fb319f52)) | ||
- **cargo-hold:** open default state for IStore ([aeb0bc4](https://github.com/brandingbrand/shipyard/commit/aeb0bc47078065d275491c5fd5d4e2fd176381ee)) | ||
- **cargo-hold:** type updates & effect helpers ([badfdee](https://github.com/brandingbrand/shipyard/commit/badfdee4e409fdf8cc7879400d957f35fb319f52)) | ||
# [11.32.0](https://github.com/brandingbrand/shipyard/compare/v11.31.0...v11.32.0) (2022-02-22) | ||
# [11.31.0](https://github.com/brandingbrand/shipyard/compare/v11.30.0...v11.31.0) (2022-02-18) | ||
# [11.30.0](https://github.com/brandingbrand/shipyard/compare/v11.29.0...v11.30.0) (2022-02-17) | ||
# [11.29.0](https://github.com/brandingbrand/shipyard/compare/v11.28.0...v11.29.0) (2022-02-16) | ||
# [11.28.0](https://github.com/brandingbrand/shipyard/compare/v11.27.0...v11.28.0) (2022-02-15) | ||
# [11.27.0](https://github.com/brandingbrand/shipyard/compare/v11.26.1...v11.27.0) (2022-02-14) | ||
### Bug Fixes | ||
* **cargo-hold:** match source only if provided ([d4233a0](https://github.com/brandingbrand/shipyard/commit/d4233a0e77944c5e71b6d05d993b12c5a943a008)) | ||
- **cargo-hold:** match source only if provided ([d4233a0](https://github.com/brandingbrand/shipyard/commit/d4233a0e77944c5e71b6d05d993b12c5a943a008)) | ||
### Features | ||
* **cargo-hold:** added AsyncAdaptor EmptyPayload ([8c9a44a](https://github.com/brandingbrand/shipyard/commit/8c9a44ae305ec5f0635ea148000b7443bdc7b36e)) | ||
* **cargo-hold:** allowed for async idle state to be undefined ([e7aae84](https://github.com/brandingbrand/shipyard/commit/e7aae84f35b758027b259df7ba490605a701b4b3)) | ||
- **cargo-hold:** added AsyncAdaptor EmptyPayload ([8c9a44a](https://github.com/brandingbrand/shipyard/commit/8c9a44ae305ec5f0635ea148000b7443bdc7b36e)) | ||
- **cargo-hold:** allowed for async idle state to be undefined ([e7aae84](https://github.com/brandingbrand/shipyard/commit/e7aae84f35b758027b259df7ba490605a701b4b3)) | ||
## [11.26.1](https://github.com/brandingbrand/shipyard/compare/v11.26.0...v11.26.1) (2022-02-11) | ||
# [11.26.0](https://github.com/brandingbrand/shipyard/compare/v11.25.1...v11.26.0) (2022-02-10) | ||
### Bug Fixes | ||
* **cargo-hold:** use typescript 4.2 compatible interface ([c0b6b5c](https://github.com/brandingbrand/shipyard/commit/c0b6b5ca3abb0c3607b866907b4486469415601e)) | ||
- **cargo-hold:** use typescript 4.2 compatible interface ([c0b6b5c](https://github.com/brandingbrand/shipyard/commit/c0b6b5ca3abb0c3607b866907b4486469415601e)) | ||
### Features | ||
* **cargo-hold:** initialize project ([4b72e26](https://github.com/brandingbrand/shipyard/commit/4b72e265e83003be09e1f57152915df61b3c47be)) | ||
* **cargo-hold:** removed cargo-hold lens in favor of util lens ([e47ea5f](https://github.com/brandingbrand/shipyard/commit/e47ea5f777f2a37749cc1c879e05c990ae004def)) | ||
- **cargo-hold:** initialize project ([4b72e26](https://github.com/brandingbrand/shipyard/commit/4b72e265e83003be09e1f57152915df61b3c47be)) | ||
- **cargo-hold:** removed cargo-hold lens in favor of util lens ([e47ea5f](https://github.com/brandingbrand/shipyard/commit/e47ea5f777f2a37749cc1c879e05c990ae004def)) | ||
# [11.25.0](https://github.com/brandingbrand/shipyard/compare/v11.24.0...v11.25.0) (2022-02-08) | ||
@@ -280,3 +181,3 @@ | ||
* **fsstate:** heavy refactor with adaptors to enable entities ([03dbd8f](https://github.com/brandingbrand/shipyard/commit/03dbd8f89fe718de8a9dfd1b09467d422f184bc5)) | ||
- **fsstate:** heavy refactor with adaptors to enable entities ([03dbd8f](https://github.com/brandingbrand/shipyard/commit/03dbd8f89fe718de8a9dfd1b09467d422f184bc5)) | ||
@@ -299,8 +200,8 @@ # [11.21.0](https://github.com/brandingbrand/shipyard/compare/v11.20.2...v11.21.0) (2022-01-21) | ||
* **fsstate:** async effect & reducer utils ([9b76085](https://github.com/brandingbrand/shipyard/commit/9b76085eef57a750b6d0f00abadb379401849d73)) | ||
* **fsstate:** create basic store ([bb31f77](https://github.com/brandingbrand/shipyard/commit/bb31f77835f4620ba6d6c743305f1d8edfa908d3)) | ||
* **fsstate:** create functional lenses ([5f52fbf](https://github.com/brandingbrand/shipyard/commit/5f52fbf255834237c9ee2eb43ee58cb113f3050d)) | ||
* **fsstate:** fsstate util types ([0d23ca0](https://github.com/brandingbrand/shipyard/commit/0d23ca0eb8a22e9328a8691453532ea58bc1139f)) | ||
* **fsstate:** initialize project ([8d276dc](https://github.com/brandingbrand/shipyard/commit/8d276dceb634a62ff94a38436c52b731aff0ab95)) | ||
* **fsstate:** make reducer filters more composable ([82d7059](https://github.com/brandingbrand/shipyard/commit/82d70594ef19c20bb0ebdac38fa0e011e7b0115e)) | ||
* **fsstate:** reactive-state ([2826a10](https://github.com/brandingbrand/shipyard/commit/2826a10180e82350324b1c9a066d847e55922722)) | ||
- **fsstate:** async effect & reducer utils ([9b76085](https://github.com/brandingbrand/shipyard/commit/9b76085eef57a750b6d0f00abadb379401849d73)) | ||
- **fsstate:** create basic store ([bb31f77](https://github.com/brandingbrand/shipyard/commit/bb31f77835f4620ba6d6c743305f1d8edfa908d3)) | ||
- **fsstate:** create functional lenses ([5f52fbf](https://github.com/brandingbrand/shipyard/commit/5f52fbf255834237c9ee2eb43ee58cb113f3050d)) | ||
- **fsstate:** fsstate util types ([0d23ca0](https://github.com/brandingbrand/shipyard/commit/0d23ca0eb8a22e9328a8691453532ea58bc1139f)) | ||
- **fsstate:** initialize project ([8d276dc](https://github.com/brandingbrand/shipyard/commit/8d276dceb634a62ff94a38436c52b731aff0ab95)) | ||
- **fsstate:** make reducer filters more composable ([82d7059](https://github.com/brandingbrand/shipyard/commit/82d70594ef19c20bb0ebdac38fa0e011e7b0115e)) | ||
- **fsstate:** reactive-state ([2826a10](https://github.com/brandingbrand/shipyard/commit/2826a10180e82350324b1c9a066d847e55922722)) |
1102
index-esm.js
@@ -1,6 +0,3 @@ | ||
import { filter, scan, shareReplay, map as map$1, switchMap as switchMap$1, withLatestFrom as withLatestFrom$1, mergeMap as mergeMap$1 } from 'rxjs/operators'; | ||
import { Subject, Subscription, filter as filter$1, withLatestFrom, map, switchMap, from, mergeMap, of, merge, BehaviorSubject, ReplaySubject } from 'rxjs'; | ||
import { pipe, branchObject, branch } from '@brandingbrand/standard-compose'; | ||
import { ok, fail, isOk } from '@brandingbrand/standard-result'; | ||
import { createLensCreator, withLens, createLens, ComposableLens } from '@brandingbrand/standard-lens'; | ||
import { filter, scan, shareReplay, map, switchMap, withLatestFrom } from 'rxjs/operators'; | ||
import { Subject, Subscription, merge, BehaviorSubject, ReplaySubject } from 'rxjs'; | ||
import equal from 'fast-deep-equal'; | ||
@@ -81,252 +78,3 @@ | ||
function _defineProperty$6(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$6(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$6(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
function asyncBuilder() { | ||
return {}; | ||
} | ||
const asyncBuilderWithStructureLens = (lens)=>({ | ||
lens | ||
}) | ||
; | ||
const withStructureLens = (lens)=>(asyncBuilder1)=>_objectSpread$6({}, asyncBuilder1, { | ||
lens | ||
}) | ||
; | ||
const withActionKey = (actionKey)=>(builder)=>_objectSpread$6({}, builder, { | ||
actionKey | ||
}) | ||
; | ||
const withMetadata = (metadata)=>(builder)=>_objectSpread$6({}, builder, { | ||
metadata | ||
}) | ||
; | ||
const withTriggerActionFilter = (triggerActionFilter)=>(builder)=>_objectSpread$6({}, builder, { | ||
triggerActionFilter | ||
}) | ||
; | ||
const withAsyncCallback = (callback)=>(builder)=>_objectSpread$6({}, builder, { | ||
callback | ||
}) | ||
; | ||
const withMapOnSuccess = (mapOnSuccess)=>(builder)=>_objectSpread$6({}, builder, { | ||
mapOnSuccess | ||
}) | ||
; | ||
const withMapOnFailure = (mapOnFailure)=>(builder)=>_objectSpread$6({}, builder, { | ||
mapOnFailure | ||
}) | ||
; | ||
const withOptimisticUpdate = (prediction)=>(builder)=>_objectSpread$6({}, builder, { | ||
prediction | ||
}) | ||
; | ||
const withEnableLoadingMore = (enableLoadMore)=>(builder)=>_objectSpread$6({}, builder, { | ||
enableLoadMore | ||
}) | ||
; | ||
function _defineProperty$5(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$5(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$5(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
/** | ||
* Takes a builder and returns an init action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildInitActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:init', | ||
callback: (idlePayload)=>idlePayload | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a loading action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildLoadingActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
actionKey: builder.actionKey, | ||
subtype: 'async:load', | ||
callback: (loadingPayload)=>loadingPayload | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a loading more action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildLoadingMoreActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:load-more', | ||
callback: (loadingPayload)=>loadingPayload | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a success action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildSucceedActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:succeed', | ||
callback: (succeedPayload)=>succeedPayload | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a failure action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildFailActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:fail', | ||
callback: (failure)=>failure | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a revert action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildRevertActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:revert', | ||
callback: (revertPayload)=>revertPayload | ||
})) | ||
; | ||
/** | ||
* Builds a full suite of async action creators. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildActionCreators = (builder)=>pipe(builder, branchObject({ | ||
init: buildInitActionCreator, | ||
load: buildLoadingActionCreator, | ||
loadMore: buildLoadingMoreActionCreator, | ||
succeed: buildSucceedActionCreator, | ||
fail: buildFailActionCreator, | ||
revert: buildRevertActionCreator | ||
})) | ||
; | ||
/** | ||
* Given a correctly-typed builder object, construct an Effect that will be triggered by the | ||
* triggering action, instigate loading states, run optimistic predictions, and end with success or | ||
* failure. Also reverts optimistic predictions if necessary on failure. | ||
* | ||
* @param builder | ||
* @return The async effect | ||
*/ const buildAsyncEffect = (builder)=>(action$, state$)=>{ | ||
// load$ gets triggered by the triggerActionFilter, runs the prediction if given, and emits | ||
// loading or loadingMore actions | ||
const load$ = action$.pipe(// these typeguards don't have perfect Typescript defs | ||
filter$1(builder.triggerActionFilter), withLatestFrom(state$), map(([action, state])=>{ | ||
const predictedPayload = builder.prediction ? builder.prediction(action.payload)(state.payload) : state.payload; | ||
if (builder.enableLoadMore === true && (state.status === 'success' || state.status === 'loading-more')) { | ||
return buildLoadingMoreActionCreator(builder).create(predictedPayload); | ||
} | ||
return buildLoadingActionCreator(builder).create(predictedPayload); | ||
})); | ||
// callbackAction$ is also triggered by the triggerActionFilter, whose payload serves as the | ||
// argument for the callback. It runs the callback, judges success vs failure, and emits the | ||
// appropriate action accordingly. If it was a failure, also emits a revert if we had a prediction. | ||
const callbackAction$ = action$.pipe(filter$1(builder.triggerActionFilter), // grab state to get back to if we have optimistic updates on and we fail | ||
withLatestFrom(state$), switchMap(([action, stateAtStart])=>from(// take advantage of the flattening of Promises - wrap it in a promise regardless | ||
new Promise((resolve, reject)=>{ | ||
try { | ||
resolve(builder.callback(action.payload)); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}).then(ok).catch(fail)).pipe(// grab latest state for mapOnSuccess etc functions to use | ||
withLatestFrom(state$), mergeMap(([wrappedResult, stateAtReturn])=>{ | ||
if (isOk(wrappedResult)) { | ||
const result = wrappedResult.ok; | ||
// Type coercion is due to Typescript not following unions quite right | ||
const mappedNewState = builder.mapOnSuccess ? builder.mapOnSuccess(result)(stateAtReturn.payload) : result; | ||
return of(buildSucceedActionCreator(builder).create(mappedNewState)); | ||
} | ||
const result = wrappedResult.failure; | ||
const mappedFailure = builder.mapOnFailure ? builder.mapOnFailure(result)(stateAtReturn.failure) : result; | ||
return builder.prediction ? of(buildRevertActionCreator(builder).create(stateAtStart.payload), buildFailActionCreator(builder).create(mappedFailure)) : of(buildFailActionCreator(builder).create(mappedFailure)); | ||
})) | ||
)); | ||
return merge(load$, callbackAction$); | ||
} | ||
; | ||
/** | ||
* Given a correctly-typed builder object, construct an Effect that will be triggered by the | ||
* triggering action, instigate loading states, run optimistic predictions, and end with success or | ||
* failure. Also reverts optimistic predictions if necessary on failure. This runs inside a lens, so | ||
* as to operate on a larger data structure. | ||
* | ||
* @param builder | ||
* @return The async effect | ||
*/ const buildAsyncEffectWithLens = (builder)=>(action$, state$)=>// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any is of limited scope and not worth specifying | ||
buildAsyncEffect(builder)(action$, state$.pipe(map(builder.lens.get))) | ||
; | ||
/** | ||
* If we input a lens it outputs a lens that goes from the outer structure of the input | ||
* lens to the async state's payload. If we do not input a lens it outputs a lens that goes | ||
* from the async state to its payload. | ||
* | ||
* @param builder | ||
* @return | ||
*/ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- we're using overloads | ||
function buildPayloadLens(builder) { | ||
const innerPayloadLens = createLensCreator().fromPath('payload'); | ||
if (builder.lens) { | ||
return innerPayloadLens.withOuterLens(builder.lens); | ||
} | ||
return innerPayloadLens; | ||
} | ||
/** | ||
* Combines multiple effects into one, merging all of their observables. | ||
@@ -459,3 +207,3 @@ * | ||
// Concatenates the reducers into an observable array. | ||
return this.internalReducer$.asObservable().pipe(accumulateToArray(), map$1((reducers)=>combineActionReducers(...reducers) | ||
return this.internalReducer$.asObservable().pipe(accumulateToArray(), map((reducers)=>combineActionReducers(...reducers) | ||
)); | ||
@@ -490,4 +238,4 @@ } | ||
}; | ||
const reducerSubscription = this.reducer$.pipe(switchMap$1((reducer)=>this.action$.pipe(scan((state, action)=>reducer(action)(state) | ||
, initialState), withLatestFrom$1(this.action$)) | ||
const reducerSubscription = this.reducer$.pipe(switchMap((reducer)=>this.action$.pipe(scan((state, action)=>reducer(action)(state) | ||
, initialState), withLatestFrom(this.action$)) | ||
)).subscribe({ | ||
@@ -509,840 +257,2 @@ next: ([state, action])=>{ | ||
const createIdleState = (payload)=>({ | ||
status: 'idle', | ||
payload | ||
}) | ||
; | ||
const createLoadingState = (payload)=>({ | ||
status: 'loading', | ||
payload | ||
}) | ||
; | ||
const createLoadingMoreState = (payload)=>({ | ||
status: 'loading-more', | ||
payload | ||
}) | ||
; | ||
const createSuccessState = (payload)=>({ | ||
status: 'success', | ||
payload | ||
}) | ||
; | ||
const createFailureState = (payload, failure)=>({ | ||
status: 'failure', | ||
payload, | ||
failure | ||
}) | ||
; | ||
function _defineProperty$4(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$4(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$4(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
/** | ||
* Given a payload it will return a state reducer that gives us an idle state of that payload. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildInitStateReducer = (_builder)=>(payload)=>()=>createIdleState(payload) | ||
; | ||
/** | ||
* Given a payload it will return a state reducer that gives us a loading state of that payload. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildLoadingStateReducer = (_builder)=>(payload)=>()=>createLoadingState(payload) | ||
; | ||
/** | ||
* Given a payload it will return a state reducer that gives us a loading more state | ||
* of that payload. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildLoadingMoreStateReducer = (_builder)=>(payload)=>()=>createLoadingMoreState(payload) | ||
; | ||
/** | ||
* Given a payload it will return a state reducer that gives us a success state of that payload. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildSuccessStateReducer = (_builder)=>(payload)=>()=>createSuccessState(payload) | ||
; | ||
/** | ||
* Given a failure it will return a state reducer that gives us a failure state. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildFailureStateReducer = (_builder)=>(failure)=>(state)=>createFailureState(state.payload, failure) | ||
; | ||
/** | ||
* Given a payload it will return a state reducer that reverts to the given payload but leaving | ||
* the status the same. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildRevertStateReducer = (_builder)=>(payload)=>(state)=>// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- fixes a nuanced type issue. | ||
_objectSpread$4({}, state, { | ||
payload | ||
}) | ||
; | ||
const makeActionReducer = (actionCreator, stateReducer)=>on(matches(actionCreator), (action)=>stateReducer(action.payload) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in an idle state. | ||
* | ||
* @param builder A builder with an action key and an idle type. | ||
* @return | ||
*/ const buildInitActionReducer = (builder)=>pipe(builder, // buildInitActionCreator will take WithMetadata and do the right thing. | ||
branch(buildInitActionCreator, buildInitStateReducer), ([actionCreator, stateReducerCreator])=>makeActionReducer(actionCreator, stateReducerCreator) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a loading state. | ||
* | ||
* @param builder A builder with an action key and a loading type. | ||
* @return | ||
*/ const buildLoadingActionReducer = (builder)=>pipe(builder, branch(buildLoadingActionCreator, buildLoadingStateReducer), ([actionCreator, stateReducerCreator])=>on(matches(actionCreator), (action)=>stateReducerCreator(action.payload) | ||
) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a loading more state. | ||
* | ||
* @param builder A builder with an action key and a loading more type. | ||
* @return | ||
*/ const buildLoadingMoreActionReducer = (builder)=>pipe(builder, branch(buildLoadingMoreActionCreator, buildLoadingMoreStateReducer), ([actionCreator, stateReducerCreator])=>makeActionReducer(actionCreator, stateReducerCreator) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a success state. | ||
* | ||
* @param builder A builder with an action key and a success type. | ||
* @return | ||
*/ const buildSuccessActionReducer = (builder)=>pipe(builder, branch(buildSucceedActionCreator, buildSuccessStateReducer), ([actionCreator, stateReducerCreator])=>makeActionReducer(actionCreator, stateReducerCreator) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a failure state. | ||
* | ||
* @param builder A builder with an action key and payload types. | ||
* @return | ||
*/ const buildFailureActionReducer = (builder)=>pipe(builder, branch(buildFailActionCreator, buildFailureStateReducer), ([actionCreator, stateReducerCreator])=>on(matches(actionCreator), (action)=>(state)=>stateReducerCreator(action.payload)(state) | ||
) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a reverted payload. | ||
* | ||
* @param builder A builder with an action key and payload types. | ||
* @return | ||
*/ const buildRevertActionReducer = (builder)=>pipe(builder, branch(buildRevertActionCreator, buildRevertStateReducer), ([actionCreator, stateReducerCreator])=>on(matches(actionCreator), (action)=>stateReducerCreator(action.payload) | ||
) | ||
) | ||
; | ||
/** | ||
* Given a builder it makes an action reducer that looks for all of the async actions | ||
* and performs the appropriate state change. | ||
* | ||
* @param builder A builder with an action key an payload types. | ||
* @return | ||
*/ const buildCombinedReducers = (builder)=>pipe(builder, branch(buildInitActionReducer, buildLoadingActionReducer, buildLoadingMoreActionReducer, buildSuccessActionReducer, buildFailureActionReducer, buildRevertActionReducer), (actionReducers)=>combineActionReducers(...actionReducers) | ||
) | ||
; | ||
/** | ||
* Returns an action reducer from buildCombinedReducers, but run in a lens so that it handles | ||
* a larger data structure. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildCombinedLensedReducers = (builder)=>(action)=>withLens(builder.lens)(buildCombinedReducers(builder)(action)) | ||
; | ||
/** | ||
* Takes a builder and returns a selector that selects the payload. | ||
* | ||
* @param builder | ||
* @return | ||
*/ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- cant use arrow functions for overloading. | ||
function buildSelectPayload(builder) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-explicit-any -- the any is limited in scope and not worth it to describe. | ||
return buildPayloadLens(builder).get; | ||
} | ||
/** | ||
* Takes a builder and returns a selector that selects the status. | ||
* | ||
* @param builder | ||
* @return | ||
*/ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- cant use arrow functions for overloading. | ||
function buildSelectStatus(builder) { | ||
const statusLens = createLensCreator().fromPath('status'); | ||
if (builder.lens) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-explicit-any -- the any is limited in scope and not worth it to describe. | ||
return statusLens.withOuterLens(builder.lens).get; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-explicit-any -- the any is limited in scope and not worth it to describe. | ||
return statusLens.get; | ||
} | ||
/** | ||
* Takes a builder and returns a selector that selects the failure data if available. | ||
* | ||
* @param builder | ||
* @return | ||
*/ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- cant use arrow functions for overloading. | ||
function buildSelectFailure(builder) { | ||
const failureLens = createLensCreator().fromPath('failure'); | ||
if (builder.lens) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return -- the any is limited in scope and not worth it to describe. | ||
return failureLens.withOuterLens(// this coercion is fine; the lens just gets 'failure' from it so if it's not a failure, | ||
// it'll return undefined, which is what we want. | ||
builder.lens).get; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-explicit-any -- the any is limited in scope and not worth it to describe. | ||
return failureLens.get; | ||
} | ||
/** | ||
* | ||
* @param actionKey The key by which you want to call async actions to set state. | ||
* @param source Optional source to emit with the actions to "lock in" these actions to target | ||
* specific reducers. | ||
* @param metadata | ||
* @return `AsyncActionCreators` - init, load, succeed, fail, & revert. | ||
* @deprecated Use builder-based async functions. | ||
*/ const createAsyncActionCreators = (actionKey, source, metadata)=>({ | ||
init: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:init', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}), | ||
load: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:load', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}), | ||
loadMore: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:load-more', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}), | ||
succeed: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:succeed', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}), | ||
fail: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:fail', | ||
source, | ||
metadata, | ||
callback: (failure)=>failure | ||
}), | ||
revert: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:revert', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}) | ||
}) | ||
; | ||
/** | ||
* `makeAsyncEffect` is the "raw" creator function that gets used in the `AsyncAdaptor`. It gets | ||
* partially applied in the adaptor creator function itself, leaving the user to fill in the details | ||
* of a particular effect using the second curried parameter. | ||
* | ||
* @param asyncActionCreators The Async Action creators that should be called to dispatch the correct | ||
* actions to update status and payload. | ||
* @return A function that takes in CreateAsyncEffectOptions and returns an Effect | ||
*/ const makeAsyncEffect = (asyncActionCreators)=>(effectOptions)=>(action$, state$)=>{ | ||
var _predict; | ||
const optimisticUpdate = (_predict = effectOptions.predict) !== null && _predict !== void 0 ? _predict : (_params, state)=>state | ||
; | ||
// changes status to loading, with optimistic updates if configured | ||
const emitLoadWithPredictor$ = action$.pipe(// type coercion is due to us filtering Actions, just by their ActionSpecifier properties | ||
filter(effectOptions.when), withLatestFrom$1(state$), map$1(([action, state])=>{ | ||
const update = optimisticUpdate(action.payload, state.payload); | ||
if ((state.status === 'success' || state.status === 'loading-more') && effectOptions.loadingMore) { | ||
return asyncActionCreators.loadMore.create(update); | ||
} | ||
return asyncActionCreators.load.create(update); | ||
})); | ||
// runs callback, emits success or failure or revert & failure. | ||
const runCallback$ = action$.pipe(filter(effectOptions.when), // grab state as of init time | ||
withLatestFrom$1(state$), switchMap$1(([action, stateAtStart])=>from(// success is wrapped in a Right structure, failure in a Left. We don't want to put an | ||
// error in the observable, so we catch it and wrap it for identification in the next step. | ||
effectOptions.do(...action.payload).then(ok).catch(fail)).pipe(// grab state after callback returned | ||
withLatestFrom$1(state$), map$1(([result, currentState])=>{ | ||
if (isOk(result)) { | ||
if (effectOptions.mapOnSuccess) { | ||
return asyncActionCreators.succeed.create(effectOptions.mapOnSuccess(result.ok)(currentState.payload)); | ||
} | ||
// onSuccess would have been required if CallbackResult wasn't a Payload, | ||
// so we know this coercion is safe. | ||
return asyncActionCreators.succeed.create(result.ok); | ||
} | ||
if (effectOptions.mapOnFail) { | ||
return asyncActionCreators.fail.create(effectOptions.mapOnFail(result.failure)(currentState.payload, currentState.status === 'failure' ? currentState.failure : undefined)); | ||
} | ||
// onFail would have been required if FailCallbackResult wasn't a FailPayload, | ||
// so we know this coercion is safe. | ||
return asyncActionCreators.fail.create(result.failure); | ||
}), mergeMap$1((asyncCallbackResult)=>{ | ||
if (matches(asyncActionCreators.fail) && effectOptions.predict !== undefined) { | ||
return of(asyncActionCreators.revert.create(stateAtStart.payload), asyncCallbackResult); | ||
} | ||
return of(asyncCallbackResult); | ||
})) | ||
)); | ||
return merge(emitLoadWithPredictor$, runCallback$); | ||
} | ||
; | ||
function _defineProperty$3(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$3(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$3(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
/** | ||
* @deprecated Use builder-based reducer functions. | ||
*/ const createReducers = ()=>({ | ||
init: (payload)=>()=>createIdleState(payload) | ||
, | ||
load: (payload)=>()=>createLoadingState(payload) | ||
, | ||
loadMore: (payload)=>()=>createLoadingMoreState(payload) | ||
, | ||
succeed: (payload)=>()=>createSuccessState(payload) | ||
, | ||
fail: (failure)=>(state)=>createFailureState(state.payload, failure) | ||
, | ||
revert: (payload)=>(state)=>_objectSpread$3({}, state, { | ||
payload | ||
}) | ||
}) | ||
; | ||
/** | ||
* @param lens | ||
* @deprecated Use builder-based async functions. | ||
*/ const createLensedReducers = (lens)=>{ | ||
const reducers = createReducers(); | ||
return { | ||
init: (payload)=>withLens(lens)(reducers.init(payload)) | ||
, | ||
load: (payload)=>withLens(lens)(reducers.load(payload)) | ||
, | ||
loadMore: (payload)=>withLens(lens)(reducers.loadMore(payload)) | ||
, | ||
succeed: (payload)=>withLens(lens)(reducers.succeed(payload)) | ||
, | ||
fail: (failure)=>withLens(lens)(reducers.fail(failure)) | ||
, | ||
revert: (payload)=>withLens(lens)(reducers.revert(payload)) | ||
}; | ||
}; | ||
/** | ||
* @param actionCreators | ||
* @param lens | ||
* @deprecated Use builder-based async functions. | ||
*/ const createCombinedReducer = (actionCreators, lens)=>{ | ||
const reducers = createLensedReducers(lens); | ||
return combineActionReducers(on(matches(actionCreators.init), (action)=>reducers.init(action.payload) | ||
), on(matches(actionCreators.load), (action)=>reducers.load(action.payload) | ||
), on(matches(actionCreators.loadMore), (action)=>reducers.loadMore(action.payload) | ||
), on(matches(actionCreators.succeed), (action)=>reducers.succeed(action.payload) | ||
), on(matches(actionCreators.fail), (action)=>reducers.fail(action.payload) | ||
), on(matches(actionCreators.revert), (action)=>reducers.revert(action.payload) | ||
)); | ||
}; | ||
const makeSelectPayload = (lens)=>(structure)=>lens.get(structure).payload | ||
; | ||
const makeSelectStatus = (lens)=>(structure)=>lens.get(structure).status | ||
; | ||
const makeSelectFailure = (lens)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
if (state.status === 'failure') { | ||
return state.failure; | ||
} | ||
return undefined; | ||
} | ||
; | ||
const createSelectors = (lens)=>({ | ||
selectPayload: makeSelectPayload(lens), | ||
selectStatus: makeSelectStatus(lens), | ||
selectFailure: makeSelectFailure(lens) | ||
}) | ||
; | ||
function _defineProperty$2(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$2(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$2(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
/** | ||
* @param options | ||
* @deprecated Use `asyncBuilder`-based functions instead. | ||
*/ const createAsyncAdaptor = (options)=>{ | ||
const payloadLens = createLens().fromPath('payload'); | ||
var _lens; | ||
const structureLens = (_lens = options.lens) !== null && _lens !== void 0 ? _lens : createLens().fromPath(); | ||
const actionCreators = createAsyncActionCreators(options.actionKey, options.emitSource, options.metadata); | ||
const reducers = createReducers(); | ||
const lensedReducers = createLensedReducers(structureLens); | ||
const combinedReducer = createCombinedReducer(actionCreators, structureLens); | ||
const selectors = createSelectors(structureLens); | ||
const createState = (initialPayload)=>createIdleState(initialPayload) | ||
; | ||
const createEffect = (effectOptions)=>{ | ||
const effect = makeAsyncEffect(actionCreators)(effectOptions); | ||
return (action$, state$)=>effect(action$, state$.pipe(map$1(structureLens.get))) | ||
; | ||
}; | ||
const withLens = (lens)=>createAsyncAdaptor(_objectSpread$2({}, options, { | ||
lens: new ComposableLens(structureLens).withOuterLens(lens) | ||
})) | ||
; | ||
return { | ||
createState, | ||
actionCreators, | ||
combinedReducer, | ||
reducers, | ||
lensedReducers, | ||
selectors, | ||
createEffect, | ||
payloadLens, | ||
withLens | ||
}; | ||
}; | ||
function _defineProperty$1(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$1(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$1(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
// dedupe ids while preserving order | ||
const mergeIdLists = (...ids)=>ids.reduce((result, id)=>{ | ||
if (!result.includes(id)) { | ||
result.push(id); | ||
} | ||
return result; | ||
}, []) | ||
; | ||
const sortIfNeeded = (deps)=>(items, idList)=>{ | ||
if (!deps.isSorted) { | ||
return idList !== null && idList !== void 0 ? idList : Object.values(items).map(deps.idSelector); | ||
} | ||
const entitiesValues = Object.values(items); | ||
entitiesValues.sort(deps.comparer); | ||
return entitiesValues.map(deps.idSelector); | ||
} | ||
; | ||
const fromEntityArray = (deps)=>(items)=>Object.fromEntries(items.map((val)=>[ | ||
deps.idSelector(val), | ||
val | ||
] | ||
)) | ||
; | ||
const makeCreateState = (deps)=>(initialEntities)=>({ | ||
ids: initialEntities.map(deps.idSelector), | ||
entities: Object.fromEntries(initialEntities.map((val)=>[ | ||
deps.idSelector(val), | ||
val | ||
] | ||
)) | ||
}) | ||
; | ||
/** | ||
* Take an array of items and make a reducer that adds those items to an existing state, | ||
* overriding existing ids that collide. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeSetMany = (deps)=>(items)=>withLens(deps.lens)((state)=>{ | ||
const entities = _objectSpread$1({}, state.entities, fromEntityArray(deps)(items)); | ||
const ids = mergeIdLists(...state.ids, ...items.map(deps.idSelector)); | ||
return { | ||
ids: sortIfNeeded(deps)(entities, ids), | ||
entities | ||
}; | ||
}) | ||
; | ||
/** | ||
* Takes an item and makes a reducer that adds that item to an existing state, overriding | ||
* the existing id if it exists. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeSetOne = (deps)=>(item)=>makeSetMany(deps)([ | ||
item | ||
]) | ||
; | ||
/** | ||
* Take an array of items and make a reducer that adds those items to an existing state, | ||
* preserving existing ids. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeAddMany = (deps)=>(items)=>(structure)=>{ | ||
const state = deps.lens.get(structure); | ||
const newIds = items.map(deps.idSelector).filter((val)=>!state.ids.includes(val) | ||
); | ||
if (newIds.length === 0) { | ||
return structure; | ||
} | ||
const newItems = items.filter((val)=>newIds.includes(deps.idSelector(val)) | ||
); | ||
return makeSetMany(deps)(newItems)(structure); | ||
} | ||
; | ||
/** | ||
* Takes an item and makes a reducer that adds that item to an existing state, preserving | ||
* the existing id if it exists. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeAddOne = (deps)=>(item)=>makeAddMany(deps)([ | ||
item | ||
]) | ||
; | ||
/** | ||
* Takes an array of items and replaces the existing state with the incoming state. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeSetAll = (deps)=>(items)=>(structure)=>makeSetMany(deps)(items)(deps.lens.set(makeCreateState(deps)([]))(structure)) | ||
; | ||
/** | ||
* Removes all entities and state ids. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeRemoveAll = (deps)=>()=>(structure)=>deps.lens.set(makeCreateState(deps)([]))(structure) | ||
; | ||
/** | ||
* Takes an array of entity ids and returns a reducer that removes those ids if they exist. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeRemoveMany = (deps)=>(ids)=>withLens(deps.lens)((state)=>{ | ||
const newIds = state.ids.filter((id)=>!ids.includes(id) | ||
); | ||
const newEntities = Object.fromEntries(newIds.map((id)=>[ | ||
id, | ||
state.entities[id] | ||
] | ||
)); | ||
// TODO @grayontheweb 04-01-2022 we shouldn't have to type coerce this but | ||
// this is a quick fix | ||
return { | ||
ids: newIds, | ||
entities: newEntities | ||
}; | ||
}) | ||
; | ||
/** | ||
* Takes an entity id, returns a reducer that removes it if it exists. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeRemoveOne = (deps)=>(id)=>makeRemoveMany(deps)([ | ||
id | ||
]) | ||
; | ||
/** | ||
* Returns a full suite of reducers applying the given dependencies. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeReducers = (deps)=>({ | ||
addMany: makeAddMany(deps), | ||
addOne: makeAddOne(deps), | ||
setMany: makeSetMany(deps), | ||
setOne: makeSetOne(deps), | ||
setAll: makeSetAll(deps), | ||
removeAll: makeRemoveAll(deps), | ||
removeMany: makeRemoveMany(deps), | ||
removeOne: makeRemoveOne(deps) | ||
}) | ||
; | ||
/** | ||
* Take all of the entities in an entity state from a given lens and returns | ||
* an array of the entities. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectAll = (lens)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
// TODO @grayontheweb 04-01-2022 this pattern is repeated with minor | ||
// variations a few times in this file | ||
return state.ids.reduce((acc, id)=>{ | ||
const entity = state.entities[id]; | ||
return entity ? [ | ||
...acc, | ||
entity | ||
] : acc; | ||
}, []); | ||
} | ||
; | ||
/** | ||
* Select the given Ids from the entity state from a give lens and returns an array of them. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectByIds = (lens)=>(ids)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
return ids.reduce((acc, id)=>{ | ||
const entity = state.entities[id]; | ||
return entity ? [ | ||
...acc, | ||
entity | ||
] : acc; | ||
}, []); | ||
} | ||
; | ||
/** | ||
* Select the given Id from the entity state from a give lens and returns either the entity | ||
* or undefined. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectById = (lens)=>(id)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
return id in state.entities ? state.entities[id] : undefined; | ||
} | ||
; | ||
/** | ||
* Given an index it will select the Nth value if it exists. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectByIndex = (lens)=>(index)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
const id = state.ids[index]; | ||
return id !== undefined ? state.entities[id] : undefined; | ||
} | ||
; | ||
/** | ||
* Given multiple indices it will select the Nth values if they exist. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectByIndices = (lens)=>(indices)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
return indices.reduce((acc, index)=>{ | ||
const id = state.ids[index]; | ||
const entity = id ? state.entities[id] : undefined; | ||
return entity !== undefined && entity in state.entities ? [ | ||
...acc, | ||
entity | ||
] : acc; | ||
}, []); | ||
} | ||
; | ||
/** | ||
* Returns the full suite of selectors given the lens. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const getSelectors = (lens)=>({ | ||
selectAll: makeSelectAll(lens), | ||
selectByIds: makeSelectByIds(lens), | ||
selectById: makeSelectById(lens), | ||
selectByIndex: makeSelectByIndex(lens), | ||
selectByIndices: makeSelectByIndices(lens) | ||
}) | ||
; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
const defaultIdSelector = (item)=>item.id | ||
; | ||
/** | ||
* Creates an entity adapter. | ||
* | ||
* @param options Options to create the entity adapter. | ||
* @return | ||
*/ const createEntityAdaptor = (options)=>{ | ||
const isSorted = Boolean(options.comparer); | ||
var _lens; | ||
const structureLens = (_lens = options.lens) !== null && _lens !== void 0 ? _lens : createLens().fromPath(); | ||
const deps = _objectSpread({}, options, { | ||
lens: structureLens, | ||
isSorted | ||
}); | ||
const unlensedDeps = _objectSpread({}, options, { | ||
isSorted, | ||
lens: createLens().fromPath() | ||
}); | ||
const lensedReducers = makeReducers(deps); | ||
const reducers = makeReducers(unlensedDeps); | ||
const withLens = (lens)=>createEntityAdaptor(_objectSpread({}, options, { | ||
lens: new ComposableLens(structureLens).withOuterLens(lens) | ||
})) | ||
; | ||
const selectors = getSelectors(structureLens); | ||
const empty = { | ||
entities: {}, | ||
ids: [] | ||
}; | ||
const createState = makeCreateState(deps); | ||
return { | ||
createState, | ||
empty, | ||
lensedReducers, | ||
reducers, | ||
selectors, | ||
withLens | ||
}; | ||
}; | ||
/** | ||
* A constant that is a good initial state for any entity state. | ||
*/ const EMPTY_ENTITY_STATE = { | ||
entities: {}, | ||
ids: [] | ||
}; | ||
/** | ||
* Returns the entity for a given id, if no id is provided it returns the first entity. | ||
* | ||
* @param id Optional entity id. | ||
* @return Entity represented by the id, or the first entity. | ||
*/ const getDefaultedEntity = (id)=>(state)=>{ | ||
const defaultedId = id !== null && id !== void 0 ? id : state.ids[0]; | ||
if (defaultedId === undefined) { | ||
return undefined; | ||
} | ||
return state.entities[defaultedId]; | ||
} | ||
; | ||
export { ActionBus, EMPTY_ENTITY_STATE, InitAction, Store, UpdateReducersAction, and, asyncBuilder, asyncBuilderWithStructureLens, buildActionCreators, buildAsyncEffect, buildAsyncEffectWithLens, buildCombinedLensedReducers, buildCombinedReducers, buildFailActionCreator, buildFailureActionReducer, buildFailureStateReducer, buildInitActionCreator, buildInitActionReducer, buildInitStateReducer, buildLoadingActionCreator, buildLoadingActionReducer, buildLoadingMoreActionCreator, buildLoadingMoreActionReducer, buildLoadingMoreStateReducer, buildLoadingStateReducer, buildPayloadLens, buildRevertActionCreator, buildRevertActionReducer, buildRevertStateReducer, buildSelectFailure, buildSelectPayload, buildSelectStatus, buildSucceedActionCreator, buildSuccessActionReducer, buildSuccessStateReducer, combineActionReducers, combineEffects, composeStateReducers, createActionCreator, createAsyncActionCreators, createAsyncAdaptor, createCombinedReducer, createEntityAdaptor, createFailureState, createHandler, createIdleState, createLensedReducers, createLoadingMoreState, createLoadingState, createReducers, createSelectors, createSuccessState, defaultIdSelector, fromEntityArray, getDefaultedEntity, getSelectors, isSubtype, isType, makeAddMany, makeAddOne, makeAsyncEffect, makeCreateState, makeReducers, makeRemoveAll, makeRemoveMany, makeRemoveOne, makeSelectAll, makeSelectById, makeSelectByIds, makeSelectByIndex, makeSelectByIndices, makeSelectFailure, makeSelectPayload, makeSelectStatus, makeSetAll, makeSetMany, makeSetOne, matches, notOfType, ofType, on, optionalSource, or, requireSource, sortIfNeeded, withActionKey, withAsyncCallback, withEnableLoadingMore, withMapOnFailure, withMapOnSuccess, withMetadata, withOptimisticUpdate, withStructureLens, withTriggerActionFilter }; | ||
export { ActionBus, InitAction, Store, UpdateReducersAction, and, combineActionReducers, combineEffects, composeStateReducers, createActionCreator, createHandler, isSubtype, isType, matches, notOfType, ofType, on, optionalSource, or, requireSource }; |
1165
index.js
@@ -7,5 +7,2 @@ 'use strict'; | ||
var rxjs = require('rxjs'); | ||
var standardCompose = require('@brandingbrand/standard-compose'); | ||
var standardResult = require('@brandingbrand/standard-result'); | ||
var standardLens = require('@brandingbrand/standard-lens'); | ||
var equal = require('fast-deep-equal'); | ||
@@ -90,252 +87,3 @@ | ||
function _defineProperty$6(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$6(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$6(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
function asyncBuilder() { | ||
return {}; | ||
} | ||
const asyncBuilderWithStructureLens = (lens)=>({ | ||
lens | ||
}) | ||
; | ||
const withStructureLens = (lens)=>(asyncBuilder1)=>_objectSpread$6({}, asyncBuilder1, { | ||
lens | ||
}) | ||
; | ||
const withActionKey = (actionKey)=>(builder)=>_objectSpread$6({}, builder, { | ||
actionKey | ||
}) | ||
; | ||
const withMetadata = (metadata)=>(builder)=>_objectSpread$6({}, builder, { | ||
metadata | ||
}) | ||
; | ||
const withTriggerActionFilter = (triggerActionFilter)=>(builder)=>_objectSpread$6({}, builder, { | ||
triggerActionFilter | ||
}) | ||
; | ||
const withAsyncCallback = (callback)=>(builder)=>_objectSpread$6({}, builder, { | ||
callback | ||
}) | ||
; | ||
const withMapOnSuccess = (mapOnSuccess)=>(builder)=>_objectSpread$6({}, builder, { | ||
mapOnSuccess | ||
}) | ||
; | ||
const withMapOnFailure = (mapOnFailure)=>(builder)=>_objectSpread$6({}, builder, { | ||
mapOnFailure | ||
}) | ||
; | ||
const withOptimisticUpdate = (prediction)=>(builder)=>_objectSpread$6({}, builder, { | ||
prediction | ||
}) | ||
; | ||
const withEnableLoadingMore = (enableLoadMore)=>(builder)=>_objectSpread$6({}, builder, { | ||
enableLoadMore | ||
}) | ||
; | ||
function _defineProperty$5(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$5(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$5(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
/** | ||
* Takes a builder and returns an init action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildInitActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:init', | ||
callback: (idlePayload)=>idlePayload | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a loading action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildLoadingActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
actionKey: builder.actionKey, | ||
subtype: 'async:load', | ||
callback: (loadingPayload)=>loadingPayload | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a loading more action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildLoadingMoreActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:load-more', | ||
callback: (loadingPayload)=>loadingPayload | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a success action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildSucceedActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:succeed', | ||
callback: (succeedPayload)=>succeedPayload | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a failure action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildFailActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:fail', | ||
callback: (failure)=>failure | ||
})) | ||
; | ||
/** | ||
* Takes a builder and returns a revert action creator. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildRevertActionCreator = (builder)=>createActionCreator(_objectSpread$5({}, builder, { | ||
subtype: 'async:revert', | ||
callback: (revertPayload)=>revertPayload | ||
})) | ||
; | ||
/** | ||
* Builds a full suite of async action creators. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildActionCreators = (builder)=>standardCompose.pipe(builder, standardCompose.branchObject({ | ||
init: buildInitActionCreator, | ||
load: buildLoadingActionCreator, | ||
loadMore: buildLoadingMoreActionCreator, | ||
succeed: buildSucceedActionCreator, | ||
fail: buildFailActionCreator, | ||
revert: buildRevertActionCreator | ||
})) | ||
; | ||
/** | ||
* Given a correctly-typed builder object, construct an Effect that will be triggered by the | ||
* triggering action, instigate loading states, run optimistic predictions, and end with success or | ||
* failure. Also reverts optimistic predictions if necessary on failure. | ||
* | ||
* @param builder | ||
* @return The async effect | ||
*/ const buildAsyncEffect = (builder)=>(action$, state$)=>{ | ||
// load$ gets triggered by the triggerActionFilter, runs the prediction if given, and emits | ||
// loading or loadingMore actions | ||
const load$ = action$.pipe(// these typeguards don't have perfect Typescript defs | ||
rxjs.filter(builder.triggerActionFilter), rxjs.withLatestFrom(state$), rxjs.map(([action, state])=>{ | ||
const predictedPayload = builder.prediction ? builder.prediction(action.payload)(state.payload) : state.payload; | ||
if (builder.enableLoadMore === true && (state.status === 'success' || state.status === 'loading-more')) { | ||
return buildLoadingMoreActionCreator(builder).create(predictedPayload); | ||
} | ||
return buildLoadingActionCreator(builder).create(predictedPayload); | ||
})); | ||
// callbackAction$ is also triggered by the triggerActionFilter, whose payload serves as the | ||
// argument for the callback. It runs the callback, judges success vs failure, and emits the | ||
// appropriate action accordingly. If it was a failure, also emits a revert if we had a prediction. | ||
const callbackAction$ = action$.pipe(rxjs.filter(builder.triggerActionFilter), // grab state to get back to if we have optimistic updates on and we fail | ||
rxjs.withLatestFrom(state$), rxjs.switchMap(([action, stateAtStart])=>rxjs.from(// take advantage of the flattening of Promises - wrap it in a promise regardless | ||
new Promise((resolve, reject)=>{ | ||
try { | ||
resolve(builder.callback(action.payload)); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}).then(standardResult.ok).catch(standardResult.fail)).pipe(// grab latest state for mapOnSuccess etc functions to use | ||
rxjs.withLatestFrom(state$), rxjs.mergeMap(([wrappedResult, stateAtReturn])=>{ | ||
if (standardResult.isOk(wrappedResult)) { | ||
const result = wrappedResult.ok; | ||
// Type coercion is due to Typescript not following unions quite right | ||
const mappedNewState = builder.mapOnSuccess ? builder.mapOnSuccess(result)(stateAtReturn.payload) : result; | ||
return rxjs.of(buildSucceedActionCreator(builder).create(mappedNewState)); | ||
} | ||
const result = wrappedResult.failure; | ||
const mappedFailure = builder.mapOnFailure ? builder.mapOnFailure(result)(stateAtReturn.failure) : result; | ||
return builder.prediction ? rxjs.of(buildRevertActionCreator(builder).create(stateAtStart.payload), buildFailActionCreator(builder).create(mappedFailure)) : rxjs.of(buildFailActionCreator(builder).create(mappedFailure)); | ||
})) | ||
)); | ||
return rxjs.merge(load$, callbackAction$); | ||
} | ||
; | ||
/** | ||
* Given a correctly-typed builder object, construct an Effect that will be triggered by the | ||
* triggering action, instigate loading states, run optimistic predictions, and end with success or | ||
* failure. Also reverts optimistic predictions if necessary on failure. This runs inside a lens, so | ||
* as to operate on a larger data structure. | ||
* | ||
* @param builder | ||
* @return The async effect | ||
*/ const buildAsyncEffectWithLens = (builder)=>(action$, state$)=>// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any is of limited scope and not worth specifying | ||
buildAsyncEffect(builder)(action$, state$.pipe(rxjs.map(builder.lens.get))) | ||
; | ||
/** | ||
* If we input a lens it outputs a lens that goes from the outer structure of the input | ||
* lens to the async state's payload. If we do not input a lens it outputs a lens that goes | ||
* from the async state to its payload. | ||
* | ||
* @param builder | ||
* @return | ||
*/ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- we're using overloads | ||
function buildPayloadLens(builder) { | ||
const innerPayloadLens = standardLens.createLensCreator().fromPath('payload'); | ||
if (builder.lens) { | ||
return innerPayloadLens.withOuterLens(builder.lens); | ||
} | ||
return innerPayloadLens; | ||
} | ||
/** | ||
* Combines multiple effects into one, merging all of their observables. | ||
@@ -516,842 +264,3 @@ * | ||
const createIdleState = (payload)=>({ | ||
status: 'idle', | ||
payload | ||
}) | ||
; | ||
const createLoadingState = (payload)=>({ | ||
status: 'loading', | ||
payload | ||
}) | ||
; | ||
const createLoadingMoreState = (payload)=>({ | ||
status: 'loading-more', | ||
payload | ||
}) | ||
; | ||
const createSuccessState = (payload)=>({ | ||
status: 'success', | ||
payload | ||
}) | ||
; | ||
const createFailureState = (payload, failure)=>({ | ||
status: 'failure', | ||
payload, | ||
failure | ||
}) | ||
; | ||
function _defineProperty$4(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$4(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$4(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
/** | ||
* Given a payload it will return a state reducer that gives us an idle state of that payload. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildInitStateReducer = (_builder)=>(payload)=>()=>createIdleState(payload) | ||
; | ||
/** | ||
* Given a payload it will return a state reducer that gives us a loading state of that payload. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildLoadingStateReducer = (_builder)=>(payload)=>()=>createLoadingState(payload) | ||
; | ||
/** | ||
* Given a payload it will return a state reducer that gives us a loading more state | ||
* of that payload. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildLoadingMoreStateReducer = (_builder)=>(payload)=>()=>createLoadingMoreState(payload) | ||
; | ||
/** | ||
* Given a payload it will return a state reducer that gives us a success state of that payload. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildSuccessStateReducer = (_builder)=>(payload)=>()=>createSuccessState(payload) | ||
; | ||
/** | ||
* Given a failure it will return a state reducer that gives us a failure state. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildFailureStateReducer = (_builder)=>(failure)=>(state)=>createFailureState(state.payload, failure) | ||
; | ||
/** | ||
* Given a payload it will return a state reducer that reverts to the given payload but leaving | ||
* the status the same. | ||
* | ||
* @param _builder | ||
* @return | ||
*/ const buildRevertStateReducer = (_builder)=>(payload)=>(state)=>// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- fixes a nuanced type issue. | ||
_objectSpread$4({}, state, { | ||
payload | ||
}) | ||
; | ||
const makeActionReducer = (actionCreator, stateReducer)=>on(matches(actionCreator), (action)=>stateReducer(action.payload) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in an idle state. | ||
* | ||
* @param builder A builder with an action key and an idle type. | ||
* @return | ||
*/ const buildInitActionReducer = (builder)=>standardCompose.pipe(builder, // buildInitActionCreator will take WithMetadata and do the right thing. | ||
standardCompose.branch(buildInitActionCreator, buildInitStateReducer), ([actionCreator, stateReducerCreator])=>makeActionReducer(actionCreator, stateReducerCreator) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a loading state. | ||
* | ||
* @param builder A builder with an action key and a loading type. | ||
* @return | ||
*/ const buildLoadingActionReducer = (builder)=>standardCompose.pipe(builder, standardCompose.branch(buildLoadingActionCreator, buildLoadingStateReducer), ([actionCreator, stateReducerCreator])=>on(matches(actionCreator), (action)=>stateReducerCreator(action.payload) | ||
) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a loading more state. | ||
* | ||
* @param builder A builder with an action key and a loading more type. | ||
* @return | ||
*/ const buildLoadingMoreActionReducer = (builder)=>standardCompose.pipe(builder, standardCompose.branch(buildLoadingMoreActionCreator, buildLoadingMoreStateReducer), ([actionCreator, stateReducerCreator])=>makeActionReducer(actionCreator, stateReducerCreator) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a success state. | ||
* | ||
* @param builder A builder with an action key and a success type. | ||
* @return | ||
*/ const buildSuccessActionReducer = (builder)=>standardCompose.pipe(builder, standardCompose.branch(buildSucceedActionCreator, buildSuccessStateReducer), ([actionCreator, stateReducerCreator])=>makeActionReducer(actionCreator, stateReducerCreator) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a failure state. | ||
* | ||
* @param builder A builder with an action key and payload types. | ||
* @return | ||
*/ const buildFailureActionReducer = (builder)=>standardCompose.pipe(builder, standardCompose.branch(buildFailActionCreator, buildFailureStateReducer), ([actionCreator, stateReducerCreator])=>on(matches(actionCreator), (action)=>(state)=>stateReducerCreator(action.payload)(state) | ||
) | ||
) | ||
; | ||
/** | ||
* Given a builder this makes an action reducer that results in a reverted payload. | ||
* | ||
* @param builder A builder with an action key and payload types. | ||
* @return | ||
*/ const buildRevertActionReducer = (builder)=>standardCompose.pipe(builder, standardCompose.branch(buildRevertActionCreator, buildRevertStateReducer), ([actionCreator, stateReducerCreator])=>on(matches(actionCreator), (action)=>stateReducerCreator(action.payload) | ||
) | ||
) | ||
; | ||
/** | ||
* Given a builder it makes an action reducer that looks for all of the async actions | ||
* and performs the appropriate state change. | ||
* | ||
* @param builder A builder with an action key an payload types. | ||
* @return | ||
*/ const buildCombinedReducers = (builder)=>standardCompose.pipe(builder, standardCompose.branch(buildInitActionReducer, buildLoadingActionReducer, buildLoadingMoreActionReducer, buildSuccessActionReducer, buildFailureActionReducer, buildRevertActionReducer), (actionReducers)=>combineActionReducers(...actionReducers) | ||
) | ||
; | ||
/** | ||
* Returns an action reducer from buildCombinedReducers, but run in a lens so that it handles | ||
* a larger data structure. | ||
* | ||
* @param builder | ||
* @return | ||
*/ const buildCombinedLensedReducers = (builder)=>(action)=>standardLens.withLens(builder.lens)(buildCombinedReducers(builder)(action)) | ||
; | ||
/** | ||
* Takes a builder and returns a selector that selects the payload. | ||
* | ||
* @param builder | ||
* @return | ||
*/ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- cant use arrow functions for overloading. | ||
function buildSelectPayload(builder) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-explicit-any -- the any is limited in scope and not worth it to describe. | ||
return buildPayloadLens(builder).get; | ||
} | ||
/** | ||
* Takes a builder and returns a selector that selects the status. | ||
* | ||
* @param builder | ||
* @return | ||
*/ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- cant use arrow functions for overloading. | ||
function buildSelectStatus(builder) { | ||
const statusLens = standardLens.createLensCreator().fromPath('status'); | ||
if (builder.lens) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-explicit-any -- the any is limited in scope and not worth it to describe. | ||
return statusLens.withOuterLens(builder.lens).get; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-explicit-any -- the any is limited in scope and not worth it to describe. | ||
return statusLens.get; | ||
} | ||
/** | ||
* Takes a builder and returns a selector that selects the failure data if available. | ||
* | ||
* @param builder | ||
* @return | ||
*/ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -- cant use arrow functions for overloading. | ||
function buildSelectFailure(builder) { | ||
const failureLens = standardLens.createLensCreator().fromPath('failure'); | ||
if (builder.lens) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return -- the any is limited in scope and not worth it to describe. | ||
return failureLens.withOuterLens(// this coercion is fine; the lens just gets 'failure' from it so if it's not a failure, | ||
// it'll return undefined, which is what we want. | ||
builder.lens).get; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-explicit-any -- the any is limited in scope and not worth it to describe. | ||
return failureLens.get; | ||
} | ||
/** | ||
* | ||
* @param actionKey The key by which you want to call async actions to set state. | ||
* @param source Optional source to emit with the actions to "lock in" these actions to target | ||
* specific reducers. | ||
* @param metadata | ||
* @return `AsyncActionCreators` - init, load, succeed, fail, & revert. | ||
* @deprecated Use builder-based async functions. | ||
*/ const createAsyncActionCreators = (actionKey, source, metadata)=>({ | ||
init: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:init', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}), | ||
load: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:load', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}), | ||
loadMore: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:load-more', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}), | ||
succeed: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:succeed', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}), | ||
fail: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:fail', | ||
source, | ||
metadata, | ||
callback: (failure)=>failure | ||
}), | ||
revert: createActionCreator({ | ||
actionKey, | ||
subtype: 'async:revert', | ||
source, | ||
metadata, | ||
callback: (payload)=>payload | ||
}) | ||
}) | ||
; | ||
/** | ||
* `makeAsyncEffect` is the "raw" creator function that gets used in the `AsyncAdaptor`. It gets | ||
* partially applied in the adaptor creator function itself, leaving the user to fill in the details | ||
* of a particular effect using the second curried parameter. | ||
* | ||
* @param asyncActionCreators The Async Action creators that should be called to dispatch the correct | ||
* actions to update status and payload. | ||
* @return A function that takes in CreateAsyncEffectOptions and returns an Effect | ||
*/ const makeAsyncEffect = (asyncActionCreators)=>(effectOptions)=>(action$, state$)=>{ | ||
var _predict; | ||
const optimisticUpdate = (_predict = effectOptions.predict) !== null && _predict !== void 0 ? _predict : (_params, state)=>state | ||
; | ||
// changes status to loading, with optimistic updates if configured | ||
const emitLoadWithPredictor$ = action$.pipe(// type coercion is due to us filtering Actions, just by their ActionSpecifier properties | ||
operators.filter(effectOptions.when), operators.withLatestFrom(state$), operators.map(([action, state])=>{ | ||
const update = optimisticUpdate(action.payload, state.payload); | ||
if ((state.status === 'success' || state.status === 'loading-more') && effectOptions.loadingMore) { | ||
return asyncActionCreators.loadMore.create(update); | ||
} | ||
return asyncActionCreators.load.create(update); | ||
})); | ||
// runs callback, emits success or failure or revert & failure. | ||
const runCallback$ = action$.pipe(operators.filter(effectOptions.when), // grab state as of init time | ||
operators.withLatestFrom(state$), operators.switchMap(([action, stateAtStart])=>rxjs.from(// success is wrapped in a Right structure, failure in a Left. We don't want to put an | ||
// error in the observable, so we catch it and wrap it for identification in the next step. | ||
effectOptions.do(...action.payload).then(standardResult.ok).catch(standardResult.fail)).pipe(// grab state after callback returned | ||
operators.withLatestFrom(state$), operators.map(([result, currentState])=>{ | ||
if (standardResult.isOk(result)) { | ||
if (effectOptions.mapOnSuccess) { | ||
return asyncActionCreators.succeed.create(effectOptions.mapOnSuccess(result.ok)(currentState.payload)); | ||
} | ||
// onSuccess would have been required if CallbackResult wasn't a Payload, | ||
// so we know this coercion is safe. | ||
return asyncActionCreators.succeed.create(result.ok); | ||
} | ||
if (effectOptions.mapOnFail) { | ||
return asyncActionCreators.fail.create(effectOptions.mapOnFail(result.failure)(currentState.payload, currentState.status === 'failure' ? currentState.failure : undefined)); | ||
} | ||
// onFail would have been required if FailCallbackResult wasn't a FailPayload, | ||
// so we know this coercion is safe. | ||
return asyncActionCreators.fail.create(result.failure); | ||
}), operators.mergeMap((asyncCallbackResult)=>{ | ||
if (matches(asyncActionCreators.fail) && effectOptions.predict !== undefined) { | ||
return rxjs.of(asyncActionCreators.revert.create(stateAtStart.payload), asyncCallbackResult); | ||
} | ||
return rxjs.of(asyncCallbackResult); | ||
})) | ||
)); | ||
return rxjs.merge(emitLoadWithPredictor$, runCallback$); | ||
} | ||
; | ||
function _defineProperty$3(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$3(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$3(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
/** | ||
* @deprecated Use builder-based reducer functions. | ||
*/ const createReducers = ()=>({ | ||
init: (payload)=>()=>createIdleState(payload) | ||
, | ||
load: (payload)=>()=>createLoadingState(payload) | ||
, | ||
loadMore: (payload)=>()=>createLoadingMoreState(payload) | ||
, | ||
succeed: (payload)=>()=>createSuccessState(payload) | ||
, | ||
fail: (failure)=>(state)=>createFailureState(state.payload, failure) | ||
, | ||
revert: (payload)=>(state)=>_objectSpread$3({}, state, { | ||
payload | ||
}) | ||
}) | ||
; | ||
/** | ||
* @param lens | ||
* @deprecated Use builder-based async functions. | ||
*/ const createLensedReducers = (lens)=>{ | ||
const reducers = createReducers(); | ||
return { | ||
init: (payload)=>standardLens.withLens(lens)(reducers.init(payload)) | ||
, | ||
load: (payload)=>standardLens.withLens(lens)(reducers.load(payload)) | ||
, | ||
loadMore: (payload)=>standardLens.withLens(lens)(reducers.loadMore(payload)) | ||
, | ||
succeed: (payload)=>standardLens.withLens(lens)(reducers.succeed(payload)) | ||
, | ||
fail: (failure)=>standardLens.withLens(lens)(reducers.fail(failure)) | ||
, | ||
revert: (payload)=>standardLens.withLens(lens)(reducers.revert(payload)) | ||
}; | ||
}; | ||
/** | ||
* @param actionCreators | ||
* @param lens | ||
* @deprecated Use builder-based async functions. | ||
*/ const createCombinedReducer = (actionCreators, lens)=>{ | ||
const reducers = createLensedReducers(lens); | ||
return combineActionReducers(on(matches(actionCreators.init), (action)=>reducers.init(action.payload) | ||
), on(matches(actionCreators.load), (action)=>reducers.load(action.payload) | ||
), on(matches(actionCreators.loadMore), (action)=>reducers.loadMore(action.payload) | ||
), on(matches(actionCreators.succeed), (action)=>reducers.succeed(action.payload) | ||
), on(matches(actionCreators.fail), (action)=>reducers.fail(action.payload) | ||
), on(matches(actionCreators.revert), (action)=>reducers.revert(action.payload) | ||
)); | ||
}; | ||
const makeSelectPayload = (lens)=>(structure)=>lens.get(structure).payload | ||
; | ||
const makeSelectStatus = (lens)=>(structure)=>lens.get(structure).status | ||
; | ||
const makeSelectFailure = (lens)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
if (state.status === 'failure') { | ||
return state.failure; | ||
} | ||
return undefined; | ||
} | ||
; | ||
const createSelectors = (lens)=>({ | ||
selectPayload: makeSelectPayload(lens), | ||
selectStatus: makeSelectStatus(lens), | ||
selectFailure: makeSelectFailure(lens) | ||
}) | ||
; | ||
function _defineProperty$2(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$2(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$2(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
/** | ||
* @param options | ||
* @deprecated Use `asyncBuilder`-based functions instead. | ||
*/ const createAsyncAdaptor = (options)=>{ | ||
const payloadLens = standardLens.createLens().fromPath('payload'); | ||
var _lens; | ||
const structureLens = (_lens = options.lens) !== null && _lens !== void 0 ? _lens : standardLens.createLens().fromPath(); | ||
const actionCreators = createAsyncActionCreators(options.actionKey, options.emitSource, options.metadata); | ||
const reducers = createReducers(); | ||
const lensedReducers = createLensedReducers(structureLens); | ||
const combinedReducer = createCombinedReducer(actionCreators, structureLens); | ||
const selectors = createSelectors(structureLens); | ||
const createState = (initialPayload)=>createIdleState(initialPayload) | ||
; | ||
const createEffect = (effectOptions)=>{ | ||
const effect = makeAsyncEffect(actionCreators)(effectOptions); | ||
return (action$, state$)=>effect(action$, state$.pipe(operators.map(structureLens.get))) | ||
; | ||
}; | ||
const withLens = (lens)=>createAsyncAdaptor(_objectSpread$2({}, options, { | ||
lens: new standardLens.ComposableLens(structureLens).withOuterLens(lens) | ||
})) | ||
; | ||
return { | ||
createState, | ||
actionCreators, | ||
combinedReducer, | ||
reducers, | ||
lensedReducers, | ||
selectors, | ||
createEffect, | ||
payloadLens, | ||
withLens | ||
}; | ||
}; | ||
function _defineProperty$1(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread$1(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty$1(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
// dedupe ids while preserving order | ||
const mergeIdLists = (...ids)=>ids.reduce((result, id)=>{ | ||
if (!result.includes(id)) { | ||
result.push(id); | ||
} | ||
return result; | ||
}, []) | ||
; | ||
const sortIfNeeded = (deps)=>(items, idList)=>{ | ||
if (!deps.isSorted) { | ||
return idList !== null && idList !== void 0 ? idList : Object.values(items).map(deps.idSelector); | ||
} | ||
const entitiesValues = Object.values(items); | ||
entitiesValues.sort(deps.comparer); | ||
return entitiesValues.map(deps.idSelector); | ||
} | ||
; | ||
const fromEntityArray = (deps)=>(items)=>Object.fromEntries(items.map((val)=>[ | ||
deps.idSelector(val), | ||
val | ||
] | ||
)) | ||
; | ||
const makeCreateState = (deps)=>(initialEntities)=>({ | ||
ids: initialEntities.map(deps.idSelector), | ||
entities: Object.fromEntries(initialEntities.map((val)=>[ | ||
deps.idSelector(val), | ||
val | ||
] | ||
)) | ||
}) | ||
; | ||
/** | ||
* Take an array of items and make a reducer that adds those items to an existing state, | ||
* overriding existing ids that collide. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeSetMany = (deps)=>(items)=>standardLens.withLens(deps.lens)((state)=>{ | ||
const entities = _objectSpread$1({}, state.entities, fromEntityArray(deps)(items)); | ||
const ids = mergeIdLists(...state.ids, ...items.map(deps.idSelector)); | ||
return { | ||
ids: sortIfNeeded(deps)(entities, ids), | ||
entities | ||
}; | ||
}) | ||
; | ||
/** | ||
* Takes an item and makes a reducer that adds that item to an existing state, overriding | ||
* the existing id if it exists. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeSetOne = (deps)=>(item)=>makeSetMany(deps)([ | ||
item | ||
]) | ||
; | ||
/** | ||
* Take an array of items and make a reducer that adds those items to an existing state, | ||
* preserving existing ids. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeAddMany = (deps)=>(items)=>(structure)=>{ | ||
const state = deps.lens.get(structure); | ||
const newIds = items.map(deps.idSelector).filter((val)=>!state.ids.includes(val) | ||
); | ||
if (newIds.length === 0) { | ||
return structure; | ||
} | ||
const newItems = items.filter((val)=>newIds.includes(deps.idSelector(val)) | ||
); | ||
return makeSetMany(deps)(newItems)(structure); | ||
} | ||
; | ||
/** | ||
* Takes an item and makes a reducer that adds that item to an existing state, preserving | ||
* the existing id if it exists. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeAddOne = (deps)=>(item)=>makeAddMany(deps)([ | ||
item | ||
]) | ||
; | ||
/** | ||
* Takes an array of items and replaces the existing state with the incoming state. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeSetAll = (deps)=>(items)=>(structure)=>makeSetMany(deps)(items)(deps.lens.set(makeCreateState(deps)([]))(structure)) | ||
; | ||
/** | ||
* Removes all entities and state ids. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeRemoveAll = (deps)=>()=>(structure)=>deps.lens.set(makeCreateState(deps)([]))(structure) | ||
; | ||
/** | ||
* Takes an array of entity ids and returns a reducer that removes those ids if they exist. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeRemoveMany = (deps)=>(ids)=>standardLens.withLens(deps.lens)((state)=>{ | ||
const newIds = state.ids.filter((id)=>!ids.includes(id) | ||
); | ||
const newEntities = Object.fromEntries(newIds.map((id)=>[ | ||
id, | ||
state.entities[id] | ||
] | ||
)); | ||
// TODO @grayontheweb 04-01-2022 we shouldn't have to type coerce this but | ||
// this is a quick fix | ||
return { | ||
ids: newIds, | ||
entities: newEntities | ||
}; | ||
}) | ||
; | ||
/** | ||
* Takes an entity id, returns a reducer that removes it if it exists. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeRemoveOne = (deps)=>(id)=>makeRemoveMany(deps)([ | ||
id | ||
]) | ||
; | ||
/** | ||
* Returns a full suite of reducers applying the given dependencies. | ||
* | ||
* @param deps | ||
* @return | ||
*/ const makeReducers = (deps)=>({ | ||
addMany: makeAddMany(deps), | ||
addOne: makeAddOne(deps), | ||
setMany: makeSetMany(deps), | ||
setOne: makeSetOne(deps), | ||
setAll: makeSetAll(deps), | ||
removeAll: makeRemoveAll(deps), | ||
removeMany: makeRemoveMany(deps), | ||
removeOne: makeRemoveOne(deps) | ||
}) | ||
; | ||
/** | ||
* Take all of the entities in an entity state from a given lens and returns | ||
* an array of the entities. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectAll = (lens)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
// TODO @grayontheweb 04-01-2022 this pattern is repeated with minor | ||
// variations a few times in this file | ||
return state.ids.reduce((acc, id)=>{ | ||
const entity = state.entities[id]; | ||
return entity ? [ | ||
...acc, | ||
entity | ||
] : acc; | ||
}, []); | ||
} | ||
; | ||
/** | ||
* Select the given Ids from the entity state from a give lens and returns an array of them. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectByIds = (lens)=>(ids)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
return ids.reduce((acc, id)=>{ | ||
const entity = state.entities[id]; | ||
return entity ? [ | ||
...acc, | ||
entity | ||
] : acc; | ||
}, []); | ||
} | ||
; | ||
/** | ||
* Select the given Id from the entity state from a give lens and returns either the entity | ||
* or undefined. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectById = (lens)=>(id)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
return id in state.entities ? state.entities[id] : undefined; | ||
} | ||
; | ||
/** | ||
* Given an index it will select the Nth value if it exists. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectByIndex = (lens)=>(index)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
const id = state.ids[index]; | ||
return id !== undefined ? state.entities[id] : undefined; | ||
} | ||
; | ||
/** | ||
* Given multiple indices it will select the Nth values if they exist. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const makeSelectByIndices = (lens)=>(indices)=>(structure)=>{ | ||
const state = lens.get(structure); | ||
return indices.reduce((acc, index)=>{ | ||
const id = state.ids[index]; | ||
const entity = id ? state.entities[id] : undefined; | ||
return entity !== undefined && entity in state.entities ? [ | ||
...acc, | ||
entity | ||
] : acc; | ||
}, []); | ||
} | ||
; | ||
/** | ||
* Returns the full suite of selectors given the lens. | ||
* | ||
* @param lens | ||
* @return | ||
*/ const getSelectors = (lens)=>({ | ||
selectAll: makeSelectAll(lens), | ||
selectByIds: makeSelectByIds(lens), | ||
selectById: makeSelectById(lens), | ||
selectByIndex: makeSelectByIndex(lens), | ||
selectByIndices: makeSelectByIndices(lens) | ||
}) | ||
; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
const defaultIdSelector = (item)=>item.id | ||
; | ||
/** | ||
* Creates an entity adapter. | ||
* | ||
* @param options Options to create the entity adapter. | ||
* @return | ||
*/ const createEntityAdaptor = (options)=>{ | ||
const isSorted = Boolean(options.comparer); | ||
var _lens; | ||
const structureLens = (_lens = options.lens) !== null && _lens !== void 0 ? _lens : standardLens.createLens().fromPath(); | ||
const deps = _objectSpread({}, options, { | ||
lens: structureLens, | ||
isSorted | ||
}); | ||
const unlensedDeps = _objectSpread({}, options, { | ||
isSorted, | ||
lens: standardLens.createLens().fromPath() | ||
}); | ||
const lensedReducers = makeReducers(deps); | ||
const reducers = makeReducers(unlensedDeps); | ||
const withLens = (lens)=>createEntityAdaptor(_objectSpread({}, options, { | ||
lens: new standardLens.ComposableLens(structureLens).withOuterLens(lens) | ||
})) | ||
; | ||
const selectors = getSelectors(structureLens); | ||
const empty = { | ||
entities: {}, | ||
ids: [] | ||
}; | ||
const createState = makeCreateState(deps); | ||
return { | ||
createState, | ||
empty, | ||
lensedReducers, | ||
reducers, | ||
selectors, | ||
withLens | ||
}; | ||
}; | ||
/** | ||
* A constant that is a good initial state for any entity state. | ||
*/ const EMPTY_ENTITY_STATE = { | ||
entities: {}, | ||
ids: [] | ||
}; | ||
/** | ||
* Returns the entity for a given id, if no id is provided it returns the first entity. | ||
* | ||
* @param id Optional entity id. | ||
* @return Entity represented by the id, or the first entity. | ||
*/ const getDefaultedEntity = (id)=>(state)=>{ | ||
const defaultedId = id !== null && id !== void 0 ? id : state.ids[0]; | ||
if (defaultedId === undefined) { | ||
return undefined; | ||
} | ||
return state.entities[defaultedId]; | ||
} | ||
; | ||
exports.ActionBus = ActionBus; | ||
exports.EMPTY_ENTITY_STATE = EMPTY_ENTITY_STATE; | ||
exports.InitAction = InitAction; | ||
@@ -1361,31 +270,2 @@ exports.Store = Store; | ||
exports.and = and; | ||
exports.asyncBuilder = asyncBuilder; | ||
exports.asyncBuilderWithStructureLens = asyncBuilderWithStructureLens; | ||
exports.buildActionCreators = buildActionCreators; | ||
exports.buildAsyncEffect = buildAsyncEffect; | ||
exports.buildAsyncEffectWithLens = buildAsyncEffectWithLens; | ||
exports.buildCombinedLensedReducers = buildCombinedLensedReducers; | ||
exports.buildCombinedReducers = buildCombinedReducers; | ||
exports.buildFailActionCreator = buildFailActionCreator; | ||
exports.buildFailureActionReducer = buildFailureActionReducer; | ||
exports.buildFailureStateReducer = buildFailureStateReducer; | ||
exports.buildInitActionCreator = buildInitActionCreator; | ||
exports.buildInitActionReducer = buildInitActionReducer; | ||
exports.buildInitStateReducer = buildInitStateReducer; | ||
exports.buildLoadingActionCreator = buildLoadingActionCreator; | ||
exports.buildLoadingActionReducer = buildLoadingActionReducer; | ||
exports.buildLoadingMoreActionCreator = buildLoadingMoreActionCreator; | ||
exports.buildLoadingMoreActionReducer = buildLoadingMoreActionReducer; | ||
exports.buildLoadingMoreStateReducer = buildLoadingMoreStateReducer; | ||
exports.buildLoadingStateReducer = buildLoadingStateReducer; | ||
exports.buildPayloadLens = buildPayloadLens; | ||
exports.buildRevertActionCreator = buildRevertActionCreator; | ||
exports.buildRevertActionReducer = buildRevertActionReducer; | ||
exports.buildRevertStateReducer = buildRevertStateReducer; | ||
exports.buildSelectFailure = buildSelectFailure; | ||
exports.buildSelectPayload = buildSelectPayload; | ||
exports.buildSelectStatus = buildSelectStatus; | ||
exports.buildSucceedActionCreator = buildSucceedActionCreator; | ||
exports.buildSuccessActionReducer = buildSuccessActionReducer; | ||
exports.buildSuccessStateReducer = buildSuccessStateReducer; | ||
exports.combineActionReducers = combineActionReducers; | ||
@@ -1395,40 +275,5 @@ exports.combineEffects = combineEffects; | ||
exports.createActionCreator = createActionCreator; | ||
exports.createAsyncActionCreators = createAsyncActionCreators; | ||
exports.createAsyncAdaptor = createAsyncAdaptor; | ||
exports.createCombinedReducer = createCombinedReducer; | ||
exports.createEntityAdaptor = createEntityAdaptor; | ||
exports.createFailureState = createFailureState; | ||
exports.createHandler = createHandler; | ||
exports.createIdleState = createIdleState; | ||
exports.createLensedReducers = createLensedReducers; | ||
exports.createLoadingMoreState = createLoadingMoreState; | ||
exports.createLoadingState = createLoadingState; | ||
exports.createReducers = createReducers; | ||
exports.createSelectors = createSelectors; | ||
exports.createSuccessState = createSuccessState; | ||
exports.defaultIdSelector = defaultIdSelector; | ||
exports.fromEntityArray = fromEntityArray; | ||
exports.getDefaultedEntity = getDefaultedEntity; | ||
exports.getSelectors = getSelectors; | ||
exports.isSubtype = isSubtype; | ||
exports.isType = isType; | ||
exports.makeAddMany = makeAddMany; | ||
exports.makeAddOne = makeAddOne; | ||
exports.makeAsyncEffect = makeAsyncEffect; | ||
exports.makeCreateState = makeCreateState; | ||
exports.makeReducers = makeReducers; | ||
exports.makeRemoveAll = makeRemoveAll; | ||
exports.makeRemoveMany = makeRemoveMany; | ||
exports.makeRemoveOne = makeRemoveOne; | ||
exports.makeSelectAll = makeSelectAll; | ||
exports.makeSelectById = makeSelectById; | ||
exports.makeSelectByIds = makeSelectByIds; | ||
exports.makeSelectByIndex = makeSelectByIndex; | ||
exports.makeSelectByIndices = makeSelectByIndices; | ||
exports.makeSelectFailure = makeSelectFailure; | ||
exports.makeSelectPayload = makeSelectPayload; | ||
exports.makeSelectStatus = makeSelectStatus; | ||
exports.makeSetAll = makeSetAll; | ||
exports.makeSetMany = makeSetMany; | ||
exports.makeSetOne = makeSetOne; | ||
exports.matches = matches; | ||
@@ -1441,11 +286,1 @@ exports.notOfType = notOfType; | ||
exports.requireSource = requireSource; | ||
exports.sortIfNeeded = sortIfNeeded; | ||
exports.withActionKey = withActionKey; | ||
exports.withAsyncCallback = withAsyncCallback; | ||
exports.withEnableLoadingMore = withEnableLoadingMore; | ||
exports.withMapOnFailure = withMapOnFailure; | ||
exports.withMapOnSuccess = withMapOnSuccess; | ||
exports.withMetadata = withMetadata; | ||
exports.withOptimisticUpdate = withOptimisticUpdate; | ||
exports.withStructureLens = withStructureLens; | ||
exports.withTriggerActionFilter = withTriggerActionFilter; |
{ | ||
"name": "@brandingbrand/cargo-hold", | ||
"version": "11.67.0", | ||
"version": "11.68.0", | ||
"license": "MIT", | ||
@@ -10,6 +10,2 @@ "typings": "./src/index.d.ts", | ||
"rxjs": "^7.5.2", | ||
"@brandingbrand/standard-compose": "11.67.0", | ||
"@brandingbrand/standard-result": "11.67.0", | ||
"@brandingbrand/types-utility": "11.67.0", | ||
"@brandingbrand/standard-lens": "11.67.0", | ||
"fast-deep-equal": "^3.1.3" | ||
@@ -16,0 +12,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# cargo-hold | ||
# shared-util-cargo-hold | ||
@@ -3,0 +3,0 @@ ## branding brand's state management library |
export * from './action-bus'; | ||
export * from './adapters'; | ||
export * from './entity'; | ||
export * from './store'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
51174
21
909
1
- Removed@brandingbrand/standard-lens@11.67.0
- Removed@brandingbrand/types-utility@11.67.0
- Removed@brandingbrand/standard-branded@11.67.0(transitive)
- Removed@brandingbrand/standard-compose@11.67.0(transitive)
- Removed@brandingbrand/standard-lens@11.67.0(transitive)
- Removed@brandingbrand/standard-result@11.67.0(transitive)
- Removed@brandingbrand/types-utility@11.67.0(transitive)