Removed unnecessary configurable Bridge batch duration
Brian Vaughn committed
Apr 11, 2019 at 09:09 UTC
4926d160d6d270805ecc8c569125403e74d97c1c
2 files changed
+11
-26
src/__tests__/setupTests.js
+7
-10
@@ -26,17 +26,14 @@ env.beforeEach(() => {
26
27
bridgeListeners = [];
28
29
- bridge = new Bridge(
30
- {
31
- listen(callback) {
32
- bridgeListeners.push(callback);
33
- },
34
- send(event: string, payload: any, transferable?: Array<any>) {
35
- bridgeListeners.forEach(callback => callback({ event, payload }));
36
- },
29
+ bridge = new Bridge({
30
+ listen(callback) {
31
+ bridgeListeners.push(callback);
32
},
38
- { batchDuration: 0 }
39
- );
33
+ send(event: string, payload: any, transferable?: Array<any>) {
34
+ bridgeListeners.forEach(callback => callback({ event, payload }));
35
+ },
36
+ });
37
38
agent = new Agent();
39
agent.addBridge(bridge);
src/bridge.js
+4
-16
@@ -6,31 +6,19 @@ import type { Wall } from './types';
6
7
const BATCH_DURATION = 100;
8
9
-type Config = {|
10
- batchDuration?: number,
11
-|};
12
-
9
type Message = {|
10
event: string,
11
payload: any,
12
|};
13
14
export default class Bridge extends EventEmitter {
19
- _batchDuration: number = BATCH_DURATION;
15
_messageQueue: Array<any> = [];
16
_timeoutID: TimeoutID | null = null;
17
_wall: Wall;
18
24
- constructor(wall: Wall, config?: Config) {
19
+ constructor(wall: Wall) {
20
super();
21
27
- if (config != null) {
28
- const { batchDuration } = config;
29
- if (batchDuration !== undefined) {
30
- this._batchDuration = batchDuration;
31
- }
32
- }
33
-
22
this._wall = wall;
23
24
wall.listen((message: Message) => {
@@ -44,7 +32,7 @@ export default class Bridge extends EventEmitter {
32
// - if there hasn't been a message recently, we set a timer for 0 ms in
33
// the future, allowing all messages created in the same tick to be sent
34
// together
47
- // - if there *has* been a message flushed in the last X ms
35
+ // - if there *has* been a message flushed in the last BATCH_DURATION ms
36
// (or we're waiting for our setTimeout-0 to fire), then _timeoutID will
37
// be set, and we'll simply add to the queue and wait for that
38
this._messageQueue.push(event, payload, transferable);
@@ -67,10 +55,10 @@ export default class Bridge extends EventEmitter {
55
}
56
this._messageQueue.length = 0;
57
70
- // Check again for queued messages in X ms. This will keep
58
+ // Check again for queued messages in BATCH_DURATION ms. This will keep
59
// flushing in a loop as long as messages continue to be added. Once no
60
// more are, the timer expires.
73
- this._timeoutID = setTimeout(this._flush, this._batchDuration);
61
+ this._timeoutID = setTimeout(this._flush, BATCH_DURATION);
62
}
63
};
64
}