by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 09:52
    voliva commented #5545
  • 09:52
    voliva commented #5545
  • 09:12
    evertbouw opened #5584
  • 08:40
    rraziel synchronize #5363
  • 08:40
    rraziel synchronize #5363
  • 07:55
    william-lohan opened #5583
  • 01:13
    cartant commented #5363
  • 01:12
    cartant commented #5363
  • 01:11
    cartant commented #5363
  • 01:08
    rraziel commented #5363
  • 01:05
    rraziel synchronize #5363
  • 01:01
    rraziel synchronize #5363
  • 00:55
    rraziel synchronize #5363
  • Jul 09 22:35
    josepot commented #5556
  • Jul 09 22:02
    benlesh review_requested #5363
  • Jul 09 22:02
    benlesh labeled #5363
  • Jul 09 13:12
    josepot commented #5556
  • Jul 09 12:43
    ladyleet commented #5363
  • Jul 09 12:41
    josepot commented #5556
  • Jul 09 12:30
    josepot edited #5556
RoboZoom
@RoboZoom
But I'd want to have a very good understanding why it's appearing, and if you're ok accepting that when it apperas
Henri Maurer
@hmaurer
Hi! Quick question: is there something akin to BehaviourSubject in the sense that you can get the "latest" value synchronously, but without an "emit" method? So just an observable + latest value?
Gatej Andrei
@Andrei0872
Hello, @hmaurer! what do you mean by emit method?
Henri Maurer
@hmaurer
@Andrei0872 sorry, I got it wrong. I meant the next method. My use-case is to give a consumer something that is observable and has a "current value", but I don't really want to give them the ability to push things to that observable, if that makes sense
Gatej Andrei
@Andrei0872

@hmaurer makes sense, have you considered something like

const o$ = privateSrc.pipe(
 // `shareReplay()` might work as well
 publishReplay(1),
 refCount(),
)

o$ can be given to the consumer and it would receive the latest stored value syncrhronously, but it won't be able to push any values, since privateSrc is responsible for that

Dorus
@Dorus
@Andrei0872 It wont be sync if the replaysubject has no values.
@hmaurer Why not use a BehaviourSubject but share around the subject.asObservable()?
Gatej Andrei
@Andrei0872
@Dorus you're right, maybe publishBehavior(defaultValue) would solve this problem
Henri Maurer
@hmaurer
@Dorus would sharing that still gives the consumer access to the current value (sync)?
Perhaps I should cut the X-Y problem: my use-case is modelling a state machine. A state machine has a current/initial state (synchronous), and then after that is a stream of states. But someone observing the state machine shouldn't be able to push states to it! It should only be able to interact with it by emitting events (in a separate stream)
Derek
@derekkite
@hmaurer what ngrx does is use a behavior subject for state, then to access the state you use a selector https://github.com/ngrx/platform/blob/master/modules/store/src/state.ts
Henri Maurer
@hmaurer
@derekkite ah, thanks!
@derekkite I don't know platform/angular but what you linked me looks like a state machine,, right? You've an actions$ observable, and a state$ observable?
Derek
@derekkite
@hmaurer yes. ngrx is similar to redux, but built around observables.
Henri Maurer
@hmaurer
@derekkite cool, thanks! Out of curiosity does ngrx handle effects? e.g. sending an HTTP request on some action? And perhaps cancelling it if another action comes in?
Derek
@derekkite
yes.
Henri Maurer
@hmaurer
@derekkite could you point me to a doc on that? It coudl be really useful as inspiration from what I am doing
Derek
@derekkite
ngrx effects are a subscription to the action stream, you filter for the desired action and do something asynchronous
ngrx.io is the documentation site.
Henri Maurer
@hmaurer
I see, yeah I'm looking at https://ngrx.io/guide/effects
and is there a way to cancel effects if another action comes in?
Derek
@derekkite
switchMap
Henri Maurer
@hmaurer
Cool. I really need to learn those operators :P Thanks
How good is NGRX? Is it generally well liked in the Angular community? Are there a lot of complaints / pitfalls? @derekkite
Also, one other question (sorry...): can effects trigger more actions? e.g. here https://ngrx.io/guide/effects#writing-effects , does the mergeMap pushes new actions that might affect the state?
RoboZoom
@RoboZoom
Effects by default return an action
Henri Maurer
@hmaurer
@RoboZoom how are they wired? In that example an effect is assigned to a property on the class, but I don't see how that's then wired to publish an action?
RoboZoom
@RoboZoom
It might be easy to think of an effect as tributary of the actions stream
Henri Maurer
@hmaurer
@RoboZoom But then how do they themselves trigger more actions?
RoboZoom
@RoboZoom

In this case:

loadMovies$ = createEffect(() => this.actions$.pipe(
    ofType('[Movies Page] Load Movies'),
    mergeMap(() => this.moviesService.getAll()
      .pipe(
        map(movies => ({ type: '[Movies API] Movies Loaded Success', payload: movies })),
        catchError(() => EMPTY)
      ))
    )
  );

You start with a stream of actions... and then the stream is filtered, and you map the payload to a new action - in this case in this line:

map(movies => ({ type: '[Movies API] Movies Loaded Success', payload: movies })),
The output of your effect is then re entered into the action stream
Henri Maurer
@hmaurer
@RoboZoom Right so that's my question: "The output of your effect is then re entered into the action stream" => how is it re-entered? Where/how does that happen?
Sorry if I'm being slow
RoboZoom
@RoboZoom
I should say the piped output is redispatched into the actions stream, then everything monitoring the actions stream will act accordingly
the createEffect() method is a factory function, so the actual object underneath handles it
Let me see if I can find it
Henri Maurer
@hmaurer
@RoboZoom I'm asking all those questions because I am not using Angular. I am writing a library for a React project of mine that could probably use a lot of inspiration from NGRX
So I can't use NGRX directly, but I want to steal what it does well :P
RoboZoom
@RoboZoom
:)
if that helps
RoboZoom
@RoboZoom
I don't know the wiring of the library well enough to not fear misleading you, so hopefully this can help you unpack the guts
Henri Maurer
@hmaurer
@RoboZoom no worries; thanks a lot for helping! So roughly speaking it subscribes to the effect observable (which at that point is an observable of actions), and emits those actions into the actions subject?
Is that a fair understanding?
RoboZoom
@RoboZoom
@hmaurer - sorry for the delay - yes, your description matches my understanding
Henri Maurer
@hmaurer
@RoboZoom Cool, thank you!
Dorus
@Dorus
@hmaurer yes if you share the behaviorSubject with .asObservable() it will share a observable where you can get the first value from sync by subscribing, and any next value by staying subscribed.
However since it's only an observable the subjects observer will be hidden so the user cannot next to it.
Also behaviorSubject has an getValue() method, so you could also expose a call that returns the result of getValue() on the behavior subject instead.
Derek
@derekkite
@hmaurer https://github.com/ngrx/platform/blob/3f2bea4f9ca74d442ec1627467cf5533ab8e0c98/modules/effects/src/effect_sources.ts this is where the effects are run and the results merged into the action stream