Created
October 17, 2019 19:03
-
-
Save jdmichaud/15887f146386ffff8512abcac3d56344 to your computer and use it in GitHub Desktop.
Simple Pool pattern in Typescript
This file contains hidden or 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
| 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