Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/async/workqueue.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
goog.provide('goog.async.WorkItem');
16
goog.provide('goog.async.WorkQueue');
17
18
goog.require('goog.asserts');
19
goog.require('goog.async.FreeList');
20
21
22
// TODO(johnlenz): generalize the WorkQueue if this is used by more
23
// than goog.async.run.
24
25
26
27
/**
28
* A low GC workqueue. The key elements of this design:
29
* - avoids the need for goog.bind or equivalent by carrying scope
30
* - avoids the need for array reallocation by using a linked list
31
* - minimizes work entry objects allocation by recycling objects
32
* @constructor
33
* @final
34
* @struct
35
*/
36
goog.async.WorkQueue = function() {
37
this.workHead_ = null;
38
this.workTail_ = null;
39
};
40
41
42
/** @define {number} The maximum number of entries to keep for recycling. */
43
goog.define('goog.async.WorkQueue.DEFAULT_MAX_UNUSED', 100);
44
45
46
/** @const @private {goog.async.FreeList<goog.async.WorkItem>} */
47
goog.async.WorkQueue.freelist_ = new goog.async.FreeList(
48
function() { return new goog.async.WorkItem(); },
49
function(item) { item.reset(); }, goog.async.WorkQueue.DEFAULT_MAX_UNUSED);
50
51
52
/**
53
* @param {function()} fn
54
* @param {Object|null|undefined} scope
55
*/
56
goog.async.WorkQueue.prototype.add = function(fn, scope) {
57
var item = this.getUnusedItem_();
58
item.set(fn, scope);
59
60
if (this.workTail_) {
61
this.workTail_.next = item;
62
this.workTail_ = item;
63
} else {
64
goog.asserts.assert(!this.workHead_);
65
this.workHead_ = item;
66
this.workTail_ = item;
67
}
68
};
69
70
71
/**
72
* @return {goog.async.WorkItem}
73
*/
74
goog.async.WorkQueue.prototype.remove = function() {
75
var item = null;
76
77
if (this.workHead_) {
78
item = this.workHead_;
79
this.workHead_ = this.workHead_.next;
80
if (!this.workHead_) {
81
this.workTail_ = null;
82
}
83
item.next = null;
84
}
85
return item;
86
};
87
88
89
/**
90
* @param {goog.async.WorkItem} item
91
*/
92
goog.async.WorkQueue.prototype.returnUnused = function(item) {
93
goog.async.WorkQueue.freelist_.put(item);
94
};
95
96
97
/**
98
* @return {goog.async.WorkItem}
99
* @private
100
*/
101
goog.async.WorkQueue.prototype.getUnusedItem_ = function() {
102
return goog.async.WorkQueue.freelist_.get();
103
};
104
105
106
107
/**
108
* @constructor
109
* @final
110
* @struct
111
*/
112
goog.async.WorkItem = function() {
113
/** @type {?function()} */
114
this.fn = null;
115
/** @type {Object|null|undefined} */
116
this.scope = null;
117
/** @type {?goog.async.WorkItem} */
118
this.next = null;
119
};
120
121
122
/**
123
* @param {function()} fn
124
* @param {Object|null|undefined} scope
125
*/
126
goog.async.WorkItem.prototype.set = function(fn, scope) {
127
this.fn = fn;
128
this.scope = scope;
129
this.next = null;
130
};
131
132
133
/** Reset the work item so they don't prevent GC before reuse */
134
goog.async.WorkItem.prototype.reset = function() {
135
this.fn = null;
136
this.scope = null;
137
this.next = null;
138
};
139
140