Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/async/freelist.js
2868 views
1
// Copyright 2015 The Closure Library Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS-IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
* @fileoverview Simple freelist.
17
*
18
* An anterative to goog.structs.SimplePool, it imposes the requirement that the
19
* objects in the list contain a "next" property that can be used to maintain
20
* the pool.
21
*/
22
23
goog.provide('goog.async.FreeList');
24
25
26
/**
27
* @template ITEM
28
*/
29
goog.async.FreeList = goog.defineClass(null, {
30
/**
31
* @param {function():ITEM} create
32
* @param {function(ITEM):void} reset
33
* @param {number} limit
34
*/
35
constructor: function(create, reset, limit) {
36
/** @private @const {number} */
37
this.limit_ = limit;
38
/** @private @const {function()} */
39
this.create_ = create;
40
/** @private @const {function(ITEM):void} */
41
this.reset_ = reset;
42
43
/** @private {number} */
44
this.occupants_ = 0;
45
/** @private {ITEM} */
46
this.head_ = null;
47
},
48
49
/**
50
* @return {ITEM}
51
*/
52
get: function() {
53
var item;
54
if (this.occupants_ > 0) {
55
this.occupants_--;
56
item = this.head_;
57
this.head_ = item.next;
58
item.next = null;
59
} else {
60
item = this.create_();
61
}
62
return item;
63
},
64
65
/**
66
* @param {ITEM} item An item available for possible future reuse.
67
*/
68
put: function(item) {
69
this.reset_(item);
70
if (this.occupants_ < this.limit_) {
71
this.occupants_++;
72
item.next = this.head_;
73
this.head_ = item;
74
}
75
},
76
77
/**
78
* Visible for testing.
79
* @package
80
* @return {number}
81
*/
82
occupants: function() { return this.occupants_; }
83
});
84
85