Guest User

Untitled

a guest
Feb 19th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. console.clear()
  2. async function main() {
  3.  
  4. const delay = time=>new Promise(resolve => setTimeout(()=>resolve(), time))
  5. const functionInvocationsObservable = new Rx.Subject()
  6. .bufferTime(/* buffer time*/ 500, /* restart buffer, -1 means never */ -1, /* maxBufferSize*/ 3)
  7. .filter(x => x.length > 0)
  8. // .subscribe(console.log)
  9.  
  10. const batchify = func => {
  11. functionInvocationsObservable.subscribe(x => {
  12. console.log(x)
  13. func(x)
  14. })
  15.  
  16. return function() {
  17. functionInvocationsObservable.next(...arguments)
  18. }
  19. }
  20.  
  21. const foo = batchify(x=>x * 2)
  22.  
  23. for (let i = 0; i <= 10; i++) {
  24. await delay(100)
  25. console.info(`invoked with: ${i}`)
  26. foo(i)
  27. }
  28.  
  29. await delay(100)
  30. console.info(`invoked with: ${28}`)
  31. foo(28)
  32. foo(30)
  33. foo(42)
  34.  
  35. }
  36.  
  37. main()
Add Comment
Please, Sign In to add comment