This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
atom.js
442 lines (398 loc) · 11.3 KB
/
atom.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//
// atom.js
// https://github.com/zynga/atom
// Author: Chris Campbell (@quaelin)
// License: BSD
//
(function (undef) {
'use strict';
var
atom,
name = 'atom',
VERSION = '0.5.6',
ObjProto = Object.prototype,
hasOwn = ObjProto.hasOwnProperty,
typeObj = 'object',
typeUndef = 'undefined',
root = typeof window !== typeUndef ? window : global,
had = hasOwn.call(root, name),
prev = root[name]
;
// Convenience methods
var slice = Array.prototype.slice;
var isArray = Array.isArray || function (obj) {
return ObjProto.toString.call(obj) === '[object Array]';
};
function inArray(arr, value) {
for (var i = arr.length; --i >= 0;) {
if (arr[i] === value) {
return true;
}
}
}
function toArray(obj) {
return isArray(obj) ? obj : [obj];
}
function isEmpty(obj) {
for (var p in obj) {
if (hasOwn.call(obj, p)) {
return false;
}
}
return true;
}
// Property getter
function get(nucleus, keyOrList, func) {
var isList = isArray(keyOrList), keys = isList ? keyOrList : [keyOrList],
key, values = [], props = nucleus.props, missing = {},
result = { values: values };
for (var i = keys.length; --i >= 0;) {
key = keys[i];
if (!hasOwn.call(props, key)) {
result.missing = missing;
missing[key] = true;
}
values.unshift(props[key]);
}
return func ? func.apply({}, values) : result;
}
// Helper to remove an exausted listener from the listeners array
function removeListener(listeners) {
for (var i = listeners.length; --i >= 0;) {
// There should only be ONE exhausted listener.
if (!listeners[i].calls) {
return listeners.splice(i, 1);
}
}
}
// Used to detect listener recursion; a given object may only appear once.
var objStack = [];
// Property setter
function set(nucleus, key, value) {
var keys, listener, listeners = nucleus.listeners, missing,
listenersCopy = [].concat(listeners), i = listenersCopy.length,
props = nucleus.props, oldValue = props[key],
had = hasOwn.call(props, key),
isObj = value && typeof value === typeObj;
props[key] = value;
if (!had || oldValue !== value || (isObj && !inArray(objStack, value))) {
if (isObj) {
objStack.push(value);
}
while (--i >= 0) {
listener = listenersCopy[i];
keys = listener.keys;
missing = listener.missing;
if (missing) {
if (hasOwn.call(missing, key)) {
delete missing[key];
if (isEmpty(missing)) {
listener.cb.apply({}, get(nucleus, keys).values);
listener.calls--;
}
}
} else if (inArray(keys, key)) {
listener.cb.apply({}, get(nucleus, keys).values);
listener.calls--;
}
if (!listener.calls) {
removeListener(listeners);
}
}
delete nucleus.needs[key];
if (isObj) {
objStack.pop();
}
}
}
// Wrapper to prevent a callback from getting invoked more than once.
function preventMultiCall(callback) {
var ran;
return function () {
if (!ran) {
ran = 1;
callback.apply(this, arguments);
}
};
}
// Helper function for setting up providers.
function provide(nucleus, key, provider) {
provider(preventMultiCall(function (result) {
set(nucleus, key, result);
}));
}
// Determine whether two keys (or sets of keys) are equivalent.
function keysMatch(keyOrListA, keyOrListB) {
var a, b;
if (keyOrListA === keyOrListB) {
return true;
}
a = [].concat(toArray(keyOrListA)).sort();
b = [].concat(toArray(keyOrListB)).sort();
return a + '' === b + '';
}
// Return an instance.
atom = root[name] = function () {
var
args = slice.call(arguments, 0),
nucleus = {},
props = nucleus.props = {},
needs = nucleus.needs = {},
providers = nucleus.providers = {},
listeners = nucleus.listeners = [],
q = []
;
// Execute the next function in the async queue.
function doNext() {
if (q) {
q.pending = q.next = (!q.next && q.length) ?
q.shift() : q.next;
q.args = slice.call(arguments, 0);
if (q.pending) {
q.next = 0;
q.pending.apply({}, [preventMultiCall(doNext)].concat(q.args));
}
}
}
var me = {
// Add a function or functions to the async queue. Functions added
// thusly must call their first arg as a callback when done. Any args
// provided to the callback will be passed in to the next function in
// the queue.
chain: function () {
if (q) {
for (var i = 0, len = arguments.length; i < len; i++) {
q.push(arguments[i]);
if (!q.pending) {
doNext.apply({}, q.args || []);
}
}
}
return me;
},
// Remove references to all properties and listeners. This releases
// memory, and effective stops the atom from working.
destroy: function () {
delete nucleus.props;
delete nucleus.needs;
delete nucleus.providers;
delete nucleus.listeners;
while (q.length) {
q.pop();
}
nucleus = props = needs = providers = listeners =
q = q.pending = q.next = q.args = 0;
},
// Call `func` on each of the specified keys. The key is provided as
// the first arg, and the value as the second.
each: function (keyOrList, func) {
var keys = toArray(keyOrList), i = -1, len = keys.length, key;
while (++i < len) {
key = keys[i];
func(key, me.get(key));
}
return me;
},
// Establish two-way binding between a key or list of keys for two
// different atoms, so that changing a property on either atom will
// propagate to the other. If a map is provided for `keyOrListOrMap`,
// properties on this atom may be bound to differently named properties
// on `otherAtom`. Note that entangled properties will not actually be
// synchronized until the first change *after* entanglement.
entangle: function (otherAtom, keyOrListOrMap) {
var
isList = isArray(keyOrListOrMap),
isMap = !isList && typeof keyOrListOrMap === typeObj,
i, key,
keys = isList ? keyOrListOrMap : isMap ? [] : [keyOrListOrMap],
map = isMap ? keyOrListOrMap : {}
;
if (isMap) {
for (key in map) {
if (hasOwn.call(map, key)) {
keys.push(key);
}
}
} else {
for (i = keys.length; --i >= 0;) {
key = keys[i];
map[key] = key;
}
}
me.each(keys, function (key) {
var otherKey = map[key];
me.on(key, function (value) {
otherAtom.set(otherKey, value);
});
otherAtom.on(otherKey, function (value) {
me.set(key, value);
});
});
return me;
},
// Get current values for the specified keys. If `func` is provided,
// it will be called with the values as args.
get: function (keyOrList, func) {
var result = get(nucleus, keyOrList, func);
return func ? result : typeof keyOrList === 'string' ?
result.values[0] : result.values;
},
// Returns true iff all of the specified keys exist (regardless of
// value).
has: function (keyOrList) {
var keys = toArray(keyOrList);
for (var i = keys.length; --i >= 0;) {
if (!hasOwn.call(props, keys[i])) {
return false;
}
}
return true;
},
// Return a list of all keys.
keys: function () {
var keys = [];
for (var key in props) {
if (hasOwn.call(props, key)) {
keys.push(key);
}
}
return keys;
},
// Add arbitrary properties to this atom's interface.
mixin: function (obj) {
for (var p in obj) {
if (hasOwn.call(obj, p)) {
me[p] = obj[p];
}
}
return me;
},
// Call `func` as soon as all of the specified keys have been set. If
// they are already set, the function will be called immediately, with
// all the values provided as args. In this, it is identical to
// `once()`. However, calling `need()` will additionally invoke
// providers when possible, in order to try and create the required
// values.
need: function (keyOrList, func) {
var key, keys = toArray(keyOrList), provider;
for (var i = keys.length; --i >= 0;) {
key = keys[i];
provider = providers[key];
if (!hasOwn.call(props, key) && provider) {
provide(nucleus, key, provider);
delete providers[key];
} else {
needs[key] = true;
}
}
if (func) {
me.once(keys, func);
}
return me;
},
// Call `func` whenever any of the specified keys is next changed. The
// values of all keys will be provided as args to the function. The
// function will automatically be unbound after being called the first
// time, so it is guaranteed to be called no more than once.
next: function (keyOrList, func) {
listeners.unshift(
{ keys: toArray(keyOrList), cb: func, calls: 1 });
return me;
},
// Unregister a listener `func` that was previously registered using
// `on()`, `bind()`, `need()`, `next()` or `once()`. `keyOrList` is
// optional; if provided, it will selectively remove the listener only
// for the specified combination of properties.
off: function (keyOrList, func) { // alias: `unbind`
var i = listeners.length, listener;
if (arguments.length === 1) {
func = keyOrList;
keyOrList = 0;
}
while (--i >= 0) {
listener = listeners[i];
if (listener.cb === func &&
(!keyOrList || keysMatch(listener.keys, keyOrList)))
{
listeners.splice(i, 1);
}
}
return me;
},
// Call `func` whenever any of the specified keys change. The values
// of the keys will be provided as args to func.
on: function (keyOrList, func) { // alias: `bind`
listeners.unshift({ keys: toArray(keyOrList), cb: func,
calls: Infinity });
return me;
},
// Call `func` as soon as all of the specified keys have been set. If
// they are already set, the function will be called immediately, with
// all the values provided as args. Guaranteed to be called no more
// than once.
once: function (keyOrList, func) {
var keys = toArray(keyOrList),
results = get(nucleus, keys),
values = results.values,
missing = results.missing;
if (!missing) {
func.apply({}, values);
} else {
listeners.unshift(
{ keys: keys, cb: func, missing: missing, calls: 1 });
}
return me;
},
// Register a provider for a particular key. The provider `func` is a
// function that will be called if there is a need to create the key.
// It must call its first arg as a callback, with the value. Provider
// functions will be called at most once.
provide: function (key, func) {
if (needs[key]) {
provide(nucleus, key, func);
} else if (!providers[key]) {
providers[key] = func;
}
return me;
},
// Set value for a key, or if `keyOrMap` is an object then set all the
// keys' corresponding values.
set: function (keyOrMap, value) {
if (typeof keyOrMap === typeObj) {
for (var key in keyOrMap) {
if (hasOwn.call(keyOrMap, key)) {
set(nucleus, key, keyOrMap[key]);
}
}
} else {
set(nucleus, keyOrMap, value);
}
return me;
}
};
me.bind = me.on;
me.unbind = me.off;
if (args.length) {
me.set.apply(me, args);
}
return me;
};
atom.VERSION = VERSION;
// For backwards compatibility with < 0.4.0
atom.create = atom;
atom.noConflict = function () {
if (root[name] === atom) {
root[name] = had ? prev : undef;
if (!had) {
try {
delete root[name];
} catch (ex) {
}
}
}
return atom;
};
if (typeof module !== typeUndef && module.exports) {
module.exports = atom;
}
}());