-
Notifications
You must be signed in to change notification settings - Fork 142
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
4 changed files
with
21 additions
and
30 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 |
---|---|---|
@@ -1,30 +1,29 @@ | ||
import { BoundedBuffer } from './boundedBuffer' | ||
|
||
describe('BoundedBuffer', () => { | ||
it('collect and drain the items', () => { | ||
const spy = jasmine.createSpy<(i: number) => void>() | ||
it('collect and drain the callbacks', () => { | ||
const spy = jasmine.createSpy<() => void>() | ||
const buffered = new BoundedBuffer() | ||
|
||
buffered.add<number>(1, spy) | ||
buffered.add(spy) | ||
expect(spy.calls.count()).toBe(0) | ||
|
||
buffered.drain() | ||
expect(spy.calls.count()).toBe(1) | ||
expect(spy.calls.first().args).toEqual([1]) | ||
|
||
buffered.drain() | ||
expect(spy.calls.count()).toBe(1) | ||
}) | ||
|
||
it('store only the N last items', () => { | ||
const spy = jasmine.createSpy<(i: number) => void>() | ||
it('store only the N last callbacks', () => { | ||
const spy = jasmine.createSpy<() => void>() | ||
const buffered = new BoundedBuffer(5) | ||
|
||
for (let i = 0; i < 10; i += 1) { | ||
buffered.add<number>(i, spy) | ||
buffered.add(spy) | ||
} | ||
|
||
buffered.drain() | ||
expect(spy.calls.allArgs()).toEqual([[5], [6], [7], [8], [9]]) | ||
expect(spy.calls.count()).toEqual(5) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,19 +1,19 @@ | ||
const DEFAULT_LIMIT = 10_000 | ||
|
||
export class BoundedBuffer { | ||
private buffer: Array<[any, (item: any) => void]> = [] | ||
private buffer: Array<() => void> = [] | ||
|
||
constructor(private limit: number = DEFAULT_LIMIT) {} | ||
|
||
add<T>(item: T, drainFn: (item: T) => void) { | ||
const length = this.buffer.push([item, drainFn]) | ||
add(callback: () => void) { | ||
const length = this.buffer.push(callback) | ||
if (length > this.limit) { | ||
this.buffer.splice(0, 1) | ||
} | ||
} | ||
|
||
drain() { | ||
this.buffer.forEach(([item, drainFn]) => drainFn(item)) | ||
this.buffer.forEach((callback) => callback()) | ||
this.buffer.length = 0 | ||
} | ||
} |
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