@one-paragon/angular-utilities 2.4.4 → 2.4.5-beta.2
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.
|
@@ -522,9 +522,7 @@ function getDestroyRef(obsOrSubOrInjector, actionOrInjector, injector) {
|
|
|
522
522
|
class RequestStateStore {
|
|
523
523
|
constructor(req, options, project) {
|
|
524
524
|
this._state = signal({ requestParams: null, response: notStarted });
|
|
525
|
-
this.setState = (state) => this._state.set(state);
|
|
526
525
|
this.state = this._state.asReadonly();
|
|
527
|
-
this.state$ = toObservable(this.state);
|
|
528
526
|
this.injector = inject(Injector);
|
|
529
527
|
this.destroy$ = new Subject();
|
|
530
528
|
this.flatteningStrategy = () => {
|
|
@@ -537,7 +535,7 @@ class RequestStateStore {
|
|
|
537
535
|
};
|
|
538
536
|
this.createRequestPipeline = () => {
|
|
539
537
|
const origin$ = new Subject();
|
|
540
|
-
const sub = origin$.pipe(this.flatteningStrategy(), tap(state => this.
|
|
538
|
+
const sub = origin$.pipe(this.flatteningStrategy(), tap(state => this._state.set(state)), pipe(takeUntil(this.destroy$))).subscribe();
|
|
541
539
|
return (params) => {
|
|
542
540
|
origin$.next(params);
|
|
543
541
|
return sub;
|
|
@@ -554,24 +552,16 @@ class RequestStateStore {
|
|
|
554
552
|
return this.requestPipeLine(value);
|
|
555
553
|
};
|
|
556
554
|
this.$selectRequestState = computed(() => this.state().response);
|
|
557
|
-
this.selectRequestState$ = this.state$.pipe(map(s => s.response));
|
|
558
|
-
/**
|
|
559
|
-
* @deprecated use selectRequestState$ instead
|
|
560
|
-
*/
|
|
561
|
-
this.selectHttpState$ = this.selectRequestState$;
|
|
562
555
|
this.$selectStatus = computed(() => this.$selectRequestState()?.status);
|
|
563
|
-
this.selectStatus$ = this.selectRequestState$.pipe(map(s => s.status));
|
|
564
556
|
this.$isSuccess = computed(() => isSuccessState(this.$selectRequestState()));
|
|
565
557
|
this.$isError = computed(() => isErrorState(this.$selectRequestState()));
|
|
566
558
|
this.$isInProgress = computed(() => this.$selectStatus() === RequestStatus.inProgress);
|
|
567
559
|
this.$isNotStarted = computed(() => this.$selectStatus() === RequestStatus.notStarted);
|
|
568
|
-
this.selectError$ = this.selectRequestState$.pipe(filter(isErrorState), map(state => state.error));
|
|
569
560
|
this.$selectError = computed(() => {
|
|
570
561
|
const state = this.$selectRequestState();
|
|
571
562
|
if (isErrorState(state))
|
|
572
563
|
return state.error;
|
|
573
564
|
});
|
|
574
|
-
this.selectResponse$ = this.selectRequestState$.pipe(filter(isSuccessState), map(state => state.body));
|
|
575
565
|
this.$selectResponse = computed(() => {
|
|
576
566
|
const state = this.$selectRequestState();
|
|
577
567
|
if (isSuccessState(state)) {
|
|
@@ -579,7 +569,6 @@ class RequestStateStore {
|
|
|
579
569
|
}
|
|
580
570
|
return undefined;
|
|
581
571
|
});
|
|
582
|
-
this.selectSuccessOrError$ = this.state$.pipe(map(r => r.response), filter(isSuccessOrErrorState), map(() => undefined));
|
|
583
572
|
this.errorHandled = false;
|
|
584
573
|
this.#onDefaultErrorHandling = (e) => {
|
|
585
574
|
if (this.defaultErrorHandling)
|
|
@@ -631,6 +620,16 @@ class RequestStateStore {
|
|
|
631
620
|
}, { injector: this.injector });
|
|
632
621
|
return this;
|
|
633
622
|
};
|
|
623
|
+
this.state$ = toObservable(this.state);
|
|
624
|
+
this.selectRequestState$ = this.state$.pipe(map(s => s.response));
|
|
625
|
+
this.selectResponse$ = this.selectRequestState$.pipe(filter(isSuccessState), map(state => state.body));
|
|
626
|
+
this.selectSuccessOrError$ = this.state$.pipe(map(r => r.response), filter(isSuccessOrErrorState), map(() => undefined));
|
|
627
|
+
/**
|
|
628
|
+
* @deprecated use selectRequestState$ instead
|
|
629
|
+
*/
|
|
630
|
+
this.selectHttpState$ = this.selectRequestState$;
|
|
631
|
+
this.selectStatus$ = this.selectRequestState$.pipe(map(s => s.status));
|
|
632
|
+
this.selectError$ = this.selectRequestState$.pipe(filter(isErrorState), map(state => state.error));
|
|
634
633
|
this.assertInjectionContext();
|
|
635
634
|
this.injector.get(DestroyRef).onDestroy(() => this.destroy$.next());
|
|
636
635
|
this.project = project;
|
|
@@ -642,9 +641,9 @@ class RequestStateStore {
|
|
|
642
641
|
this._useDefaultErrorHandler = config.useDefaultErrorHandler;
|
|
643
642
|
this.defaultSuccessHandling = config.defaultSuccessHandling;
|
|
644
643
|
if (this._useDefaultErrorHandler) {
|
|
645
|
-
this.
|
|
644
|
+
this.effectOnState('error', e => {
|
|
646
645
|
if (!this.errorHandled) {
|
|
647
|
-
this.#onDefaultErrorHandling(e);
|
|
646
|
+
this.#onDefaultErrorHandling(e.error);
|
|
648
647
|
}
|
|
649
648
|
});
|
|
650
649
|
}
|
|
@@ -659,7 +658,17 @@ class RequestStateStore {
|
|
|
659
658
|
}
|
|
660
659
|
onError(cb) {
|
|
661
660
|
this.errorHandled = true;
|
|
662
|
-
this.
|
|
661
|
+
this.effectOnState('error', cb);
|
|
662
|
+
return this;
|
|
663
|
+
}
|
|
664
|
+
onErrorResponse(cb) {
|
|
665
|
+
this.errorHandled = true;
|
|
666
|
+
this.effectOnState('error', cb);
|
|
667
|
+
return this;
|
|
668
|
+
}
|
|
669
|
+
onErrorWithRequest(func) {
|
|
670
|
+
this.errorHandled = true;
|
|
671
|
+
this.effectOnState('error', (error, requestParams) => func({ requestParams, error }));
|
|
663
672
|
return this;
|
|
664
673
|
}
|
|
665
674
|
#onDefaultErrorHandling;
|
|
@@ -667,42 +676,54 @@ class RequestStateStore {
|
|
|
667
676
|
* if no handler was provided will call `console.error`
|
|
668
677
|
*/
|
|
669
678
|
useDefaultErrorHandler() {
|
|
670
|
-
this.
|
|
679
|
+
this.effectOnState('error', this.#onDefaultErrorHandling);
|
|
671
680
|
return this;
|
|
672
681
|
}
|
|
673
682
|
/**
|
|
674
683
|
* if no handler was provided will call `console.log` with 'Success'
|
|
675
684
|
*/
|
|
676
685
|
useDefaultSuccessHandler() {
|
|
677
|
-
this.
|
|
686
|
+
this.effectOnState('success', this.defaultSuccessHandling || (() => console.log('Success')));
|
|
678
687
|
return this;
|
|
679
688
|
}
|
|
680
689
|
onSuccess(cb) {
|
|
681
|
-
this.
|
|
690
|
+
this.effectOnState('success', cb);
|
|
682
691
|
return this;
|
|
683
692
|
}
|
|
684
|
-
|
|
685
|
-
this.
|
|
693
|
+
onSuccessResponse(cb) {
|
|
694
|
+
this.effectOnState('success', cb);
|
|
686
695
|
return this;
|
|
687
696
|
}
|
|
688
697
|
onSuccessWithRequest(func) {
|
|
689
|
-
this.
|
|
690
|
-
if (isSuccessState(response))
|
|
691
|
-
func({ requestParams, body: response.body });
|
|
692
|
-
});
|
|
698
|
+
this.effectOnState('success', (body, requestParams) => func({ requestParams, body }));
|
|
693
699
|
return this;
|
|
694
700
|
}
|
|
695
|
-
|
|
696
|
-
this.
|
|
697
|
-
this.subscriber(this.state$, ({ requestParams, response }) => {
|
|
698
|
-
if (isErrorState(response))
|
|
699
|
-
func({ requestParams, error: response.error });
|
|
700
|
-
});
|
|
701
|
+
onSuccessOrError(cb) {
|
|
702
|
+
this.effectOnState('both', cb);
|
|
701
703
|
return this;
|
|
702
704
|
}
|
|
703
705
|
createRequest(...params) {
|
|
704
706
|
return this.req(...params).pipe(map(re => createSuccess((this.project ? this.project(re) : re))), mapError(createFailure), startWith(inProgress), map(state => ({ requestParams: params, response: state })), defaultShareReplay());
|
|
705
707
|
}
|
|
708
|
+
effectOnState(onState, func) {
|
|
709
|
+
effect(() => {
|
|
710
|
+
const { requestParams, response } = this.state();
|
|
711
|
+
switch (onState) {
|
|
712
|
+
case 'error':
|
|
713
|
+
if (isErrorState(response))
|
|
714
|
+
untracked(() => func(response.error, requestParams));
|
|
715
|
+
break;
|
|
716
|
+
case 'success':
|
|
717
|
+
if (isSuccessState(response))
|
|
718
|
+
untracked(() => func(response.body, requestParams));
|
|
719
|
+
break;
|
|
720
|
+
case 'both':
|
|
721
|
+
if (isSuccessOrErrorState(response))
|
|
722
|
+
untracked(() => func());
|
|
723
|
+
break;
|
|
724
|
+
}
|
|
725
|
+
}, { injector: this.injector });
|
|
726
|
+
}
|
|
706
727
|
assertInjectionContext() {
|
|
707
728
|
try {
|
|
708
729
|
assertInInjectionContext(RequestStateStore);
|