-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.js
140 lines (95 loc) · 2.75 KB
/
counter.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
/**
* Counter API
* @type {Object}
*/
Counter = {
/**
* Timeout delay before log is called.
* @type {Number}
*/
delay: 1000,
/**
* Hash of counter objects, keyed with collection._name's.'
* @type {Object}
*/
counters: {},
/**
* Resets a counter's added, changed and removed counter values to 0.
* @param {Object} counter Counter object.
*/
reset: function(counter) {
counter.added = counter.changed = counter.removed = 0;
},
/**
* Returns the counter object for the given `collection`.
* Creates a new counter object if no counter is found.
* @param {Mongo.Collection} collection Collection to create the counter for.
* @return {Object} Counter object.
*/
get: function(collection) {
var name = collection._name;
var counter = Counter.counters[name];
if (!counter) {
counter = Counter.counters[name] = { name: name };
Counter.reset(counter);
}
return counter;
},
/**
* Updates a counter's added counter.
* @param {Mongo.Collection} collection Collection counter to update.
* @param {Number} count Number to add to the added counter.
*/
added: function(collection, count) {
if (count) {
var counter = Counter.get(collection);
counter.added += count;
Counter.queue(counter);
}
},
/**
* Updates a counter's changed counter.
* @param {Mongo.Collection} collection Collection counter to update.
* @param {Number} count Number to add to the changed counter.
*/
changed: function(collection, count) {
if (count) {
var counter = Counter.get(collection);
counter.changed += count;
Counter.queue(counter);
}
},
/**
* Updates a counter's removed counter.
* @param {Mongo.Collection} collection Collection counter to update.
* @param {Number} count Number to add to the removed counter.
*/
removed: function(collection, count) {
if (count) {
var counter = Counter.get(collection);
counter.removed += count;
Counter.queue(counter);
}
},
/**
* Clears and queues a timeout function for a counter.
* @param {Object} counter Counter object to queue.
*/
queue: function(counter) {
Meteor.clearTimeout(counter.timeout);
counter.timeout = Meteor.setTimeout(function() {
Counter.log(counter);
}, Counter.delay);
},
/**
* Logs the number of added, changed and removed calls.
* @param {Object} counter Counter object to log the values of.
*/
log: function(counter) {
console.log('\nFixtures [', counter.name, ']');
console.log('├─ removed', counter.removed);
console.log('├─ changed', counter.changed);
console.log('└─── added', counter.added);
Counter.reset(counter);
}
};