-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathfast_render.js
99 lines (82 loc) · 3.15 KB
/
fast_render.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
FastRender = {};
FastRender.enabled = typeof __fast_render_config != 'undefined';
FastRender._dataReceived = false;
FastRender._revertedBackToOriginal = false;
FastRender._blockDDP = Meteor._localStorage.getItem('__frblockddp') != undefined;
if(FastRender._blockDDP) {
console.log("FastRender is blocking DDP messages. apply 'FastRender.debugger.unblockDDP()' to unblock again.");
}
FastRender._disable = Meteor._localStorage.getItem('__frdisable') != undefined;
if(FastRender._disable) {
console.log("FastRender is disabled. apply 'FastRender.debugger.enableFR()' to enable it back.")
}
// This allow us to apply DDP message even if Meteor block accepting messages
// When doing initial login, Meteor sends an login message
// Then it'll block the accpeting DDP messages from server
// This is the cure
FastRender.injectDdpMessage = function(conn, message) {
FastRender["debugger"].log('injecting ddp message:', message);
// Removed check for conn._bufferedWrites due to https://github.com/kadirahq/fast-render/pull/167/files#r74189260
// and https://github.com/kadirahq/fast-render/issues/176
var originalWait = conn._waitingForQuiescence;
conn._waitingForQuiescence = function() {return false};
conn._livedata_data(message);
conn._waitingForQuiescence = originalWait;
};
FastRender.init = function(payload) {
if(FastRender._disable) return;
FastRender._securityCheck(payload);
FastRender._subscriptions = (payload && payload.subscriptions) || {};
FastRender._subscriptionIdMap = {};
FastRender._dataReceived = true;
FastRender._payload = payload;
// merging data from different subscriptions
// yes, this is a minimal mergeBox on the client
var allData = {};
if (payload) {
_.each(payload.collectionData, function(subData, collName) {
if(!allData[collName]) {
allData[collName] = {};
}
collData = allData[collName];
subData.forEach(function(dataSet) {
dataSet.forEach(function(item) {
if(!collData[item._id]) {
collData[item._id] = item;
} else {
DeepExtend(collData[item._id], item);
}
});
});
});
}
var connection = Meteor.connection;
_.each(allData, function(collData, collName) {
_.each(collData, function(item, id) {
var id = IDTools.idStringify(item._id);
delete item._id;
var ddpMessage = {
msg: 'added',
collection: collName,
id: id,
fields: item,
frGen: true
};
FastRender.injectDdpMessage(connection, ddpMessage);
});
});
// If the connection supports buffered DDP writes, then flush now.
if (connection._flushBufferedWrites) connection._flushBufferedWrites();
// let Meteor know, user login process has been completed
if(typeof Accounts != 'undefined') {
Accounts._setLoggingIn(false);
}
};
FastRender._securityCheck = function(payload) {
if(payload && payload.loginToken) {
var localStorageLoginToken = Meteor._localStorage.getItem('Meteor.loginToken');
if(localStorageLoginToken != payload.loginToken) {
throw new Error("seems like cookie tossing is happening. visit here: http://git.io/q4IRHQ");
}
}
};