Created
December 21, 2015 13:49
-
-
Save Makio64/7422170c0b7b2682c46d to your computer and use it in GitHub Desktop.
Utility class to manage pool of objects
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
| # Utility class to manage pool of objects | |
| class ObjectPool | |
| constructor: (@create, @minSize, @maxSize) -> | |
| @list = [] | |
| for [0...@minSize] | |
| @add() | |
| return | |
| add: () -> | |
| return @list.push @create() | |
| checkOut: () -> | |
| if @list.length == 0 | |
| return @create() | |
| else | |
| return @list.pop() | |
| checkIn: (item) -> | |
| if @list.length < @maxSize | |
| @list.push item | |
| return | |
| module.exports = ObjectPool |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment