@one-paragon/angular-utilities 2.1.5 → 2.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/action-state/action-state-spinner/action-state-spinner.component.d.ts +12 -0
- package/action-state/action-state-ui/action-state-ui.module.d.ts +7 -0
- package/{src/action-state/index.ts → action-state/index.d.ts} +4 -8
- package/action-state/ngrx-ext/ngrx-ext.module.d.ts +8 -0
- package/action-state/ngrx.d.ts +31 -0
- package/fesm2022/one-paragon-angular-utilities.mjs +6346 -0
- package/fesm2022/one-paragon-angular-utilities.mjs.map +1 -0
- package/http-request-state/RequestStateFactory.d.ts +17 -0
- package/http-request-state/RequestStateStore.d.ts +101 -0
- package/{src/http-request-state/deprecated.ts → http-request-state/deprecated.d.ts} +20 -20
- package/http-request-state/directives/HttpStateDirectiveBase.d.ts +14 -0
- package/http-request-state/directives/http-error-state-directive.d.ts +9 -0
- package/http-request-state/directives/http-inProgress-state-directive.d.ts +9 -0
- package/http-request-state/directives/http-notStarted-state-directive.d.ts +9 -0
- package/http-request-state/directives/http-success-state-directive.d.ts +16 -0
- package/{src/http-request-state/directives/index.ts → http-request-state/directives/index.d.ts} +5 -5
- package/http-request-state/directives/request-state-directive.d.ts +34 -0
- package/http-request-state/helpers.d.ts +9 -0
- package/http-request-state/http-state-module.d.ts +11 -0
- package/{src/http-request-state/index.ts → http-request-state/index.d.ts} +7 -7
- package/http-request-state/request-state.d.ts +12 -0
- package/http-request-state/rxjs/getRequestorBody.d.ts +3 -0
- package/http-request-state/rxjs/getRequestorState.d.ts +3 -0
- package/{src/http-request-state/rxjs/index.ts → http-request-state/rxjs/index.d.ts} +4 -4
- package/http-request-state/rxjs/tapError.d.ts +3 -0
- package/http-request-state/rxjs/tapSuccess.d.ts +3 -0
- package/http-request-state/types.d.ts +41 -0
- package/index.d.ts +5 -0
- package/ngrx/actionable-selector.d.ts +32 -0
- package/ngrx/index.d.ts +1 -0
- package/package.json +27 -15
- package/{src/public-api.ts → public-api.d.ts} +16 -35
- package/rxjs/defaultShareReplay.d.ts +2 -0
- package/{src/rxjs/index.ts → rxjs/index.d.ts} +5 -5
- package/rxjs/mapError.d.ts +2 -0
- package/rxjs/rxjs-operators.d.ts +13 -0
- package/rxjs/subjectifier.d.ts +10 -0
- package/rxjs/subscriber.directive.d.ts +14 -0
- package/table-builder/classes/DefaultSettings.d.ts +9 -0
- package/table-builder/classes/MatTableObservableDataSource.d.ts +9 -0
- package/table-builder/classes/TableBuilderConfig.d.ts +23 -0
- package/table-builder/classes/TableBuilderDataSource.d.ts +18 -0
- package/table-builder/classes/TableState.d.ts +81 -0
- package/table-builder/classes/data-store.d.ts +8 -0
- package/{src/table-builder/classes/display-col.ts → table-builder/classes/display-col.d.ts} +5 -5
- package/table-builder/classes/filter-info.d.ts +39 -0
- package/table-builder/classes/table-builder-general-settings.d.ts +117 -0
- package/table-builder/classes/table-builder.d.ts +22 -0
- package/table-builder/classes/table-store.d.ts +146 -0
- package/table-builder/classes/table-store.helpers.d.ts +31 -0
- package/table-builder/components/array-column.component.d.ts +15 -0
- package/table-builder/components/column-builder/column-builder.component.d.ts +41 -0
- package/table-builder/components/column-builder/column-helpers.d.ts +38 -0
- package/table-builder/components/column-header-menu/column-header-menu.component.d.ts +50 -0
- package/table-builder/components/date-filter/date-filter.component.d.ts +37 -0
- package/table-builder/components/date-time-filter/date-time-filter.component.d.ts +37 -0
- package/table-builder/components/filter/filter.component.d.ts +48 -0
- package/table-builder/components/filter/in-list/in-list-filter.component.d.ts +23 -0
- package/table-builder/components/gen-col-displayer/gen-col-displayer.component.d.ts +17 -0
- package/table-builder/components/generic-table/generic-table.component.d.ts +84 -0
- package/table-builder/components/generic-table/paginator.component.d.ts +26 -0
- package/table-builder/components/group-by-list/group-by-list.component.d.ts +11 -0
- package/table-builder/components/in-filter/in-filter.component.d.ts +20 -0
- package/{src/table-builder/components/index.ts → table-builder/components/index.d.ts} +9 -9
- package/table-builder/components/initialization-component/initialization-component.d.ts +15 -0
- package/table-builder/components/link-column.component.d.ts +23 -0
- package/table-builder/components/number-filter/number-filter.component.d.ts +39 -0
- package/table-builder/components/profiles-menu/profiles-menu.component.d.ts +33 -0
- package/table-builder/components/reset-menu/reset-menu.component.d.ts +25 -0
- package/table-builder/components/scroll-strategy.d.ts +45 -0
- package/table-builder/components/sort-menu/sort-menu.component-store.d.ts +24 -0
- package/table-builder/components/sort-menu/sort-menu.component.d.ts +19 -0
- package/table-builder/components/table-container/table-container.d.ts +104 -0
- package/table-builder/components/table-container/table-container.helpers/data-state.helpers.d.ts +7 -0
- package/table-builder/components/table-container/table-container.helpers/filter-state.helpers.d.ts +19 -0
- package/table-builder/components/table-container/table-container.helpers/groupBy.helpers.d.ts +19 -0
- package/table-builder/components/table-container/table-container.helpers/meta-data.helpers.d.ts +2 -0
- package/table-builder/components/table-container/table-container.helpers/sort-state.helpers.d.ts +12 -0
- package/table-builder/components/table-container/tableProps.d.ts +10 -0
- package/table-builder/components/table-container/virtual-scroll-container.d.ts +40 -0
- package/table-builder/components/table-container-filter/filter-list/filter-list.component.d.ts +15 -0
- package/table-builder/components/table-container-filter/gen-filter-displayer/gen-filter-displayer.component.d.ts +12 -0
- package/table-builder/components/table-container-filter/table-wrapper-filter-store.d.ts +14 -0
- package/table-builder/components/table-header-menu/table-header-menu.component.d.ts +15 -0
- package/table-builder/directives/custom-cell-directive.d.ts +34 -0
- package/{src/table-builder/directives/index.ts → table-builder/directives/index.d.ts} +5 -6
- package/table-builder/directives/multi-sort.directive.d.ts +10 -0
- package/table-builder/directives/resize-column.directive.d.ts +43 -0
- package/table-builder/directives/table-wrapper.directive.d.ts +8 -0
- package/table-builder/directives/tb-filter.directive.d.ts +116 -0
- package/table-builder/enums/filterTypes.d.ts +36 -0
- package/table-builder/functions/boolean-filter-function.d.ts +3 -0
- package/table-builder/functions/date-filter-function.d.ts +4 -0
- package/table-builder/functions/download-data.d.ts +1 -0
- package/table-builder/functions/null-filter-function.d.ts +2 -0
- package/table-builder/functions/number-filter-function.d.ts +4 -0
- package/table-builder/functions/sort-data-function.d.ts +6 -0
- package/table-builder/functions/string-filter-function.d.ts +5 -0
- package/{src/table-builder/interfaces/ColumnInfo.ts → table-builder/interfaces/ColumnInfo.d.ts} +6 -7
- package/{src/table-builder/interfaces/dictionary.ts → table-builder/interfaces/dictionary.d.ts} +3 -3
- package/table-builder/interfaces/report-def.d.ts +171 -0
- package/table-builder/ngrx/tableBuilderStateStore.d.ts +69 -0
- package/table-builder/pipes/column-total.pipe.d.ts +8 -0
- package/table-builder/pipes/format-filter-type.pipe.d.ts +8 -0
- package/table-builder/pipes/format-filter-value.pipe.d.ts +11 -0
- package/table-builder/pipes/key-display.d.ts +9 -0
- package/table-builder/services/export-to-csv.service.d.ts +14 -0
- package/table-builder/services/link-creator.service.d.ts +16 -0
- package/table-builder/services/table-template-service.d.ts +14 -0
- package/table-builder/services/transform-creator.d.ts +7 -0
- package/table-builder/table-builder.module.d.ts +14 -0
- package/utilities/array-helpers.d.ts +1 -0
- package/utilities/directives/auto-focus.directive.d.ts +9 -0
- package/utilities/directives/clickEmitterDirective.d.ts +7 -0
- package/utilities/directives/clickSubject.d.ts +9 -0
- package/utilities/directives/conditional-classes.directive.d.ts +12 -0
- package/utilities/directives/dialog-service.d.ts +10 -0
- package/utilities/directives/dialog.d.ts +45 -0
- package/utilities/directives/mat-toggle-group-directive.d.ts +21 -0
- package/utilities/directives/prevent-enter.directive.d.ts +6 -0
- package/utilities/directives/stop-propagation.directive.d.ts +7 -0
- package/utilities/directives/styler.d.ts +16 -0
- package/utilities/directives/trim-whitespace.directive.d.ts +7 -0
- package/{src/utilities/index.ts → utilities/index.d.ts} +15 -22
- package/utilities/module.d.ts +19 -0
- package/utilities/pipes/function.pipe.d.ts +11 -0
- package/utilities/pipes/phone.pipe.d.ts +8 -0
- package/utilities/pipes/space-case.pipes.d.ts +17 -0
- package/karma.conf.js +0 -44
- package/ng-package.json +0 -7
- package/src/action-state/action-state-spinner/action-state-spinner.component.css +0 -16
- package/src/action-state/action-state-spinner/action-state-spinner.component.html +0 -6
- package/src/action-state/action-state-spinner/action-state-spinner.component.spec.ts +0 -25
- package/src/action-state/action-state-spinner/action-state-spinner.component.ts +0 -25
- package/src/action-state/action-state-ui/action-state-ui.module.ts +0 -13
- package/src/action-state/ngrx-ext/ngrx-ext.module.ts +0 -15
- package/src/action-state/ngrx.ts +0 -69
- package/src/http-request-state/RequestStateFactory.ts +0 -56
- package/src/http-request-state/RequestStateStore.ts +0 -284
- package/src/http-request-state/directives/HttpStateDirectiveBase.ts +0 -29
- package/src/http-request-state/directives/http-error-state-directive.ts +0 -21
- package/src/http-request-state/directives/http-inProgress-state-directive.ts +0 -19
- package/src/http-request-state/directives/http-notStarted-state-directive.ts +0 -19
- package/src/http-request-state/directives/http-success-state-directive.ts +0 -29
- package/src/http-request-state/directives/request-state-directive.spec.ts +0 -73
- package/src/http-request-state/directives/request-state-directive.ts +0 -78
- package/src/http-request-state/helpers.ts +0 -30
- package/src/http-request-state/http-state-module.ts +0 -23
- package/src/http-request-state/models/view-context.ts +0 -18
- package/src/http-request-state/observable.spec.ts +0 -43
- package/src/http-request-state/request-state.ts +0 -69
- package/src/http-request-state/rxjs/getRequestorBody.ts +0 -10
- package/src/http-request-state/rxjs/getRequestorState.ts +0 -8
- package/src/http-request-state/rxjs/tapError.ts +0 -16
- package/src/http-request-state/rxjs/tapSuccess.ts +0 -16
- package/src/http-request-state/strategies.spec.ts +0 -42
- package/src/http-request-state/types.ts +0 -54
- package/src/ngrx/actionable-selector.ts +0 -160
- package/src/ngrx/index.ts +0 -1
- package/src/rxjs/defaultShareReplay.ts +0 -8
- package/src/rxjs/mapError.ts +0 -8
- package/src/rxjs/rxjs-operators.ts +0 -130
- package/src/rxjs/subjectifier.ts +0 -17
- package/src/rxjs/subscriber.directive.ts +0 -57
- package/src/specs/clickSubject.spec.ts +0 -95
- package/src/specs/dialog.spec.ts +0 -101
- package/src/specs/toggleGroupDirective.spec.ts +0 -229
- package/src/table-builder/classes/DefaultSettings.ts +0 -11
- package/src/table-builder/classes/MatTableObservableDataSource.ts +0 -23
- package/src/table-builder/classes/TableBuilderConfig.ts +0 -42
- package/src/table-builder/classes/TableBuilderDataSource.ts +0 -65
- package/src/table-builder/classes/TableState.ts +0 -125
- package/src/table-builder/classes/data-store.ts +0 -10
- package/src/table-builder/classes/filter-info.ts +0 -126
- package/src/table-builder/classes/table-builder-general-settings.ts +0 -198
- package/src/table-builder/classes/table-builder.ts +0 -105
- package/src/table-builder/classes/table-store.helpers.ts +0 -104
- package/src/table-builder/classes/table-store.ts +0 -440
- package/src/table-builder/components/array-column.component.ts +0 -34
- package/src/table-builder/components/column-builder/column-builder.component.html +0 -73
- package/src/table-builder/components/column-builder/column-builder.component.scss +0 -43
- package/src/table-builder/components/column-builder/column-builder.component.spec.ts +0 -49
- package/src/table-builder/components/column-builder/column-builder.component.ts +0 -129
- package/src/table-builder/components/column-builder/column-helpers.ts +0 -54
- package/src/table-builder/components/column-header-menu/column-header-menu.component.html +0 -109
- package/src/table-builder/components/column-header-menu/column-header-menu.component.scss +0 -97
- package/src/table-builder/components/column-header-menu/column-header-menu.component.ts +0 -94
- package/src/table-builder/components/date-filter/date-filter.component.html +0 -23
- package/src/table-builder/components/date-filter/date-filter.component.ts +0 -22
- package/src/table-builder/components/date-time-filter/date-time-filter.component.html +0 -9
- package/src/table-builder/components/date-time-filter/date-time-filter.component.ts +0 -20
- package/src/table-builder/components/filter/filter.component.html +0 -103
- package/src/table-builder/components/filter/filter.component.scss +0 -60
- package/src/table-builder/components/filter/filter.component.spec.ts +0 -87
- package/src/table-builder/components/filter/filter.component.ts +0 -64
- package/src/table-builder/components/filter/in-list/in-list-filter.component.ts +0 -85
- package/src/table-builder/components/gen-col-displayer/gen-col-displayer.component.html +0 -60
- package/src/table-builder/components/gen-col-displayer/gen-col-displayer.component.scss +0 -57
- package/src/table-builder/components/gen-col-displayer/gen-col-displayer.component.ts +0 -44
- package/src/table-builder/components/generic-table/generic-table.component.html +0 -96
- package/src/table-builder/components/generic-table/generic-table.component.scss +0 -38
- package/src/table-builder/components/generic-table/generic-table.component.ts +0 -399
- package/src/table-builder/components/generic-table/paginator.component.ts +0 -106
- package/src/table-builder/components/group-by-list/group-by-list.component.css +0 -17
- package/src/table-builder/components/group-by-list/group-by-list.component.html +0 -14
- package/src/table-builder/components/group-by-list/group-by-list.component.spec.ts +0 -23
- package/src/table-builder/components/group-by-list/group-by-list.component.ts +0 -18
- package/src/table-builder/components/in-filter/in-filter.component.css +0 -3
- package/src/table-builder/components/in-filter/in-filter.component.html +0 -19
- package/src/table-builder/components/in-filter/in-filter.component.ts +0 -65
- package/src/table-builder/components/initialization-component/initialization-component.html +0 -78
- package/src/table-builder/components/initialization-component/initialization-component.ts +0 -27
- package/src/table-builder/components/link-column.component.ts +0 -42
- package/src/table-builder/components/number-filter/number-filter.component.css +0 -10
- package/src/table-builder/components/number-filter/number-filter.component.html +0 -22
- package/src/table-builder/components/number-filter/number-filter.component.spec.ts +0 -30
- package/src/table-builder/components/number-filter/number-filter.component.ts +0 -25
- package/src/table-builder/components/profiles-menu/profiles-menu.component.html +0 -76
- package/src/table-builder/components/profiles-menu/profiles-menu.component.scss +0 -126
- package/src/table-builder/components/profiles-menu/profiles-menu.component.spec.ts +0 -23
- package/src/table-builder/components/profiles-menu/profiles-menu.component.ts +0 -63
- package/src/table-builder/components/reset-menu/reset-menu.component.css +0 -3
- package/src/table-builder/components/reset-menu/reset-menu.component.html +0 -10
- package/src/table-builder/components/reset-menu/reset-menu.component.ts +0 -87
- package/src/table-builder/components/scroll-strategy.ts +0 -139
- package/src/table-builder/components/sort-menu/sort-menu.component-store.ts +0 -57
- package/src/table-builder/components/sort-menu/sort-menu.component.html +0 -109
- package/src/table-builder/components/sort-menu/sort-menu.component.scss +0 -119
- package/src/table-builder/components/sort-menu/sort-menu.component.ts +0 -88
- package/src/table-builder/components/table-container/table-container.helpers/data-state.helpers.ts +0 -112
- package/src/table-builder/components/table-container/table-container.helpers/filter-state.helpers.ts +0 -125
- package/src/table-builder/components/table-container/table-container.helpers/groupBy.helpers.ts +0 -120
- package/src/table-builder/components/table-container/table-container.helpers/meta-data.helpers.ts +0 -16
- package/src/table-builder/components/table-container/table-container.helpers/sort-state.helpers.ts +0 -47
- package/src/table-builder/components/table-container/table-container.html +0 -79
- package/src/table-builder/components/table-container/table-container.scss +0 -47
- package/src/table-builder/components/table-container/table-container.ts +0 -403
- package/src/table-builder/components/table-container/tableProps.ts +0 -18
- package/src/table-builder/components/table-container/virtual-scroll-container.ts +0 -216
- package/src/table-builder/components/table-container-filter/filter-list/filter-list.component.html +0 -34
- package/src/table-builder/components/table-container-filter/filter-list/filter-list.component.ts +0 -44
- package/src/table-builder/components/table-container-filter/gen-filter-displayer/gen-filter-displayer.component.css +0 -42
- package/src/table-builder/components/table-container-filter/gen-filter-displayer/gen-filter-displayer.component.html +0 -11
- package/src/table-builder/components/table-container-filter/gen-filter-displayer/gen-filter-displayer.component.spec.ts +0 -85
- package/src/table-builder/components/table-container-filter/gen-filter-displayer/gen-filter-displayer.component.ts +0 -36
- package/src/table-builder/components/table-container-filter/table-wrapper-filter-store.ts +0 -23
- package/src/table-builder/components/table-header-menu/table-header-menu.component.css +0 -21
- package/src/table-builder/components/table-header-menu/table-header-menu.component.html +0 -48
- package/src/table-builder/components/table-header-menu/table-header-menu.component.ts +0 -35
- package/src/table-builder/directives/custom-cell-directive.ts +0 -59
- package/src/table-builder/directives/multi-sort.directive.spec.ts +0 -124
- package/src/table-builder/directives/multi-sort.directive.ts +0 -50
- package/src/table-builder/directives/resize-column.directive.ts +0 -107
- package/src/table-builder/directives/table-wrapper.directive.ts +0 -13
- package/src/table-builder/directives/tb-filter.directive.ts +0 -376
- package/src/table-builder/enums/filterTypes.ts +0 -40
- package/src/table-builder/functions/boolean-filter-function.ts +0 -12
- package/src/table-builder/functions/date-filter-function.ts +0 -68
- package/src/table-builder/functions/download-data.ts +0 -11
- package/src/table-builder/functions/null-filter-function.ts +0 -9
- package/src/table-builder/functions/number-filter-function.ts +0 -41
- package/src/table-builder/functions/sort-data-function.ts +0 -80
- package/src/table-builder/functions/string-filter-function.ts +0 -51
- package/src/table-builder/interfaces/column-template.ts +0 -9
- package/src/table-builder/interfaces/report-def.ts +0 -186
- package/src/table-builder/ngrx/tableBuilderStateStore.ts +0 -197
- package/src/table-builder/pipes/column-total.pipe.ts +0 -16
- package/src/table-builder/pipes/format-filter-type.pipe.ts +0 -12
- package/src/table-builder/pipes/format-filter-value.pipe.ts +0 -40
- package/src/table-builder/pipes/key-display.ts +0 -13
- package/src/table-builder/services/export-to-csv.service.ts +0 -75
- package/src/table-builder/services/link-creator.service.ts +0 -98
- package/src/table-builder/services/table-template-service.ts +0 -47
- package/src/table-builder/services/transform-creator.ts +0 -96
- package/src/table-builder/specs/table-custom-filters.spec.ts +0 -262
- package/src/table-builder/styles/collapser.styles.scss +0 -16
- package/src/table-builder/table-builder.module.ts +0 -38
- package/src/test.ts +0 -17
- package/src/utilities/array-helpers.ts +0 -13
- package/src/utilities/directives/auto-focus.directive.ts +0 -20
- package/src/utilities/directives/clickEmitterDirective.ts +0 -15
- package/src/utilities/directives/clickSubject.ts +0 -19
- package/src/utilities/directives/conditional-classes.directive.ts +0 -36
- package/src/utilities/directives/dialog-service.ts +0 -19
- package/src/utilities/directives/dialog.ts +0 -144
- package/src/utilities/directives/mat-toggle-group-directive.ts +0 -60
- package/src/utilities/directives/prevent-enter.directive.ts +0 -12
- package/src/utilities/directives/stop-propagation.directive.ts +0 -19
- package/src/utilities/directives/styler.ts +0 -44
- package/src/utilities/directives/trim-whitespace.directive.ts +0 -20
- package/src/utilities/module.ts +0 -55
- package/src/utilities/pipes/function.pipe.ts +0 -21
- package/src/utilities/pipes/phone.pipe.ts +0 -20
- package/src/utilities/pipes/space-case.pipes.spec.ts +0 -47
- package/src/utilities/pipes/space-case.pipes.ts +0 -26
- package/tsconfig.lib.json +0 -20
- package/tsconfig.lib.prod.json +0 -10
- package/tsconfig.spec.json +0 -17
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Directive, Input } from '@angular/core';
|
|
2
|
-
import { isSuccessState } from '../helpers';
|
|
3
|
-
import { HttpStateDirectiveBase } from './HttpStateDirectiveBase';
|
|
4
|
-
import { RequestState } from '../types';
|
|
5
|
-
import { RequestStateStore } from '../RequestStateStore';
|
|
6
|
-
import { Observable } from 'rxjs';
|
|
7
|
-
|
|
8
|
-
export interface HttpSuccessStateViewContext<T> {
|
|
9
|
-
$implicit: T;
|
|
10
|
-
}
|
|
11
|
-
@Directive({ selector: '[httpSuccessState]', })
|
|
12
|
-
export class HttpSuccessStateDirective<V, R = null> extends HttpStateDirectiveBase<V, R> {
|
|
13
|
-
@Input() httpSuccessStateTypeSafety?: RequestStateStore<any, V, R> | Observable<RequestState<V>>;
|
|
14
|
-
render = (state: RequestState<R extends null ? V : R>) => {
|
|
15
|
-
if (isSuccessState(state)) {
|
|
16
|
-
this.viewContainer.createEmbeddedView(this.templateRef, { $implicit: state.body });
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
return false;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
ngOnInit() {
|
|
23
|
-
super.ngOnInit();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
static ngTemplateContextGuard<V, R>(dir: HttpSuccessStateDirective<V, R>, ctx: any): ctx is HttpSuccessStateViewContext<R extends null ? V : R> {
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
2
|
-
import { Component, inject } from '@angular/core';
|
|
3
|
-
import { HttpRequestModule } from '../http-state-module';
|
|
4
|
-
import { RequestStateStore } from '../RequestStateStore';
|
|
5
|
-
import { Subject } from 'rxjs';
|
|
6
|
-
import { RequestStateOptions } from '../types';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
@Component({
|
|
10
|
-
template: `
|
|
11
|
-
<div *httpRequestState='requestState;let state;'>
|
|
12
|
-
<ng-container *httpNotStartedState>not started</ng-container>
|
|
13
|
-
<ng-container *httpInProgressState>in progress</ng-container>
|
|
14
|
-
<ng-container *httpSuccessState='let body' >success {{body}}</ng-container>
|
|
15
|
-
<ng-container *httpErrorState='let error' >error {{error}}</ng-container>
|
|
16
|
-
</div>
|
|
17
|
-
`,
|
|
18
|
-
}) export class TestHttpRequestStateDirectiveComponent {
|
|
19
|
-
requestState = inject<RequestStateStore<any, any>>(RequestStateStore);
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class MockHttpRequestState<T> extends RequestStateStore<[], T> {
|
|
24
|
-
subject = new Subject<T>();
|
|
25
|
-
constructor(options?: RequestStateOptions) {
|
|
26
|
-
super(()=> {
|
|
27
|
-
return this.subject;
|
|
28
|
-
}, options );
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let requestState: MockHttpRequestState<string>;
|
|
33
|
-
let fixture: ComponentFixture<TestHttpRequestStateDirectiveComponent>;
|
|
34
|
-
|
|
35
|
-
describe('Request State Directive', () => {
|
|
36
|
-
|
|
37
|
-
beforeEach(() => {
|
|
38
|
-
requestState = new MockHttpRequestState<string>();
|
|
39
|
-
fixture = TestBed.configureTestingModule({
|
|
40
|
-
declarations: [TestHttpRequestStateDirectiveComponent],
|
|
41
|
-
providers: [{ provide: RequestStateStore, useValue: requestState }],
|
|
42
|
-
imports: [HttpRequestModule]
|
|
43
|
-
}).createComponent(TestHttpRequestStateDirectiveComponent);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should begin with the state of not started', () => {
|
|
47
|
-
verifyText('not started');
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should be in progress while in progress', () => {
|
|
51
|
-
requestState.request();
|
|
52
|
-
verifyText('in progress');
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('should be success when success', () => {
|
|
56
|
-
requestState.request();
|
|
57
|
-
requestState.subject.next('yay')
|
|
58
|
-
verifyText('success yay');
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('should be error when error', () => {
|
|
62
|
-
requestState.request();
|
|
63
|
-
requestState.subject.error('ouch');
|
|
64
|
-
verifyText('error ouch');
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
function verifyText(text:string) {
|
|
68
|
-
fixture.detectChanges();
|
|
69
|
-
const node: HTMLElement = fixture.nativeElement;
|
|
70
|
-
expect(node.innerText).toBe(text);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
});
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { Directive, Input, TemplateRef, ViewContainerRef, inject } from '@angular/core';
|
|
2
|
-
import { defaultShareReplay } from '../../rxjs/defaultShareReplay';
|
|
3
|
-
import { isObservable, Observable, Subject, Unsubscribable } from 'rxjs';
|
|
4
|
-
import { map, switchAll } from 'rxjs/operators';
|
|
5
|
-
import { RequestStateStore } from '../RequestStateStore';
|
|
6
|
-
import { RequestState, RequestStatus } from '../types';
|
|
7
|
-
|
|
8
|
-
export interface HttpRequestStateViewContext<T> {
|
|
9
|
-
$implicit?: HttpRequestStateAny<T>;
|
|
10
|
-
state?: HttpRequestStateAny<T>;
|
|
11
|
-
status: {
|
|
12
|
-
inProgress: boolean;
|
|
13
|
-
notStarted: boolean;
|
|
14
|
-
success: boolean;
|
|
15
|
-
error: boolean;
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
@Directive({ selector: '[httpRequestState]', })
|
|
20
|
-
export class HttpRequestStateDirective<TParam extends any[], V, R, T = R extends null ? V : R> {
|
|
21
|
-
private viewContainer: ViewContainerRef;
|
|
22
|
-
|
|
23
|
-
readonly ViewContext: HttpRequestStateViewContext<T> = {
|
|
24
|
-
$implicit: undefined,
|
|
25
|
-
state: undefined,
|
|
26
|
-
status: {
|
|
27
|
-
inProgress: false,
|
|
28
|
-
notStarted: true,
|
|
29
|
-
success: false,
|
|
30
|
-
error: false,
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
subject = new Subject<Observable<RequestState<T>>>();
|
|
36
|
-
state = this.subject.pipe(switchAll(), defaultShareReplay());
|
|
37
|
-
subscription: Unsubscribable;
|
|
38
|
-
hooks: ((state: RequestState<T>) => void)[] = [];
|
|
39
|
-
|
|
40
|
-
@Input('httpRequestState') set stateStore(store: RequestStateStore<TParam, V, R, T> | Observable<RequestState<T>>) {
|
|
41
|
-
if (isObservable(store)) {
|
|
42
|
-
this.subject.next(store);
|
|
43
|
-
} else {
|
|
44
|
-
this.subject.next(store.state$.pipe(map(state => state.response)));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
constructor(templateRef: TemplateRef<any>, viewContainer: ViewContainerRef) {
|
|
49
|
-
this.viewContainer = viewContainer;
|
|
50
|
-
this.viewContainer.createEmbeddedView(templateRef, this.ViewContext);
|
|
51
|
-
this.subscription = this.state.subscribe(state => {
|
|
52
|
-
this.ViewContext.$implicit = state;
|
|
53
|
-
this.ViewContext.state = state;
|
|
54
|
-
this.ViewContext.status.inProgress = state.status === RequestStatus.inProgress;
|
|
55
|
-
this.ViewContext.status.notStarted = state.status === RequestStatus.notStarted;
|
|
56
|
-
this.ViewContext.status.success = state.status === RequestStatus.success;
|
|
57
|
-
this.ViewContext.status.error = state.status === RequestStatus.fail;
|
|
58
|
-
this.hooks.forEach(hook => hook(state));
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
ngOnDestroy() {
|
|
64
|
-
this.viewContainer.clear();
|
|
65
|
-
this.subscription.unsubscribe();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
static ngTemplateContextGuard<V, R>(dir: HttpRequestStateDirective<any, V, R>, ctx: any): ctx is HttpRequestStateViewContext<R extends null ? V : R> {
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export type HttpRequestStateAny<T> = {
|
|
75
|
-
status: RequestStatus;
|
|
76
|
-
body?: T;
|
|
77
|
-
error?: any;
|
|
78
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { RequestState, RequestStateError, RequestStateInProgress, RequestStateNotStarted, RequestStateSuccess, RequestStatus } from "./types";
|
|
2
|
-
|
|
3
|
-
export function isErrorState<T>(state: RequestState<T>): state is RequestStateError {
|
|
4
|
-
return state.status === RequestStatus.fail;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function isSuccessState<T>(state: RequestState<T>): state is RequestStateSuccess<T> {
|
|
8
|
-
return state.status === RequestStatus.success;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function isSuccessOrErrorState<T>(state: RequestState<T>): state is (RequestStateSuccess<T> | RequestStateError) {
|
|
12
|
-
return state.status === RequestStatus.success || state.status === RequestStatus.fail;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function createSuccess<T>(body: T): RequestStateSuccess<T> {
|
|
16
|
-
return { status: RequestStatus.success, body };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function createFailure(error: any): RequestStateError {
|
|
20
|
-
return { status: RequestStatus.fail, error };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const notStarted: RequestStateNotStarted = { status: RequestStatus.notStarted };
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
export const inProgress: RequestStateInProgress = { status: RequestStatus.inProgress };
|
|
27
|
-
|
|
28
|
-
export function statusIsSuccessOrInProgress( status: RequestStatus) {
|
|
29
|
-
return [RequestStatus.success, RequestStatus.inProgress].includes(status);
|
|
30
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { NgModule } from '@angular/core';
|
|
2
|
-
import { HttpSuccessStateDirective } from './directives/http-success-state-directive';
|
|
3
|
-
import { HttpRequestStateDirective } from './directives/request-state-directive';
|
|
4
|
-
import { HttpErrorStateDirective } from './directives/http-error-state-directive';
|
|
5
|
-
import { HttpInProgressStateDirective } from './directives/http-inProgress-state-directive';
|
|
6
|
-
import { HttpNotStartedStateDirective } from './directives/http-notStarted-state-directive';
|
|
7
|
-
|
|
8
|
-
@NgModule({
|
|
9
|
-
imports: [
|
|
10
|
-
HttpSuccessStateDirective,
|
|
11
|
-
HttpRequestStateDirective,
|
|
12
|
-
HttpErrorStateDirective,
|
|
13
|
-
HttpInProgressStateDirective,
|
|
14
|
-
HttpNotStartedStateDirective,
|
|
15
|
-
],
|
|
16
|
-
exports: [
|
|
17
|
-
HttpSuccessStateDirective,
|
|
18
|
-
HttpRequestStateDirective,
|
|
19
|
-
HttpErrorStateDirective,
|
|
20
|
-
HttpInProgressStateDirective,
|
|
21
|
-
HttpNotStartedStateDirective,
|
|
22
|
-
]
|
|
23
|
-
}) export class HttpRequestModule { }
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { RequestStatus } from "../types";
|
|
2
|
-
|
|
3
|
-
export interface HttpRequestStateViewContext<T> {
|
|
4
|
-
$implicit?: HttpRequestStateAny<T>;
|
|
5
|
-
state?: HttpRequestStateAny<T>;
|
|
6
|
-
status: {
|
|
7
|
-
inProgress: boolean;
|
|
8
|
-
notStarted: boolean;
|
|
9
|
-
success: boolean;
|
|
10
|
-
error: boolean;
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type HttpRequestStateAny<T> = {
|
|
15
|
-
status: RequestStatus;
|
|
16
|
-
body?: T;
|
|
17
|
-
error?: any;
|
|
18
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { MockHttpRequestState } from "./directives/request-state-directive.spec";
|
|
2
|
-
import { createSuccess, inProgress, notStarted, createFailure } from "./helpers";
|
|
3
|
-
|
|
4
|
-
describe('HttpRequestStateStore', () => {
|
|
5
|
-
|
|
6
|
-
it('should begin with the state of not started', (done) => {
|
|
7
|
-
const requestState = new MockHttpRequestState<string>();
|
|
8
|
-
requestState.state$.subscribe( state => {
|
|
9
|
-
expect(state).toEqual({ requestParams: null as any , response: notStarted});
|
|
10
|
-
done();
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('should be in progress when request is made', (done) => {
|
|
15
|
-
const requestState = new MockHttpRequestState<string>();
|
|
16
|
-
requestState.request();
|
|
17
|
-
requestState.state$.subscribe( state => {
|
|
18
|
-
expect(state).toEqual({ requestParams: [], response: inProgress});
|
|
19
|
-
done();
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should be in success when request completes successfully', (done) => {
|
|
24
|
-
const requestState = new MockHttpRequestState<string>();
|
|
25
|
-
requestState.request();
|
|
26
|
-
requestState.subject.next('hi');
|
|
27
|
-
requestState.state$.subscribe( state => {
|
|
28
|
-
expect(state).toEqual({ requestParams: [], response: createSuccess('hi')});
|
|
29
|
-
done();
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should be in error when request errors', (done) => {
|
|
34
|
-
const requestState = new MockHttpRequestState<string>();
|
|
35
|
-
requestState.request();
|
|
36
|
-
requestState.subject.error('error');
|
|
37
|
-
requestState.state$.subscribe( state => {
|
|
38
|
-
expect(state).toEqual({ requestParams: [], response: createFailure('error') });
|
|
39
|
-
done();
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
});
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { map, startWith, switchMap } from 'rxjs/operators';
|
|
2
|
-
import { getRequestorBody } from './rxjs/getRequestorBody';
|
|
3
|
-
import { getRequestorStatus } from './rxjs/getRequestorState';
|
|
4
|
-
import { mapError } from '../rxjs/mapError';
|
|
5
|
-
import { defaultShareReplay } from '../rxjs/defaultShareReplay';
|
|
6
|
-
import { Observable, of, ReplaySubject } from 'rxjs';
|
|
7
|
-
import { RequestFactory, RequestState, RequestStatus } from './types';
|
|
8
|
-
import { createFailure, createSuccess, inProgress, notStarted } from './helpers';
|
|
9
|
-
|
|
10
|
-
export type HttpRequestState$ <T> = Observable<RequestState<T>>;
|
|
11
|
-
|
|
12
|
-
export interface HttpRequestor <Res, Req> {
|
|
13
|
-
httpState$: Observable<RequestState<Res>>;
|
|
14
|
-
status$: Observable<RequestStatus>;
|
|
15
|
-
body$: Observable<Res>;
|
|
16
|
-
request: Req;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function httpRequest<T = any>(req: Observable<T>): HttpRequestState$<T> {
|
|
20
|
-
return req.pipe(
|
|
21
|
-
map(createSuccess),
|
|
22
|
-
mapError(createFailure),
|
|
23
|
-
startWith(inProgress),
|
|
24
|
-
defaultShareReplay()
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function httpRequestor<TParam extends any[], T>(req: RequestFactory<TParam,T> ): HttpRequestor<T, (...params: [...TParam]) => Observable<RequestState<T>>> {
|
|
29
|
-
const request$ = new ReplaySubject<[...TParam]>(1);
|
|
30
|
-
const httpState$ : HttpRequestState$<T> = request$.pipe(
|
|
31
|
-
switchMap( (params) => httpRequest<T>(req(...params)) ),
|
|
32
|
-
startWith(notStarted),
|
|
33
|
-
defaultShareReplay(),
|
|
34
|
-
);
|
|
35
|
-
const request = (...params: [...TParam]) => {
|
|
36
|
-
request$.next(params);
|
|
37
|
-
return httpState$;
|
|
38
|
-
};
|
|
39
|
-
return ({
|
|
40
|
-
request,
|
|
41
|
-
httpState$,
|
|
42
|
-
body$: httpState$.pipe(getRequestorBody),
|
|
43
|
-
status$: httpState$.pipe(getRequestorStatus)
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function chainRequest<TParam extends any[], T, TT>(
|
|
48
|
-
httpState$: Observable<RequestState<T>>,
|
|
49
|
-
request:RequestFactory<TParam,TT>,
|
|
50
|
-
requestParams: ((responseFromPrevious: T) => [...TParam]) | (() => [...TParam])
|
|
51
|
-
): Observable<RequestState<TT>> {
|
|
52
|
-
return httpState$.pipe(
|
|
53
|
-
switchMap( res => {
|
|
54
|
-
if(res.status === RequestStatus.success) {
|
|
55
|
-
const secondReq = httpRequestor<[...TParam], TT>(request);
|
|
56
|
-
const param = requestParams(res.body);
|
|
57
|
-
secondReq.request(...param );
|
|
58
|
-
return secondReq.httpState$;
|
|
59
|
-
} else {
|
|
60
|
-
return of(res);
|
|
61
|
-
}
|
|
62
|
-
}),
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { isSuccessState } from '../helpers';
|
|
2
|
-
import { RequestState } from '../types';
|
|
3
|
-
import { Observable } from 'rxjs';
|
|
4
|
-
import { filter, map } from 'rxjs/operators';
|
|
5
|
-
|
|
6
|
-
export const getRequestorBody = <T>(source: Observable<RequestState<T>>) =>
|
|
7
|
-
source.pipe(
|
|
8
|
-
filter(isSuccessState),
|
|
9
|
-
map( res => res.body)
|
|
10
|
-
);
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Observable } from 'rxjs';
|
|
2
|
-
import { tap } from 'rxjs/operators';
|
|
3
|
-
import { RequestState } from '../types';
|
|
4
|
-
import { isErrorState } from '../helpers';
|
|
5
|
-
|
|
6
|
-
export function tapError(onError: (error: any) => void) {
|
|
7
|
-
return function (src: Observable<RequestState>): Observable<RequestState> {
|
|
8
|
-
return src.pipe(
|
|
9
|
-
tap( result => {
|
|
10
|
-
if(isErrorState(result)) {
|
|
11
|
-
onError(result.error);
|
|
12
|
-
}
|
|
13
|
-
})
|
|
14
|
-
);
|
|
15
|
-
};
|
|
16
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Observable } from 'rxjs';
|
|
2
|
-
import { tap } from 'rxjs/operators';
|
|
3
|
-
import { RequestState } from '../types';
|
|
4
|
-
import { isSuccessState } from '../helpers';
|
|
5
|
-
|
|
6
|
-
export function tapSuccess<T>(onSuccess: (body: T) => void) {
|
|
7
|
-
return function (src: Observable<RequestState<T>>): Observable<RequestState<T>> {
|
|
8
|
-
return src.pipe(
|
|
9
|
-
tap( result => {
|
|
10
|
-
if(isSuccessState(result)) {
|
|
11
|
-
onSuccess(result.body);
|
|
12
|
-
}
|
|
13
|
-
})
|
|
14
|
-
);
|
|
15
|
-
};
|
|
16
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { toArray } from "rxjs/operators";
|
|
2
|
-
import { MockHttpRequestState } from "./directives/request-state-directive.spec";
|
|
3
|
-
import { RequestStrategy } from "./types";
|
|
4
|
-
|
|
5
|
-
describe('HttpRequestStateStore', () => {
|
|
6
|
-
|
|
7
|
-
it('should throw when strategy is use once and it was reused', () => {
|
|
8
|
-
const requestState = new MockHttpRequestState<string>({strategy: RequestStrategy.singleUse});
|
|
9
|
-
requestState.request();
|
|
10
|
-
expect( ()=>{
|
|
11
|
-
requestState.request();
|
|
12
|
-
} ).toThrow();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
it('should emit both when strategy is concurrent', (done) => {
|
|
17
|
-
const requestState = new MockHttpRequestState<string>({strategy: RequestStrategy.concurrent});
|
|
18
|
-
requestState.request();
|
|
19
|
-
requestState.request();
|
|
20
|
-
requestState.selectResponse$.pipe( toArray()
|
|
21
|
-
).subscribe( d => {
|
|
22
|
-
expect(d.length).toBe(2);
|
|
23
|
-
done();
|
|
24
|
-
});
|
|
25
|
-
requestState.subject.next('hi');
|
|
26
|
-
requestState.ngOnDestroy();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should emit only one when strategy is cancel previous', (done) => {
|
|
30
|
-
const requestState = new MockHttpRequestState<string>({strategy: RequestStrategy.cancelPrevious});
|
|
31
|
-
requestState.request();
|
|
32
|
-
requestState.request();
|
|
33
|
-
requestState.selectResponse$.pipe( toArray()
|
|
34
|
-
).subscribe( d => {
|
|
35
|
-
expect(d.length).toBe(1);
|
|
36
|
-
done();
|
|
37
|
-
});
|
|
38
|
-
requestState.subject.next('hi');
|
|
39
|
-
requestState.ngOnDestroy();
|
|
40
|
-
|
|
41
|
-
});
|
|
42
|
-
});
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { Injector } from '@angular/core';
|
|
2
|
-
import { Observable } from 'rxjs';
|
|
3
|
-
export enum RequestStatus {
|
|
4
|
-
notStarted,
|
|
5
|
-
inProgress,
|
|
6
|
-
success,
|
|
7
|
-
fail,
|
|
8
|
-
cancelled,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type RequestStateCancelled = {
|
|
12
|
-
status: RequestStatus.cancelled
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type RequestStateNotStarted = {
|
|
16
|
-
status: RequestStatus.notStarted
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export type RequestStateInProgress = {
|
|
20
|
-
status: RequestStatus.inProgress
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export type RequestStateSuccess<T> = {
|
|
24
|
-
status: RequestStatus.success;
|
|
25
|
-
body: T;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type RequestStateError = {
|
|
29
|
-
status: RequestStatus.fail;
|
|
30
|
-
error: any;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
export type RequestState<T = any> =
|
|
35
|
-
RequestStateCancelled |
|
|
36
|
-
RequestStateNotStarted |
|
|
37
|
-
RequestStateInProgress |
|
|
38
|
-
RequestStateSuccess<T> |
|
|
39
|
-
RequestStateError ;
|
|
40
|
-
|
|
41
|
-
export enum RequestStrategy {
|
|
42
|
-
concurrent = 1,
|
|
43
|
-
singleUse = 2,
|
|
44
|
-
cancelPrevious = 3,
|
|
45
|
-
sequential = 4,
|
|
46
|
-
}
|
|
47
|
-
export interface RequestStateOptions<TParam extends any[] = any> {
|
|
48
|
-
strategy?: RequestStrategy;
|
|
49
|
-
autoRequest?: TParam extends never[] ? true : never;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface RequestCreatorOptions<TParam extends any[] = any> extends RequestStateOptions<TParam> { injector?: Injector }
|
|
53
|
-
|
|
54
|
-
export type RequestFactory<TParam extends any[], T> = (...params: [...TParam]) => Observable<T>
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { EnvironmentInjector, inject, Injector, makeEnvironmentProviders, provideAppInitializer, runInInjectionContext } from "@angular/core";
|
|
2
|
-
import { Action, createSelector, MemoizedSelector, Store } from "@ngrx/store";
|
|
3
|
-
|
|
4
|
-
let _store: Store<any>;
|
|
5
|
-
let _cache: AppStoreCache;
|
|
6
|
-
let _ei : EnvironmentInjector;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Creates a selector that can dispatch an action if conditions are met.
|
|
10
|
-
* Note: The props of the selector factory must include the props of the action.
|
|
11
|
-
* @param selectorFactory A method that returns selector.
|
|
12
|
-
* @param action The action that will be dispatched when conditions are met.
|
|
13
|
-
* @param [dispatchIf = defaultFilter] Optional. A method that takes the result of the selector and returns a boolean. The actions gets dispatched
|
|
14
|
-
* if true is returned. If no method is passed in than the action will be dispatched if the selector returns undefined or null.
|
|
15
|
-
*/
|
|
16
|
-
export const createActionableSelector = <State, Result, Props extends any[]>(
|
|
17
|
-
selectorFactory: ((...props: Props) => MemoizedSelector<State, Result>),
|
|
18
|
-
action: ActionReturner<Props>,
|
|
19
|
-
dispatchIf: (data: Result) => boolean = defaultFilter,
|
|
20
|
-
) => {
|
|
21
|
-
const options = {
|
|
22
|
-
selectorFactory,
|
|
23
|
-
action: (...props: Props) => () => _store.dispatch(action(...props)),
|
|
24
|
-
dispatchIf
|
|
25
|
-
}
|
|
26
|
-
const actionable = createActionResultSelector(options);
|
|
27
|
-
return (...props: Props) => createSelector(actionable(...props), b => b.result);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export const createActionSelector = <State, Result, Props extends any[] = any[]>(options: CreateActionResultsOptions<State, Result, Props>) => {
|
|
31
|
-
const actionable = createActionResultSelector(options);
|
|
32
|
-
return ((...props: Props) => createSelector(actionable(...props), b => b.result));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const createActionableResultSelector = <State, Result, Props extends any[]>(
|
|
36
|
-
selectorFactory: ((...props: Props) => MemoizedSelector<State, Result>),
|
|
37
|
-
action: ActionReturner<Props>,
|
|
38
|
-
dispatchIf: (data: Result) => boolean = defaultFilter,
|
|
39
|
-
) => {
|
|
40
|
-
const options: CreateActionResultsOptions<State, Result, Props> = {
|
|
41
|
-
selectorFactory,
|
|
42
|
-
action: (...props: Props) => () => _store.dispatch(action(...props)),
|
|
43
|
-
dispatchIf
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
return createActionResultSelector(options);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export const createActionResultSelector = <State, Result, Props extends any[] = any[]>(options: CreateActionResultsOptions<State, Result, Props>) => {
|
|
50
|
-
const id = crypto.randomUUID();
|
|
51
|
-
const slctr = (...props: Props) => createSelector(
|
|
52
|
-
options.selectorFactory(...props),
|
|
53
|
-
(selected: Result) => {
|
|
54
|
-
const needsDispatch = (options.dispatchIf || defaultFilter)(selected);
|
|
55
|
-
if(needsDispatch && !_cache.isInCache(id, ...props)){
|
|
56
|
-
queueMicrotask(() =>
|
|
57
|
-
runInInjectionContext(options.injector || _ei, () => {
|
|
58
|
-
options.action(...props)()
|
|
59
|
-
}));
|
|
60
|
-
}
|
|
61
|
-
return ({
|
|
62
|
-
loading: needsDispatch,
|
|
63
|
-
result: selected
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
) as ActionableMemoizedSelector<State, LoadingState<Result>>;
|
|
67
|
-
return slctr;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
class AppStoreCache {
|
|
71
|
-
cache: Cache = {};
|
|
72
|
-
isInCache = (actionableSelectorId: string, ...p: any[]): boolean => {
|
|
73
|
-
const props = p.reduce((a, b) => {
|
|
74
|
-
if(typeof b != 'object') b = { b };
|
|
75
|
-
return({ ...a, ...b })
|
|
76
|
-
}, {});
|
|
77
|
-
|
|
78
|
-
let itemsInCache = this.cache[actionableSelectorId] = (this.cache[actionableSelectorId] || []);
|
|
79
|
-
|
|
80
|
-
if(itemsInCache.some(itemInCache => shallowEquals(itemInCache, props))){
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
itemsInCache.push(props);
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export const clearActionableSelectorRequestCache = () => _cache.cache = {};
|
|
90
|
-
|
|
91
|
-
const shallowEquals = (cachedItem: object, newItem: object): boolean => {
|
|
92
|
-
const cachedItemKeys = Object.keys(cachedItem);
|
|
93
|
-
const secondKeys = Object.keys(newItem);
|
|
94
|
-
if(cachedItemKeys.length !== secondKeys.length) {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
for(let index = 0; index < cachedItemKeys.length; index++) {
|
|
98
|
-
const currentKey = cachedItemKeys[index];
|
|
99
|
-
if(cachedItem[currentKey] !== newItem[currentKey]){
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function defaultFilter(data: any) {
|
|
107
|
-
return data == null || data == undefined;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const setStore = (store: Store) => {
|
|
111
|
-
if(store instanceof Store) _store ??= store;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const setEnvironmentInjector = (ei: EnvironmentInjector) => {
|
|
115
|
-
if(ei instanceof EnvironmentInjector) _ei ??= ei;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
export function provideActionableSelector(){
|
|
119
|
-
return makeEnvironmentProviders([
|
|
120
|
-
provideAppInitializer(setUpStoreFactory)
|
|
121
|
-
])
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export const setUpStoreFactory = () => {
|
|
125
|
-
const store = inject(Store);
|
|
126
|
-
const env = inject(EnvironmentInjector);
|
|
127
|
-
_cache ??= new AppStoreCache();
|
|
128
|
-
setStore(store);
|
|
129
|
-
setEnvironmentInjector(env)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function setUpStoreFactoryOld(store: Store, env: EnvironmentInjector){
|
|
133
|
-
_cache ??= new AppStoreCache();
|
|
134
|
-
return () => new Promise((resolve, reject) => {
|
|
135
|
-
setStore(store);
|
|
136
|
-
setEnvironmentInjector(env)
|
|
137
|
-
resolve(null);
|
|
138
|
-
})
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
export interface CreateActionResultsOptions<State, Result, Props extends any[]>{
|
|
143
|
-
selectorFactory: (...props: Props) => MemoizedSelector<State, Result>,
|
|
144
|
-
action: (...props: Props) => () => void,
|
|
145
|
-
dispatchIf?: (data: Result) => boolean,
|
|
146
|
-
injector?: Injector
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
interface LoadingState<T> {
|
|
150
|
-
loading: boolean;
|
|
151
|
-
result?: T;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
type ActionableMemoizedSelector<State, Result> = MemoizedSelector<State, Result>;
|
|
155
|
-
|
|
156
|
-
export type ActionReturner<Props extends any[]> = (...props: Props) => Action;
|
|
157
|
-
|
|
158
|
-
type Cache = {
|
|
159
|
-
[key: string]: { [key: string]: any }[];
|
|
160
|
-
}
|
package/src/ngrx/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './actionable-selector';
|