-
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.
feat(client): add client side update batching
the client now batches and sends updates in intervals (default: every 50ms). can be disabled by setting updateInterval to 0
- Loading branch information
1 parent
184b7d0
commit 720c33a
Showing
5 changed files
with
114 additions
and
18 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,27 @@ | ||
import { mergeDiff } from './utils'; | ||
|
||
type Queue = (diff: any) => void; | ||
|
||
export function createUpdateBatcher( | ||
flush: (diff: any) => void, | ||
updateInterval: number | ||
): Queue { | ||
if (!updateInterval) return flush; | ||
|
||
let queuedUpdate: any; | ||
let pendingUpdate = null; | ||
|
||
// WARNING: queue function has sideeffects, | ||
// as diff is merged in place | ||
return (diff: any) => { | ||
if (pendingUpdate) { | ||
mergeDiff(diff, queuedUpdate); | ||
} else { | ||
queuedUpdate = diff; | ||
pendingUpdate = setTimeout(() => { | ||
pendingUpdate = null; | ||
flush(queuedUpdate); | ||
}, updateInterval); | ||
} | ||
}; | ||
} |
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