LeetCode-OpenSource/ayanami

How to avoid infinit self calls

stkevintan opened this issue · 3 comments

if we invoke else service method suck as this.getActions() inside the effectActions[actionName] function in sync way, it will cause an error:

InternalError: "too much recursion"

the effect can be:

  @Effect()
  fetchInfo(url$: Observable<string>):Observable<EffectAction> {
    return url$.pipe(
      switchMap(url => from(client.get(url))),
      mergeMap(data => of(this.getActions().setData(data), this.getActions().setLoading(false))),
      startWith(this.getActions().setLoading(true)),
    )
  }

the effect init function is :

const effect$: Observable<EffectAction> = effectActions[actionName](payload$, state$)

because the startWith is sync, so this.getActions() will request the ikari instance, but the ikari instance is not fully constructed at that time. so it will invoke the create function again and agian...

I try to fix it by rewrite some souce code as follow, but I dont known if it has some bad side effects or not

from:

actions[actionName] = (payload: any) => payload$.next(payload)

to:

   const payload$ = new BehaviorSubject<any>(undefined)
    actions[actionName] = (payload: any) => {
      payload$.next(payload)
    }
    const effect$: Observable<EffectAction> = from(Promise.resolve()).pipe(
      switchMap(() => effectActions[actionName](payload$, state$))
    )
@Effect()
  fetchInfo(url$: Observable<string>):Observable<EffectAction> {
    return url$.pipe(
      switchMap(url => from(client.get(url))),
      mergeMap(data => of(this.getActions().setData(data), this.getActions().setLoading(false))),
-     startWith(this.getActions().setLoading(true)),
    )
  }

I think that the issue is caused by the startWith operator. How about:

@Effect()
  fetchInfo(url$: Observable<string>):Observable<EffectAction> {
    return url$.pipe(
      switchMap(url => from(client.get(url))),
      mergeMap(data => of(
        this.getActions().setLoading(true),
        this.getActions().setData(data),
        this.getActions().setLoading(false)
      )),
    )
  }

Maybe we should show some tips about misuse the startWith operator? 🤔️

@runjuu
I think we should tell user not use this in sync context include startWith, such as :

@Effect()
fetchInfo(url$: Observable<string>):Observable<EffectAction> {
    this.getState()    //  Error 
    this.getState$()  //  Error
    this.getActions() //  Error
    return url$.pipe(
      switchMap(url => from(client.get(url))),
      mergeMap(data => of(
        this.getActions().setLoading(true),
        this.getActions().setData(data),
        this.getActions().setLoading(false)
      )),
    )
  }

or we can do something avoid this error ?