Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
Rxjs/lodash throttle - how to use it on condition and in case that the condition may change while app runing?

I'm listening to the observable that may return true or false value - the only thing that I want to do is to set throttleTime for function call when it's true and don't have it when it's false. So I did some kind of workaround for that but I don't like this solution. I have tried a different approach where I tried to do it in the actions' effect but without success..

So this is the observable:

    this.store$
      .pipe(
        takeUntil(this.componentDestroyed$),
        select(selectGlobalsFiltered([
          GlobalPreferencesKeys.liveAircraftMovement])),
        distinctUntilChanged()
      )
      .subscribe((globals) => {
        if (globals && globals[GlobalPreferencesKeys.liveAircraftMovement] !== undefined) {
          this.isLiveMovementEnabled = (globals[GlobalPreferencesKeys.liveAircraftMovement] === 'true');
        }

        if (!this.isLiveMovementEnabled) {
          this.processPlaneData = throttle(this.processPlaneData, 4000);
        } else {
          this.processPlaneData = this.notThrottledFunction;
        }
      });

And as you can see I've created excat the same method that is 'pure' - notThrottledFunction and I'm assigning it when it's needed.

  processPlaneData(data: Airplane[]): void {
    this.store$.dispatch(addAllAirplanes({ airplanes: data }));
  }

  notThrottledFunction(data: Airplane[]): void {
    this.store$.dispatch(addAllAirplanes({ airplanes: data }));
  }

So basically this is working solution, but I'm pretty sure there is a better approach for doing such a things.

*throttle(this.processPlaneData, isLiveMovementEnabled ? 0 : 4000) doesn't work

So the second approch where I tried to do this inside of effect, I added a new argument for addAllAirplanes action - isLiveMovementEnabled: this.isLiveMovementEnabled

  addAllAirplanes$ = createEffect(() =>
    this.actions$.pipe(
      ofType(ActionTypes.ADD_ALL_AIRPLANES),
      map((data) => {
        if (data.isLiveMovementEnabled) {
          return addAllAirplanesSuccessWithThrottle(data);
        } else  {
          return addAllAirplanesSuccess(data);
        }
      }
    )
  );

And then I added another effect for addAllAirplanesSuccessWithThrottle

  addAllAirplanesThrottle$ = createEffect(() =>
    this.actions$.pipe(
      ofType(ActionTypes.ADD_ALL_AIRPLANES_THROTTLE),
      throttleTime(4000),
      map((data) => addAllAirplanesSuccess(data))
    )
  );

But it doesn't work. Can someone help me?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

(It's not clear how the data arrives in your example code, but I'll assume an Observable)

throttle and throttleTime are similar to your use case, but I think it makes sense to build your own custom implementation without them. I'd suggest managing the timing yourself, using Date to determine time deltas.

You've already cached the live filtering boolean in the component state, so we can just handle all of the data stream from your position update observable and filter them out manually (which is all throttle does, but it expects you to be able to feed it an interval at subscription time, and yours needs to be dynamic).

Setup component scoped variables to contain previous timestamps, as

private prevTime: number;
private intervalLimit: number = 4000;

Supposing data$ is your input plane position data stream:

data$.pipe(filter(data => {
  const now: number = Date.now();
  const diff = now - this.prevTime; 
  if (this.isLiveMovementEnabled) {
    // no throttle - pass every update, but prepare for disabling too
    // record when we last allowed an update & allow the update
    this.prevTime = now;
    return true;
  } else if (diff > intervalLimit) {
    // we are throttling results, but this one gets through!
    this.prevTime = now;
    return true;
  } else {
    // we're throttling, and we're in the throttle period. eat the result!
    return false;
  }
}

Something like that gives you full control over the logic used whenever data comes in. You can add other operations like takeUntil and distinctUntilChanges into the pipe and trust that when you subscribe you'll be getting updated when you want them.

You can even adjust the intervalLimit to dynamically adjust the throttle period on the fly.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!