-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: batched transport for shared state
- Loading branch information
Showing
5 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { BATCHED_TRANSPORT_CHANNEL } from './constants.js' | ||
|
||
class BatchedTransport { | ||
constructor(transport) { | ||
this._transport = transport; | ||
this._stack = []; | ||
this._listeners = new Map(); | ||
this._sending = false; | ||
|
||
this._transport.addListener(BATCHED_TRANSPORT_CHANNEL, stack => { | ||
stack.forEach(entry => { | ||
const [channel, args] = entry; | ||
const callbacks = this._listeners.get(channel); | ||
|
||
callbacks.forEach(callback => { | ||
callback(...args); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
addListener(channel, callback) { | ||
if (!this._listeners.has(channel)) { | ||
this._listeners.set(channel, new Set()); | ||
} | ||
|
||
const callbacks = this._listeners.get(channel); | ||
callbacks.add(callback); | ||
} | ||
|
||
async emit(channel, ...args) { | ||
this._stack.push([channel, args]); | ||
|
||
if (!this._sending) { | ||
this._sending = true; | ||
this._sending = await false; | ||
const stack = this._stack; | ||
this._stack = []; | ||
this._transport.emit(BATCHED_TRANSPORT_CHANNEL, stack); | ||
} | ||
} | ||
|
||
removeListener(channel, callback) { | ||
if (!this._listeners.has(channel)) { | ||
const callbacks = this._listeners.get(channel); | ||
callbacks.delete(callback); | ||
} | ||
} | ||
|
||
removeAllListeners(channel = null) { | ||
if (channel === null) { | ||
this._listeners.clear(); | ||
} else if (this._listeners.has(channel)) { | ||
const callbacks = this._listeners.get(channel); | ||
callbacks.clear(); | ||
} | ||
} | ||
} | ||
|
||
export default BatchedTransport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters