Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/async/freelist.js
4122 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Simple freelist.
9
*
10
* An anterative to goog.structs.SimplePool, it imposes the requirement that the
11
* objects in the list contain a "next" property that can be used to maintain
12
* the pool.
13
*/
14
goog.module('goog.async.FreeList');
15
goog.module.declareLegacyNamespace();
16
17
/** @template ITEM */
18
class FreeList {
19
/**
20
* @param {function():ITEM} create
21
* @param {function(ITEM):void} reset
22
* @param {number} limit
23
*/
24
constructor(create, reset, limit) {
25
/** @private @const {number} */
26
this.limit_ = limit;
27
/** @private @const {function()} */
28
this.create_ = create;
29
/** @private @const {function(ITEM):void} */
30
this.reset_ = reset;
31
32
/** @private {number} */
33
this.occupants_ = 0;
34
/** @private {ITEM} */
35
this.head_ = null;
36
}
37
38
/** @return {ITEM} */
39
get() {
40
let item;
41
if (this.occupants_ > 0) {
42
this.occupants_--;
43
item = this.head_;
44
this.head_ = item.next;
45
item.next = null;
46
} else {
47
item = this.create_();
48
}
49
return item;
50
}
51
52
/** @param {ITEM} item An item available for possible future reuse. */
53
put(item) {
54
this.reset_(item);
55
if (this.occupants_ < this.limit_) {
56
this.occupants_++;
57
item.next = this.head_;
58
this.head_ = item;
59
}
60
}
61
62
/**
63
* Visible for testing.
64
* @return {number}
65
* @package
66
*/
67
occupants() {
68
return this.occupants_;
69
}
70
}
71
72
exports = FreeList;
73
74