-
-
Notifications
You must be signed in to change notification settings - Fork 204
added logging to the hold method #678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c26f2b2
056bfc0
5aab5ef
39404eb
8c3a9c1
be81e89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,8 +1,32 @@ | ||||||||||
import { Injectable, OnDestroy } from '@angular/core'; | ||||||||||
import { EMPTY, isObservable, Observable, OperatorFunction, Subscribable, Subscription, Unsubscribable } from 'rxjs'; | ||||||||||
import { | ||||||||||
ErrorHandler, | ||||||||||
inject, | ||||||||||
Injectable, | ||||||||||
InjectFlags, | ||||||||||
OnDestroy, | ||||||||||
} from '@angular/core'; | ||||||||||
import { | ||||||||||
EMPTY, | ||||||||||
isObservable, | ||||||||||
Observable, | ||||||||||
OperatorFunction, | ||||||||||
Subscribable, | ||||||||||
Subscription, | ||||||||||
Unsubscribable, | ||||||||||
} from 'rxjs'; | ||||||||||
import { catchError, map, pluck, tap } from 'rxjs/operators'; | ||||||||||
import { isKeyOf, isOperateFnArrayGuard, isStringArrayGuard, pipeFromArray, safePluck } from './core'; | ||||||||||
import { AccumulationFn, createAccumulationObservable, createSideEffectObservable } from './cdk'; | ||||||||||
import { | ||||||||||
isKeyOf, | ||||||||||
isOperateFnArrayGuard, | ||||||||||
isStringArrayGuard, | ||||||||||
pipeFromArray, | ||||||||||
safePluck, | ||||||||||
} from './core'; | ||||||||||
import { | ||||||||||
AccumulationFn, | ||||||||||
createAccumulationObservable, | ||||||||||
createSideEffectObservable, | ||||||||||
} from './cdk'; | ||||||||||
import { stateful } from './rxjs/operators'; | ||||||||||
|
||||||||||
type ProjectStateFn<T> = (oldState: T) => Partial<T>; | ||||||||||
|
@@ -120,30 +144,38 @@ export class RxState<T extends object> implements OnDestroy, Subscribable<T> { | |||||||||
k3: K3 | ||||||||||
): T[K1][K2][K3]; | ||||||||||
/** @internal **/ | ||||||||||
get<K1 extends keyof T, | ||||||||||
get< | ||||||||||
K1 extends keyof T, | ||||||||||
K2 extends keyof T[K1], | ||||||||||
K3 extends keyof T[K1][K2], | ||||||||||
K4 extends keyof T[K1][K2][K3]>(k1: K1, k2: K2, k3: K3, k4: K4): T[K1][K2][K3][K4]; | ||||||||||
K4 extends keyof T[K1][K2][K3] | ||||||||||
>(k1: K1, k2: K2, k3: K3, k4: K4): T[K1][K2][K3][K4]; | ||||||||||
/** @internal **/ | ||||||||||
get<K1 extends keyof T, | ||||||||||
get< | ||||||||||
K1 extends keyof T, | ||||||||||
K2 extends keyof T[K1], | ||||||||||
K3 extends keyof T[K1][K2], | ||||||||||
K4 extends keyof T[K1][K2][K3], | ||||||||||
K5 extends keyof T[K1][K2][K3][K4]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): T[K1][K2][K3][K4][K5]; | ||||||||||
K5 extends keyof T[K1][K2][K3][K4] | ||||||||||
>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): T[K1][K2][K3][K4][K5]; | ||||||||||
/** @internal **/ | ||||||||||
get<K1 extends keyof T, | ||||||||||
get< | ||||||||||
K1 extends keyof T, | ||||||||||
K2 extends keyof T[K1], | ||||||||||
K3 extends keyof T[K1][K2], | ||||||||||
K4 extends keyof T[K1][K2][K3], | ||||||||||
K5 extends keyof T[K1][K2][K3][K4], | ||||||||||
K6 extends keyof T[K1][K2][K3][K4][K5]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): T[K1][K2][K3][K4][K5][K6]; | ||||||||||
K6 extends keyof T[K1][K2][K3][K4][K5] | ||||||||||
>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): T[K1][K2][K3][K4][K5][K6]; | ||||||||||
/** @internal **/ | ||||||||||
get<K1 extends keyof T, | ||||||||||
get< | ||||||||||
K1 extends keyof T, | ||||||||||
K2 extends keyof T[K1], | ||||||||||
K3 extends keyof T[K1][K2], | ||||||||||
K4 extends keyof T[K1][K2][K3], | ||||||||||
K5 extends keyof T[K1][K2][K3][K4], | ||||||||||
K6 extends keyof T[K1][K2][K3][K4][K5]>( | ||||||||||
K6 extends keyof T[K1][K2][K3][K4][K5] | ||||||||||
>( | ||||||||||
...keys: | ||||||||||
| [K1] | ||||||||||
| [K1, K2] | ||||||||||
|
@@ -163,9 +195,9 @@ export class RxState<T extends object> implements OnDestroy, Subscribable<T> { | |||||||||
if (!!keys && keys.length) { | ||||||||||
return safePluck(this.accumulator.state, keys); | ||||||||||
} else { | ||||||||||
return hasStateAnyKeys ? | ||||||||||
this.accumulator.state : | ||||||||||
undefined as unknown as T; | ||||||||||
return hasStateAnyKeys | ||||||||||
? this.accumulator.state | ||||||||||
: ((undefined as unknown) as T); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
|
@@ -472,33 +504,41 @@ export class RxState<T extends object> implements OnDestroy, Subscribable<T> { | |||||||||
/** | ||||||||||
* @internal | ||||||||||
*/ | ||||||||||
select<K1 extends keyof T, | ||||||||||
select< | ||||||||||
K1 extends keyof T, | ||||||||||
K2 extends keyof T[K1], | ||||||||||
K3 extends keyof T[K1][K2]>(k1: K1, k2: K2, k3: K3): Observable<T[K1][K2][K3]>; | ||||||||||
K3 extends keyof T[K1][K2] | ||||||||||
>(k1: K1, k2: K2, k3: K3): Observable<T[K1][K2][K3]>; | ||||||||||
/** | ||||||||||
* @internal | ||||||||||
*/ | ||||||||||
select<K1 extends keyof T, | ||||||||||
select< | ||||||||||
K1 extends keyof T, | ||||||||||
K2 extends keyof T[K1], | ||||||||||
K3 extends keyof T[K1][K2], | ||||||||||
K4 extends keyof T[K1][K2][K3]>(k1: K1, k2: K2, k3: K3, k4: K4): Observable<T[K1][K2][K3][K4]>; | ||||||||||
K4 extends keyof T[K1][K2][K3] | ||||||||||
>(k1: K1, k2: K2, k3: K3, k4: K4): Observable<T[K1][K2][K3][K4]>; | ||||||||||
/** | ||||||||||
* @internal | ||||||||||
*/ | ||||||||||
select<K1 extends keyof T, | ||||||||||
select< | ||||||||||
K1 extends keyof T, | ||||||||||
K2 extends keyof T[K1], | ||||||||||
K3 extends keyof T[K1][K2], | ||||||||||
K4 extends keyof T[K1][K2][K3], | ||||||||||
K5 extends keyof T[K1][K2][K3][K4]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): Observable<T[K1][K2][K3][K4][K5]>; | ||||||||||
K5 extends keyof T[K1][K2][K3][K4] | ||||||||||
>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): Observable<T[K1][K2][K3][K4][K5]>; | ||||||||||
/** | ||||||||||
* @internal | ||||||||||
*/ | ||||||||||
select<K1 extends keyof T, | ||||||||||
select< | ||||||||||
K1 extends keyof T, | ||||||||||
K2 extends keyof T[K1], | ||||||||||
K3 extends keyof T[K1][K2], | ||||||||||
K4 extends keyof T[K1][K2][K3], | ||||||||||
K5 extends keyof T[K1][K2][K3][K4], | ||||||||||
K6 extends keyof T[K1][K2][K3][K4][K5]>( | ||||||||||
K6 extends keyof T[K1][K2][K3][K4][K5] | ||||||||||
>( | ||||||||||
k1: K1, | ||||||||||
k2: K2, | ||||||||||
k3: K3, | ||||||||||
|
@@ -547,7 +587,14 @@ export class RxState<T extends object> implements OnDestroy, Subscribable<T> { | |||||||||
obsOrObsWithSideEffect: Observable<S>, | ||||||||||
sideEffectFn?: (arg: S) => void | ||||||||||
): void { | ||||||||||
const sideEffect = obsOrObsWithSideEffect.pipe(catchError(e => EMPTY)) | ||||||||||
const sideEffect = obsOrObsWithSideEffect.pipe( | ||||||||||
catchError((e) => { | ||||||||||
// used injector for compatibility with https://github.com/rx-angular/rx-angular/blob/master/libs/state/docs/usage.md#inherit | ||||||||||
const errorHandler = inject(ErrorHandler, InjectFlags.Optional); | ||||||||||
d-stolyar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
errorHandler?.handleError(e); | ||||||||||
Comment on lines
+593
to
+594
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last detail, why considering the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. He has probably added it because it throws Your recommendation with this one is correct: constructor(private errorHandler: ErrorHandler) {} Since the stable version is not released yet I think it's ok to have breaking changes. We can have a migration script for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can create a migration script, but honestly, this looks bad: constructor(private errorHandler: ErrorHandler) {
super(this.errorHandler);
} I would prefer keeping the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just checked that code and it seems like it's working only inside tests for now because the injector is explicitly set there. E.g. this doesn't work even with inject flags: @Component({
selector: 'app-root',
template: '<button (click)="onClick()">Click me</button>',
})
export class AppComponent {
onClick(): void {
console.log(inject(ErrorHandler, InjectFlags.Optional));
}
} That would respect inject flags if there's any injector on the stack: function injectInjectorOnly(token, flags = InjectFlags.Default) {
if (_currentInjector === undefined) {
throw new Error(`inject() must be called from an injection context`);
}
}
function ɵɵinject(token, flags = InjectFlags.Default) {
// The `getInjectImplementation` returns `undefined` here
return (getInjectImplementation() || injectInjectorOnly)(resolveForwardRef(token), flags);
} I'm wondering if it can be moved to the constructor but it also has to be wrapped into try-catch to prevent the error if there is no current injector: export class ... {
private errorHandler: ErrorHandler | null;
constructor() {
try {
this.errorHandler = inject(ErrorHandler);
} catch {
this.errorHandler = null;
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
this sounds like the only valid solution for me since the approach with extending from RxState is pretty common. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about this? @hoebbelsB class ... {
private errorHandler: ErrorHandler
constructor() {
try {
this.errorHandler = inject(ErrorHandler)
} catch {
const injector = Injector.create({
providers: [{ provide: ErrorHandler, deps: [] }]
});
this.errorHandler = injector.get(ErrorHandler);
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will create There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To conclude, we should try this approach and decide: export class ... {
private errorHandler: ErrorHandler | null;
constructor() {
try {
this.errorHandler = inject(ErrorHandler);
} catch {
this.errorHandler = null;
}
} |
||||||||||
return EMPTY; | ||||||||||
}) | ||||||||||
); | ||||||||||
if (typeof sideEffectFn === 'function') { | ||||||||||
this.effectObservable.nextEffectObservable( | ||||||||||
sideEffect.pipe(tap(sideEffectFn)) | ||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.