-
Notifications
You must be signed in to change notification settings - Fork 96
WIP: refactor(Effects): Remove runEffects and StateUpdates, add NgModule #32
Conversation
LGTM 👍 |
Yeah, looks good to me. What I would like to see in README:
as a side note:
|
I think this is good. The previous implementation was a bit too much.
It should be easy enough for users to reproduce the previous experience: @Injectable()
export class BaseEffects implements OnDestroy {
subscription: Subscription;
constructor(private actions: Actions, store: Store<T>) {
this.subscription = mergeEffects(this).subscribe(store);
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
@Injectable()
export class AuthEffects extends BaseEffects {
@Effect() login$ = this.actions$
.ofType('login')
.mergeMap(...)
} The downside with this is when we need to inject more providers in the constructor. What about creating an export interface OnSubscribe<T>() {
onSubscribe(actions: Actions, store: Store<T>): void;
}
@Injectable()
export class BaseEffects implements OnSubscribe, OnDestroy {
actions$: Actions;
subscription: Subscription;
onSubscribe(actions: Actions, store: Store<T>): void {
this.actions$ = actions; // or inject in constructor
this.subscription = mergeEffects(this).subscribe(store);
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
@Injectable()
export class AuthEffects extends BaseEffects {
@Effect() login$ = this.actions$
.ofType('login')
.mergeMap(...)
} Yet another option to not rely on constructor in base class for acquiring the @Effect() login$ = actions$ => actions$
.ofType('login')
.mergeMap(...) WDYT? |
I'd prefer configuring the decorator: @Effect(/* dispatch: boolean = true */ false) login$ = actions$ => actions$
.ofType('login')
.do(...) |
☝️ that looks reasonable. |
Recreating the updates stream is pretty straightforward and makes it more clear how to apply selectors in effects: actions$
.ofType('login')
.withLatestFrom(store$)
.map(([ action, state ]) => ({ action, state }))
Yes, as of today if you want your service to be automatically instantiated it must implement the
I don't think this is technically possible yet.
Yeah, configuring the decorator like that seems like a reasonable improvement of the API. I think I'd prefer it this way to leave room for future configuration options:
I'm following what they are doing in redux-observable. See redux-observable/redux-observable#95 |
yes, and it's gonna be a source of confusion nevertheless, that's why it better be mentioned in readme :) |
import { Injectable, Inject } from '@angular/core'; | ||
import { Action, Dispatcher } from '@ngrx/store'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import { Operator } from 'rxjs/Operator'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't seem to be needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't fully implemented the typings for the lift
method and importing the Operator
interface will be required to do so. See: https://github.com/ngrx/store/blob/master/src/store.ts#L25
I've tried @Effect() redirectAfterLogin$ = this.actions$
.ofType(AuthActions.REDIRECT_AFTER_LOGIN)
.withLatestFrom(this.store.let(appSelectors.getAuthRedirectUrl()))
.do(([action, url]) => router.navigateByUrl.next(url))
.mapTo(this.authActions.resetRedirectAfterLogin())
Great work! |
Due to the new version of Effects, I tried to see if I could inject the Any thoughts on this? |
@meenie can you make a plunker with this issue? |
Problems with this proposed API:
I'll try to address these issues with a new API sometime early this week. |
I really like this approach ☝️ 👍 |
Is there any way to unit test an effect? I want to find a way to send an action to my effect. In the current version there is the sendAction function. |
AKA the release where I delete most of this library.
In @ngrx/effects v1, providers were created dynamically via
runEffects(...effects: Type | Type[])
. However, the providers generated this way were not statically analyzable and so they would not work with the offline compiler.I'm looking for comments on the new, offline compiler friendly API:
Note: services in RC5 are automatically instantiated if they implement the
OnDestroy
interface. This has the added benefit of automatically starting / stopping effects dynamically, like during routing.This has been published with the @beta tag. Get it with
npm install @ngrx/effects@beta
Also in this release is the new release structure outlined in ngrx/core#4
cc @fxck @btroncone @robwormald