This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
75 lines (65 loc) · 1.88 KB
/
util.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
// each_slice
// http://stackoverflow.com/questions/10249658/equivalent-of-ruby-enumerableeach-slice-in-javascript
_.mixin({
each_slice: function(obj, slice_size, iterator, context) {
if (_.isUndefined(iterator)) {
var sliced = [];
for (var i=0, l=obj.length; i < l; i+=slice_size) {
sliced.push(obj.slice(i,i+slice_size));
}
return sliced;
}
else {
for (var i=0, l=obj.length; i < l; i+=slice_size) {
iterator.call(context, obj.slice(i,i+slice_size), i, obj);
}
}
}
});
Utils = {
// hook console.log
hook_console_log: function (callback) {
var console = window.console,
_log = console ? console.log : function(){},
_console = {log: function () {_log.apply(console, arguments)} };
console.log = function () {
callback(arguments, _console);
_log.apply(console, arguments);
};
},
hook_console_log_if: function (test, callback) {
this.hook_console_log(function (obj, console) {
if (test(obj, console)) {
callback(obj, console);
}
});
},
// enhance async.js for Meteor.
async : {
_wait_func: function (ms) {
return function (callback) {
Meteor.setTimeout(function () { callback(null, null) }, ms);
};
},
_compact_func: function (callback) {
return function (err, result) {
callback(err, _.compact(result));
};
},
parallel_wait: function (ms, tasks, callback) {
async.parallel(tasks.concat([this._wait_func(ms)]),
this._compact_func(callback));
},
repeat_interval: function (ms, tasks, callback) {
var _this = this;
async.forever(function (next) {
_this.parallel_wait(ms, tasks, function (err, result) {
callback(err, result);
next();
});
}, function (err) {
callback(err, null);
});
},
}
};