Reputation: 33
I have an endpoint which receives an array of ids to do some processing.
The backend dev, asked me to send only 10 ids during rushhour, and 50 outside them.
The user enters in a field, all the ids they want to process (top 100).
What I need to do is, first, know how much ids send to the back, regarding the current time.
I haven't chunked the ids before hand, because I wanted to take a number of ids related to the condition mentioned before, so I was trying to repeat the whole obs until there are none ids to process. There is another challenged, which is that it must have a delay of 1 min between each.
concatMap(() =>
of(null).pipe(
map(() => this.isRushHour()),
map(isRushHour => this.storeListToProccess.splice(0, isRushHour ? 10 : 50)),
switchMap(p => this.createZones(p)),
delayWhen(() => this.storeListToProccess.length ? interval(this.delayMs) : interval(0)),
repeatWhen(()=> !!this.storeListToProccess.length)
)
)
Is there any way to achieve this or is prechunking the ids the only way to do it?
Upvotes: 1
Views: 234
Reputation: 33
I found a solution, (two, in fact). I will post the one using RXjs:
interval(1000)
.pipe(
takeWhile(()=> storeListToProccess.length>0),
map(()=>storeListToProccess.splice(0, isRushHour() ? 1 : 2)),
tap((IdsToSend)=> console.log(IdsToSend)),
finalize(()=>console.log("Finished"))
)
.subscribe()
Upvotes: 0
Reputation: 12001
I would resolve you task in this way:
from(this.storeListToProccess).pipe(
bufferCount(this.isRushHour() ? 10 : 50), // here we chunkify the whole array into groups of 10 or 50 elements
concatMap((p) => merge(this.createZones(p), timer(this.delayMs)))
// concatMap () => timer will make sure that no new request will be send, untill timer is complete
)
and that is basically it, if you don't need handle request results. If you do need to handle it, more information on the question is required.
Upvotes: 1