javascript - RxJS: Send x requests per second -


i have function createevent() sends request google calendar.

google calendar's api requires me send @ max 5 requests per second.

if call createevent() 100 times flood google calendar , requests denied. if possible createevent() contain logic required throttle requests 5 per second.

i'm trying avoid,

calendar.addeventtoqueue(eventdata); calendar.addeventtoqueue(eventdata); calendar.addeventtoqueue(eventdata); cleandar.submitevents(); 

and instead

calendar.createevent(eventdata); calendar.createevent(eventdata); calendar.createevent(eventdata); 

i think gave answer while on rate limiting.

you can rxjs:

//this replaced whatever event source //i made button click in case var source = rx.observable.fromevent($button, 'click')    //captures either events 1 second max of 5 events   //projects each set observable   .windowwithtimeorcount(1000, 5)    //only first window in single second gets propagated   //the others dropped   .throttlefirst(1000)    //flatten sequence concatenating windows   .concatall()    //build event body *after* have done filtering   .flatmap(function() {      //the calendar request api supports promises      //handled implicitly in rx operators flatmap. result     //this automatically wait until receives response.     return gapi.client.calendar.events.insert(/*event body*/);   });  //process response source.subscribe(   function(response) { console.log('event created!'); }); 

Comments