@one-paragon/angular-utilities 2.4.3 → 2.4.5-beta.1
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,7 @@ class RequestStateStore {
|
|
|
659
658
|
}
|
|
660
659
|
onError(cb) {
|
|
661
660
|
this.errorHandled = true;
|
|
662
|
-
this.
|
|
661
|
+
this.effectOnState('error', cb);
|
|
663
662
|
return this;
|
|
664
663
|
}
|
|
665
664
|
#onDefaultErrorHandling;
|
|
@@ -667,42 +666,55 @@ class RequestStateStore {
|
|
|
667
666
|
* if no handler was provided will call `console.error`
|
|
668
667
|
*/
|
|
669
668
|
useDefaultErrorHandler() {
|
|
670
|
-
this.
|
|
669
|
+
this.effectOnState('error', this.#onDefaultErrorHandling);
|
|
671
670
|
return this;
|
|
672
671
|
}
|
|
673
672
|
/**
|
|
674
673
|
* if no handler was provided will call `console.log` with 'Success'
|
|
675
674
|
*/
|
|
676
675
|
useDefaultSuccessHandler() {
|
|
677
|
-
this.
|
|
676
|
+
this.effectOnState('success', this.defaultSuccessHandling || (() => console.log('Success')));
|
|
678
677
|
return this;
|
|
679
678
|
}
|
|
680
679
|
onSuccess(cb) {
|
|
681
|
-
this.
|
|
680
|
+
this.effectOnState('success', cb);
|
|
682
681
|
return this;
|
|
683
682
|
}
|
|
684
683
|
onSuccessOrError(cb) {
|
|
685
|
-
this.
|
|
684
|
+
this.effectOnState('both', cb);
|
|
686
685
|
return this;
|
|
687
686
|
}
|
|
688
687
|
onSuccessWithRequest(func) {
|
|
689
|
-
this.
|
|
690
|
-
if (isSuccessState(response))
|
|
691
|
-
func({ requestParams, body: response.body });
|
|
692
|
-
});
|
|
688
|
+
this.effectOnState('success', (body, requestParams) => func({ requestParams, body }));
|
|
693
689
|
return this;
|
|
694
690
|
}
|
|
695
691
|
onErrorWithRequest(func) {
|
|
696
692
|
this.errorHandled = true;
|
|
697
|
-
this.
|
|
698
|
-
if (isErrorState(response))
|
|
699
|
-
func({ requestParams, error: response.error });
|
|
700
|
-
});
|
|
693
|
+
this.effectOnState('error', (error, requestParams) => func({ requestParams, error }));
|
|
701
694
|
return this;
|
|
702
695
|
}
|
|
703
696
|
createRequest(...params) {
|
|
704
697
|
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
698
|
}
|
|
699
|
+
effectOnState(onState, func) {
|
|
700
|
+
effect(() => {
|
|
701
|
+
const { requestParams, response } = this.state();
|
|
702
|
+
switch (onState) {
|
|
703
|
+
case 'error':
|
|
704
|
+
if (isErrorState(response))
|
|
705
|
+
untracked(() => func(response.error, requestParams));
|
|
706
|
+
break;
|
|
707
|
+
case 'success':
|
|
708
|
+
if (isSuccessState(response))
|
|
709
|
+
untracked(() => func(response.body, requestParams));
|
|
710
|
+
break;
|
|
711
|
+
case 'both':
|
|
712
|
+
if (isSuccessOrErrorState(response))
|
|
713
|
+
untracked(() => func());
|
|
714
|
+
break;
|
|
715
|
+
}
|
|
716
|
+
}, { injector: this.injector });
|
|
717
|
+
}
|
|
706
718
|
assertInjectionContext() {
|
|
707
719
|
try {
|
|
708
720
|
assertInInjectionContext(RequestStateStore);
|
|
@@ -4409,7 +4421,11 @@ function updateGroupByState(groupedData, { data, groups, expanded, sorts }, firs
|
|
|
4409
4421
|
if (firstRun
|
|
4410
4422
|
|| dataUpdated(data, groups, expanded, sorts)
|
|
4411
4423
|
|| groupsUpdated(groups, expanded, sorts)) {
|
|
4412
|
-
|
|
4424
|
+
const combinedGroupsAndSorts = groups.value.map(g => {
|
|
4425
|
+
const sortForGroup = sorts.value.find(s => s.key === g.key);
|
|
4426
|
+
return { ...g, sort: sortForGroup?.sort || '' };
|
|
4427
|
+
});
|
|
4428
|
+
groupedData = groups.value.length ? getGroupedData(data.value, combinedGroupsAndSorts, metaData) : data.value;
|
|
4413
4429
|
}
|
|
4414
4430
|
else {
|
|
4415
4431
|
let curr = [...sorts.value];
|