-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,76 @@ | ||
import { assert as assertMod, sleep } from '../dep.ts'; | ||
import { Bundler } from './bundler.ts'; | ||
|
||
const { assertEquals } = assertMod; | ||
|
||
export const b = ([value]: TemplateStringsArray) => new TextEncoder().encode(value); | ||
|
||
const joinMerger = (updates: Uint8Array[]) => { | ||
const totLength = updates.reduce((acc, cur) => acc + cur.length, 0); | ||
const ret = new Uint8Array(totLength); | ||
updates.reduce((offset, cur) => { | ||
ret.set(cur, offset); | ||
return offset + cur.length; | ||
}, 0); | ||
return ret; | ||
}; | ||
|
||
Deno.test('Bundler.1', async () => { | ||
const result: Array<Uint8Array> = []; | ||
const bundler = new Bundler( | ||
joinMerger, | ||
(update) => { | ||
result.push(update); | ||
return Promise.resolve(); | ||
}, | ||
{ | ||
thresholdSize: 100, | ||
delayMs: 100, | ||
}, | ||
); | ||
|
||
await bundler.produce(b`Hello `); | ||
await bundler.produce(b`World.`); | ||
await sleep(0.1); | ||
assertEquals(result, [b`Hello World.`]); | ||
}); | ||
|
||
Deno.test('Bundler.2', async () => { | ||
const result: Array<Uint8Array> = []; | ||
const bundler = new Bundler( | ||
joinMerger, | ||
(update) => { | ||
result.push(update); | ||
return Promise.resolve(); | ||
}, | ||
{ | ||
thresholdSize: 3, | ||
delayMs: 100, | ||
}, | ||
); | ||
|
||
await bundler.produce(b`Hello `); | ||
await bundler.produce(b`World.`); | ||
assertEquals(result, [b`Hello `, b`World.`]); | ||
}); | ||
|
||
Deno.test('Bundler.3', async () => { | ||
const result: Array<Uint8Array> = []; | ||
const bundler = new Bundler( | ||
joinMerger, | ||
(update) => { | ||
result.push(update); | ||
return Promise.resolve(); | ||
}, | ||
{ | ||
thresholdSize: 10, | ||
delayMs: 100, | ||
}, | ||
); | ||
|
||
await bundler.produce(b`Hello `); | ||
await bundler.produce(b`World,`); | ||
await bundler.produce(b`Hello `); | ||
await bundler.produce(b`World.`); | ||
assertEquals(result, [b`Hello World,`, b`Hello World.`]); | ||
}); |
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,49 @@ | ||
export type BundlerOpts = { | ||
thresholdSize?: number; | ||
delayMs?: number; | ||
}; | ||
|
||
export class Bundler { | ||
protected _bundle: Array<Uint8Array> = []; | ||
protected _timerId: number | undefined; | ||
protected _cumulativeSize = 0; | ||
protected _opts: Required<BundlerOpts>; | ||
|
||
constructor( | ||
public readonly merge: (updates: Array<Uint8Array>) => Uint8Array, | ||
public readonly emit: (update: Uint8Array) => Promise<void>, | ||
opts: { | ||
thresholdSize?: number; | ||
delayMs?: number; | ||
} = {}, | ||
) { | ||
this._opts = { | ||
thresholdSize: 3000, | ||
delayMs: 200, | ||
...opts, | ||
}; | ||
} | ||
|
||
public async produce(update: Uint8Array) { | ||
this._bundle.push(update); | ||
this._cumulativeSize += update.byteLength; | ||
if (this._cumulativeSize > this._opts.thresholdSize) { | ||
// Emit immediately | ||
await this.issue(); | ||
} else if (!this._timerId) { | ||
// Schedule emit | ||
this._timerId = setTimeout(() => this.issue(), this._opts.delayMs); | ||
} | ||
} | ||
|
||
public async issue() { | ||
if (this._timerId) { | ||
clearTimeout(this._timerId); | ||
} | ||
this._timerId = undefined; | ||
const output = this._bundle.length === 1 ? this._bundle[0] : this.merge(this._bundle); | ||
this._bundle = []; | ||
this._cumulativeSize = 0; | ||
await this.emit(output); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
*/ | ||
export * as assert from 'https://deno.land/[email protected]/assert/mod.ts'; | ||
export * as hex from 'https://deno.land/[email protected]/encoding/hex.ts'; | ||
export { sleep } from 'https://deno.land/x/[email protected]/mod.ts'; |
Oops, something went wrong.