-
Notifications
You must be signed in to change notification settings - Fork 1
/
lang.js
205 lines (198 loc) · 5.84 KB
/
lang.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
jojo.ns("jojo.lang");
/**
* Class to handle collection registration/removal with optional events
*/
jojo.lang.Registry = Class.create();
jojo.lang.Registry.prototype = {
/**
* Creates a new Registry
* @constructor
* @param {Boolean} unique True to require unique IDs for all items in the registry
* @param {Boolean} fireEvents True to implement an event publisher
* @param {String} uniqueErrorMessage Error message to throw when unique property is violated
*/
initialize: function(unique, fireEvents, uniqueErrorMessage) {
//public properties
this.items = [];
this.itemCache = {};
if (typeof unique == "undefined" || unique == null) {
unique = true;
}
this.unique = unique;
this.uniqueErrorMessage = uniqueErrorMessage || "Error: All items in this registry instance must have unique IDs.";
//decorate with event functionality if needed
if (typeof fireEvents == "undefined" || fireEvents == null) {
fireEvents = false;
}
this.fireEvents = fireEvents;
if (this.fireEvents) {
Object.extend(this, new jojo.event.EventPublisher());
}
},
/**
* Add an item to the registry
* @param {Object} item Item to be added
* @param {Boolean} onlyAddIfNotAlreadyAdded True to check if an item already exists with the current item ID - if one exists, method will return false
* @param {Integer} stackIndex Optional, where to insert the item into the stack/list of registry items
* @return {Boolean}
*/
add: function(item, onlyAddIfNotAlreadyAdded, stackIndex) {
if (item.id) {
if (onlyAddIfNotAlreadyAdded && this.findById(item.id)) {
return false;
}
if (this.unique && this.findById(item.id) != null) {
throw new Error(this.uniqueErrorMessage + "\n\nid: " + item.id);
}
if (stackIndex != undefined && !isNaN(stackIndex)) {
if (this.items.length == 0) {
this.items.push(item);
}
else {
var tmpItems = [];
stackIndex = stackIndex < 0 ? 0 : (stackIndex > this.items.length - 1 ? this.items.length - 1 : stackIndex);
this.each(function(h, index){
if (index == stackIndex) {
tmpItems.push(item);
}
tmpItems.push(h);
});
this.items = tmpItems;
}
}
else {
this.items.push(item);
}
if (this.unique) {
this.itemCache[item.id] = item;
}
if (this.fireEvents) {
this.fire("itemAdded", {item: item});
}
} else {
throw new Error("Items must have 'id' properties in order to be added to a registry.");
}
return true;
},
/**
* Copy items from one collection based object (basically any object that supports .each enumeration)
* into this registry instance
*/
addRange: function(collection, onlyAddIfNotAlreadyAdded) {
var me = this;
collection.each(function(item) {
me.add(item, onlyAddIfNotAlreadyAdded);
});
},
/**
* Add an item to the registry
* @param {Object} item Item to be added
* @param {Integer} stackIndex Where to insert the item into the stack/list of registry items
* @param {Boolean} onlyAddIfNotAlreadyAdded True to check if an item already exists with the current item ID -
* if one exists, method will return false
*/
addAt: function(item, stackIndex, onlyAddIfNotAlreadyAdded) {
return this.add(item, onlyAddIfNotAlreadyAdded, stackIndex);
},
/**
* Finds an item in the registry
* @param {Function} iterator Function which returns true when the desired item is found
* @return {Object} The item
*/
find: function(iterator) {
return this.items.find(iterator);
},
/**
* Finds an item in the registry by ID
* @param {String} id ID of the registry item
* @return {Object} The item
*/
findById: function(id) {
if (this.unique) {
return this.itemCache[id];
}
for(var i = 0; i < this.items.length; i++) {
if (this.items[i].id == id) {
return this.items[i];
}
}
return null;
},
/**
* Removes an item from the registry by ID
* @param {String} id ID of the registry item
* @return {Boolean}
*/
removeById: function(id) {
var item = this.findById(id);
return this.remove(item);
},
/**
* Removes an item from the registry
* @param {Object} item The registry item
* @return {Boolean}
*/
remove: function(item) {
if (!item) {
return false;
}
var foundItem = false;
this.items = this.items.reject(function(it) {
if (it === item) {
foundItem = true;
return true;
}
return false;
});
if(this.unique) {
delete this.itemCache[item.id];
}
if (foundItem) {
if (this.fireEvents) {
this.fire("itemRemoved", {item: item});
}
if (this.items.length == 0 && this.fireEvents) {
this.fire("cleared");
}
return true;
}
return false;
},
/**
* Removes all items from the registry
*/
removeAll: function() {
var me = this;
this.each(function(item) {
me.remove(item);
});
},
/**
* Performs an action on each item in the registry
* @param {Function} iterator Function containing code to execute on each item
*/
each: function(iterator) {
this.items.each(iterator);
}
};
/**
* Semaphore is a nice little async helper when multiple potential async paths need to complete before a common callback gets executed
*/
jojo.lang.Semaphore = Class.create({
initialize: function(callback, context) {
this.semaphore = 0;
this.callback = callback;
this.context = context || this;
},
increment: function() {
this.semaphore++;
},
decrement: function() {
this.semaphore--;
},
execute: function() {
if (this.semaphore == 0 && this.callback) {
this.callback.apply(this.context, arguments); //this means that the args that actually reach the callback will be from the LAST async block to call .execute();
}
}
});