| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict' |
| 2 | |||||
| 3 | function reusify (Constructor) { | ||||
| 4 | var head = new Constructor() | ||||
| 5 | var tail = head | ||||
| 6 | |||||
| 7 | function get () { | ||||
| 8 | var current = head | ||||
| 9 | |||||
| 10 | if (current.next) { | ||||
| 11 | head = current.next | ||||
| 12 | } else { | ||||
| 13 | head = new Constructor() | ||||
| 14 | tail = head | ||||
| 15 | } | ||||
| 16 | |||||
| 17 | current.next = null | ||||
| 18 | |||||
| 19 | return current | ||||
| 20 | } | ||||
| 21 | |||||
| 22 | function release (obj) { | ||||
| 23 | tail.next = obj | ||||
| 24 | tail = obj | ||||
| 25 | } | ||||
| 26 | |||||
| 27 | return { | ||||
| 28 | get: get, | ||||
| 29 | release: release | ||||
| 30 | } | ||||
| 31 | } | ||||
| 32 | |||||
| 33 | module.exports = reusify | ||||