-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmodel.js
49 lines (38 loc) · 1.11 KB
/
model.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
var extend = require('extend');
var _ = require('lodash');
var winston = require('winston');
var model = exports;
model.broadcast = function () {};
model.data = {};
model.apply = function (obj) {
// TODO: Should check if anything actually changed
extend(model.data, obj);
model.broadcast('apply', obj);
}
model.set = function (path_str, value) {
var path = path_str.split('.');
var segment, select = model.data;
while ((segment = path.shift())) {
if (path.length && select[segment]) {
select = select[segment];
} else if (path.length) {
select = select[segment] = {};
} else {
if (_.isEqual(select[segment], value)) return;
select[segment] = value;
}
}
winston.info("SET", path_str, value);
model.broadcast('set', [path_str, value]);
}
model.queue = function (name, value, maxEntries) {
if (!Array.isArray(model.data[name])) {
model.data[name] = [];
}
var queue = model.data[name];
queue.unshift(value);
if (queue.length > maxEntries) {
model.data[name] = queue.slice(0, maxEntries);
}
model.broadcast('queue', [name, value, maxEntries]);
};