-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
batches all socket events, this includes subscribe, unsubscribe, subscribeKeys calls. this way nested subscribe calls (e.g. "on" inside "each") will now only emit a single socket package (with a list of multiple events) BREAKING CHANGE: this breaks compatibility with previous versions. make sure to upgrade both client and server
- Loading branch information
1 parent
8659c99
commit 8a06672
Showing
9 changed files
with
742 additions
and
700 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { createQueue } from './queue'; | ||
import { createEventBroker } from './socketAdapter/eventBroker'; | ||
import { Socket } from './socketAdapter/socket'; | ||
|
||
type GenericEvents = { | ||
[key: string]: any; | ||
}; | ||
|
||
export type GenericQueuedEvent<Events extends GenericEvents> = { | ||
event: keyof Events; | ||
data: Events[keyof Events]; | ||
}; | ||
|
||
export function createBatchedClient<Events extends GenericEvents>( | ||
connection: Socket, | ||
interval?: number | ||
) { | ||
type QueuedEvent = GenericQueuedEvent<Events>; | ||
|
||
const events = createEventBroker<QueuedEvent['data']>(); | ||
|
||
const queue = createQueue<QueuedEvent, QueuedEvent[]>({ | ||
batch(current, update) { | ||
return [...current, update]; | ||
}, | ||
createState() { | ||
return []; | ||
}, | ||
flush(events) { | ||
connection.send('data', { events }); | ||
}, | ||
updateInterval: interval, | ||
}); | ||
|
||
connection.on('data', (data: { events: QueuedEvent[] }) => { | ||
data?.events?.forEach((event) => { | ||
events.notify(event.event as string, event.data); | ||
}); | ||
}); | ||
|
||
return { | ||
queue<K extends keyof Events>(event: K, data: Events[K]) { | ||
queue({ event, data }); | ||
}, | ||
subscribe<K extends keyof Events>( | ||
event: K, | ||
callback: (data: Events[K]) => void | ||
) { | ||
events.addListener(event as string, callback as any); | ||
return () => events.removeListener(event as string, callback as any); | ||
}, | ||
unsubscribe<K extends keyof Events>( | ||
event: K, | ||
callback?: (data: Events[K]) => void | ||
) { | ||
events.removeListener(event as string, callback as any); | ||
}, | ||
}; | ||
} |
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,33 @@ | ||
export type Queue<T> = (data: T) => void; | ||
|
||
export function createQueue<QueuedData, BatchedData>({ | ||
batch, | ||
createState, | ||
flush, | ||
updateInterval, | ||
}: { | ||
createState: () => BatchedData; | ||
batch: (current: BatchedData, update: QueuedData) => BatchedData; | ||
flush: (batchedData: BatchedData) => void; | ||
updateInterval?: number; | ||
}): Queue<QueuedData> { | ||
if (!updateInterval) | ||
return (update) => { | ||
flush(batch(createState(), update)); | ||
}; | ||
|
||
let current = createState(); | ||
let pendingUpdate: ReturnType<typeof setTimeout> | null = null; | ||
|
||
return (update) => { | ||
if (!pendingUpdate) { | ||
current = createState(); | ||
pendingUpdate = setTimeout(() => { | ||
pendingUpdate = null; | ||
flush(current); | ||
}, updateInterval); | ||
} | ||
|
||
current = batch(current, update); | ||
}; | ||
} |
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
Oops, something went wrong.