-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpublish_cache_client.js
200 lines (172 loc) · 5.61 KB
/
publish_cache_client.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
var cachedSubs = {};
Meteor.subscribeCache = function (/*name .. [arguments] .. (callback|callbacks)*/) {
var args = _.toArray(arguments);
var callbacks = {};
var methodName = '/cache/' + args.shift();
if (args.length) {
var lastParam = args[args.length - 1];
if (typeof lastParam === "function") {
callbacks.onReady = args.pop();
} else if (lastParam && (typeof lastParam.onReady === "function" ||
typeof lastParam.onError === "function")) {
callbacks = args.pop();
}
};
/**
* We set up our CachedSubs information based on request
*/
var alreadyCached = false;
cachedSubs[methodName] = cachedSubs[methodName] || [];
var thisSubs = cachedSubs[methodName];
var argsStr = JSON.stringify(args);
var subscriptionObj = _.findWhere(thisSubs, {args: argsStr});
var timestamp = Math.floor(new Date().getTime() / 1000);
if (subscriptionObj &&
(subscriptionObj.sec + subscriptionObj.timestamp) > timestamp) {
alreadyCached = true;
} else {
// We are going to create a new subscriptionObj,
// so lets remove the old one, if it exists
if (subscriptionObj) {
var index = thisSubs.indexOf(subscriptionObj);
if (index > -1) thisSubs.splice(index, 1);
}
var id = Random.id();
subscriptionObj = {
id: id,
sec: 5,
timestamp: timestamp,
ready: false,
args: JSON.stringify(args),
readyDeps: new Tracker.Dependency,
remove: function() {
},
stop: function() {
},
cache: function(sec) {
this.sec = sec;
}
};
cachedSubs[methodName].push(subscriptionObj);
}
// Even though subscription handle methods aren't really relevant
// to subscribeCache, we still return them in order to be
// consistent with the subscribe api, and allow callers
// (e.g. iron:router) to use this package without breaking.
var handle = {
stop: function(){
subscriptionObj.stop();
},
ready: function() {
subscriptionObj.readyDeps.depend();
return subscriptionObj.ready;
},
cache: function(sec) {
subscriptionObj.cache(sec);
return this;
}
};
if (alreadyCached) {
if (callbacks.onReady) {
callbacks.onReady();
}
subscriptionObj.readyDeps.changed();
subscriptionObj.ready = true;
} else {
Meteor.apply(methodName, args, function (err, res) {
function finish() {
subscriptionObj.readyDeps.changed();
subscriptionObj.ready = true;
subscriptionObj.timestamp = Math.floor(new Date().getTime() / 1000 );
}
if (err) {
if (callbacks.onError) {
callbacks.onError(err);
}
}
if (!_.isObject(res)) {
finish();
err = new Error('Unrecognized return format');
if (callbacks.onError) {
callbacks.onError(err);
}
}
if (_.isEmpty(res)) {
if (callbacks.onReady) {
callbacks.onReady();
}
finish();
return;
}
var localCollections = Meteor.connection._mongo_livedata_collections;
for (collectionName in res) {
var docs = res[collectionName];
var collection = localCollections[collectionName];
//We do not handle collection that does not exist on the client.
if (!collection) {
console.error('Collection "' + collectionName + '" does not exist in local collection.');
continue;
}
if (! _.isArray(docs)) {
console.error('Unrecognized document format.');
continue;
}
docs.forEach(function (doc) {
if (!doc) {
return;
}
if (!doc._id) {
console.error('Return document does not contain _id field.');
return;
}
doc.subCacheCached = true;
collection.upsert({_id: doc._id}, doc);
});
};
if (callbacks.onReady) {
callbacks.onReady();
}
finish();
return;
});
}
return handle;
};
/*
* We don't want to interfere with the regular Meteor pub/sub, when application
* subscribe to the same data that Meteor is not aware it exists on the client
* already. We will override the msg from 'added' to 'changed'.
*
* This solution enables to cache the initial data set early. And application
* can 'turn' those data into 'live data' with regular pubsub.
*
*/
var originalLiveDataData = Meteor.default_connection._livedata_data;
Meteor.default_connection._livedata_data = function (msg) {
var serverDoc = Meteor._get(this._serverDocuments, msg.collection, msg.id);
if (!serverDoc && msg.msg == 'added') {
var localCollection = Meteor.default_connection._mongo_livedata_collections[msg.collection];
if (localCollection) {
var existingDoc = localCollection.findOne(msg.id);
if (existingDoc) {
//use deep extend from jquery. It will be recursively merged.
//XXX This could be a performance issue.
msg.fields = $.extend(true, existingDoc, msg.fields);
delete msg.fields._id;
msg.msg = "changed";
}
}
}
// If we get a remove msg, check if the doc is cached or not. For all cached documents we have set the key
// subCacheCached = true. Just don't apply the msg and thats it.
if (!serverDoc && msg.msg == 'removed') {
var localCollection = Meteor.default_connection._mongo_livedata_collections[msg.collection];
if (localCollection) {
var existingDoc = localCollection.findOne({_id: msg.id, subCacheCached: true });
if (existingDoc) {
return;
}
}
}
return originalLiveDataData.call(this, msg);
};