Skip to content

Instantly share code, notes, and snippets.

@jdmichaud
Created October 17, 2019 19:03
Show Gist options
  • Select an option

  • Save jdmichaud/15887f146386ffff8512abcac3d56344 to your computer and use it in GitHub Desktop.

Select an option

Save jdmichaud/15887f146386ffff8512abcac3d56344 to your computer and use it in GitHub Desktop.
Simple Pool pattern in Typescript
interface Pool<T> {
get(): T;
release(t: T): void;
}
class BasePool<T> implements Pool<T> {
private readonly elements: T[] = [];
constructor(
private readonly allocator: () => T,
) {}
get(): T {
if (this.elements.length > 0) {
return this.elements.pop();
}
return this.allocator()
}
release(t: T): void {
this.elements.push(t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment