-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflux-helpers.js
84 lines (65 loc) · 2.05 KB
/
flux-helpers.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
// watches a reactive collection for changes and emits a changed event
// (via callback) when it's changed.
trackCollection = function(collection, callback) {
if (!collection || !callback) {
throw Error('Collection and callback params needed');
}
// only run on the client, not needed for SSR
if (Meteor.isClient) {
Meteor.startup(function() {
Tracker.autorun(function(computation) {
var docs = collection.find().fetch();
if (computation.firstRun) {
return; // ignore first empty run
}
if (this.__debugFluxHelpers) { // universal global
console.log('[Tracker] collection changed', docs);
}
callback(docs);
});
});
}
};
// Watches a Mongo.Collection cursor for changes and emits a changed event
// (via callback) when it's changed.
trackCollectionCursor = function(cursor, callback) {
if (!cursor || !(cursor instanceof Mongo.Cursor) || !callback) {
throw Error('Mongo.Collection cursor and callback params needed');
}
// only run on the client, not needed for SSR
if (Meteor.isClient) {
Meteor.startup(function() {
Tracker.autorun(function(computation) {
var docs = cursor.fetch();
if (computation.firstRun) {
return; // ignore first empty run
}
if (this.__debugFluxHelpers) { // universal global
console.log('[Tracker] collection changed', docs);
}
callback(docs);
});
});
}
};
// track changes on the viewer's user account
trackViewer = function(callback) {
if (!callback) {
throw Error('Callback param needed');
}
// only run on the client, not needed for SSR
if (Meteor.isClient) {
Meteor.startup(function() {
Tracker.autorun(function(computation) {
var user = Meteor.user();
if (computation.firstRun) {
return; // ignore first empty run
}
if (this.__debugFluxHelpers) { // universal global
console.log('[Tracker] user changed', user);
}
callback(user);
});
});
}
};