Skip to content

Commit

Permalink
refine types
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Oct 13, 2019
1 parent b085f6d commit 6c91902
Show file tree
Hide file tree
Showing 2 changed files with 358 additions and 340 deletions.
22 changes: 11 additions & 11 deletions packages/repeater/src/buffers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export interface RepeaterBuffer<T> {
full: boolean;
empty: boolean;
add(value: T): void;
remove(): T;
add(value: PromiseLike<T> | T): void;
remove(): PromiseLike<T> | T;
}

export class FixedBuffer<T> implements RepeaterBuffer<T> {
private arr: T[] = [];
private arr: (PromiseLike<T> | T)[] = [];

get empty(): boolean {
return this.arr.length === 0;
Expand All @@ -22,15 +22,15 @@ export class FixedBuffer<T> implements RepeaterBuffer<T> {
}
}

add(value: T): void {
add(value: PromiseLike<T> | T): void {
if (this.full) {
throw new Error("Buffer full");
} else {
this.arr.push(value);
}
}

remove(): T {
remove(): PromiseLike<T> | T {
if (this.empty) {
throw new Error("Buffer empty");
}
Expand All @@ -41,7 +41,7 @@ export class FixedBuffer<T> implements RepeaterBuffer<T> {

// TODO: use a circular buffer here
export class SlidingBuffer<T> implements RepeaterBuffer<T> {
private arr: T[] = [];
private arr: (PromiseLike<T> | T)[] = [];

get empty(): boolean {
return this.arr.length === 0;
Expand All @@ -57,15 +57,15 @@ export class SlidingBuffer<T> implements RepeaterBuffer<T> {
}
}

add(value: T): void {
add(value: PromiseLike<T> | T): void {
while (this.arr.length >= this.capacity) {
this.arr.shift();
}

this.arr.push(value);
}

remove(): T {
remove(): PromiseLike<T> | T {
if (this.empty) {
throw new Error("Buffer empty");
}
Expand All @@ -75,7 +75,7 @@ export class SlidingBuffer<T> implements RepeaterBuffer<T> {
}

export class DroppingBuffer<T> implements RepeaterBuffer<T> {
private arr: T[] = [];
private arr: (PromiseLike<T> | T)[] = [];

get empty(): boolean {
return this.arr.length === 0;
Expand All @@ -91,13 +91,13 @@ export class DroppingBuffer<T> implements RepeaterBuffer<T> {
}
}

add(value: T): void {
add(value: PromiseLike<T> | T): void {
if (this.arr.length < this.capacity) {
this.arr.push(value);
}
}

remove(): T {
remove(): PromiseLike<T> | T {
if (this.empty) {
throw new Error("Buffer empty");
}
Expand Down
Loading

0 comments on commit 6c91902

Please sign in to comment.