-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathssr_context.js
66 lines (56 loc) · 1.49 KB
/
ssr_context.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
const deepMerge = require('deepmerge');
SsrContext = class {
constructor() {
this._html = '';
this._head = '';
this._collections = {};
}
getCollection(collName) {
let collection = this._collections[collName];
if (!collection) {
const minimongo = Package.minimongo;
collection = this._collections[collName] = new minimongo.LocalCollection();
}
return collection;
}
setHtml(html) {
this._html = html;
}
getHtml() {
return this._html;
}
addToHead(headHtml) {
this._head += `\n${headHtml}`;
}
getHead() {
return this._head;
}
addSubscription(name, params) {
const fastRenderContext = FastRender.frContext.get();
if (!fastRenderContext) {
throw new Error(
`Cannot add a subscription: ${name} without FastRender Context`
);
}
const args = [name].concat(params);
const data = fastRenderContext.subscribe(...args);
this.addData(data);
}
addData(data) {
_.each(data, (collDataCollection, collectionName) => {
const collection = this.getCollection(collectionName);
collDataCollection.forEach((collData) => {
collData.forEach((item) => {
const existingDoc = collection.findOne(item._id);
if (existingDoc) {
const newDoc = deepMerge(existingDoc, item);
delete newDoc._id;
collection.update(item._id, newDoc);
} else {
collection.insert(item);
}
});
});
});
}
};