-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
25 lines (20 loc) · 1.03 KB
/
index.js
File metadata and controls
25 lines (20 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import xs from 'xstream';
import concurrent from 'concurrent/concurrent'
import delay from 'xstream/extra/delay'
// Creates a stream of 15 sequentially numbered "requests" [0, 1, 2, 3...]
let requests = [...Array(15).keys()];
const requests$ = xs.fromArray(requests);
// Creates a responses proxy that we can pass to concurrent
// This enables our circular dependency of passing the completed responses to
// the request limiter
const responsesProxy$ = xs.create();
// Creates the limited stream of requests (and track it)
const limitedRequests$ = concurrent(requests$, responsesProxy$, 3).debug();
// Time to do something with the concurrent requests!
// We would usually map limitedRequests$ to XHR calls and deal
// with a stream of responses but, for this example, we'll
// delay the concurrent requests then map them to `true`
const responses$ = limitedRequests$.map(x => true).compose(delay(1000));
// Now we pipe our responses to the responsesProxy that limitedRequests$ uses to
// release another request
responsesProxy$.imitate(responses$);