-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
142 lines (123 loc) · 4.47 KB
/
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
console.error("elevatedevdesign:template-subscriptions is not compatible with >= meteor@1.4.0 please remove to upgrade");
TemplateSubscriptionsImpl = function(){
};
TemplateSubscriptionsImpl.prototype.init = function(){
this._blazeFireCallbacks = Blaze._fireCallbacks;
/*
* Add helper to determine when subscriptions are ready
*/
UI.registerHelper('subscriptionsReady', function( s){
if( UI._templateInstance().view.template._subscriptionsReady !== undefined ){
return UI._templateInstance().view.template._subscriptionsReady.get();
}else{
return true;
}
});
/*
* Inject ourselves into blaze to get callbacks
*/
Blaze._fireCallbacks = function(view, which){
if( which === "created" ){
TemplateSubscriptions._createdCallback(view);
}
if( which === "destroyed" ){
TemplateSubscriptions._destroyedCallback(view);
}
TemplateSubscriptions._blazeFireCallbacks(view, which);
};
var self = this;
Blaze.TemplateInstance.prototype.subscriptionsChanged = function(){
var view = this.view;
var newSubsValue = view.template.subscriptions();
self._cacheEach( view, _.map(view.template._subscriptionMap, function(sub){ return sub.args;}) );
self._unsubscribeEach( view, _.map(view.template._subscriptionMap, function(sub){ return sub.args;}) );
self._subscribeEach( view, newSubsValue );
};
};
TemplateSubscriptionsImpl.prototype._calcHash = function( sub ){
return EJSON.stringify(sub);
};
/*
* Setup variables used in template
*/
TemplateSubscriptionsImpl.prototype._initTemplate = function(view){
var self = this;
if( view.template && view.template.subscriptions ){
view.template._subscriptionList = view.template.subscriptions();
view.template._subscriptionMap = {};
view.template._subscriptionsReady = new ReactiveVar( view.template._subscriptionList.length === 0 );
}
};
TemplateSubscriptionsImpl.prototype._setSubscriptionState = function( view, sub, state ){
var hash = this._calcHash( sub );
var template = view.template;
if( template._subscriptionMap[hash] ){
template._subscriptionMap[hash].state = state;
template._subscriptionMap[hash].args = sub;
}else{
template._subscriptionMap[hash] = {state: state, args: sub};
}
var allTrue = true;
_.forEach(view.template._subscriptionMap, function(sub ){
if( !view.template._subscriptionMap[hash].state ){
allTrue = false;
return;
}
});
view.template._subscriptionsReady.set( allTrue );
};
/*
* Helpers for subscribe list of arg lists, unsubscribe list and add to cache manager
*/
TemplateSubscriptionsImpl.prototype._subscribeEach = function( view, subs ){
var self = this;
var template = view.template;
_.forEach(subs, function( sub ){
var hash = self._calcHash(sub);
var subArray = sub;
var readyCallback = function( ){ self._setSubscriptionState( view, subArray, true ); };
// clone the sub args array
sub = sub.slice(0);
self._setSubscriptionState( view, sub, false );
sub.push( readyCallback );
template._subscriptionMap[hash].subscription = Meteor.subscribe.apply( Meteor, sub );
});
};
TemplateSubscriptionsImpl.prototype._unsubscribeEach = function( view, subs ){
var self = this;
_.forEach(subs, function( sub ){
var hash = self._calcHash( sub );
if( view.template._subscriptionMap[hash] && view.template._subscriptionMap[hash].subscription ){
view.template._subscriptionMap[hash].subscription.stop();
delete view.template._subscriptionMap[hash];
}
});
};
TemplateSubscriptionsImpl.prototype._cacheEach = function( view, subs ){
var cacheManager = view.template.cacheManager ? view.template.cacheManager : self.cacheManager;
if( cacheManager !== undefined ){
_.forEach(subs, function( sub ){
cacheManager.subscribe.apply( cacheManager, sub );
});
}
};
/*
* Callbacks on blaze templates
*/
/*
* Call init and setup subscriptions for template
*/
TemplateSubscriptionsImpl.prototype._createdCallback = function(view){
this._initTemplate( view );
if( view.template && view.template.subscriptions ){
this._subscribeEach(view,view.template._subscriptionList);
}
};
TemplateSubscriptionsImpl.prototype._destroyedCallback = function(view){
if( view.template && view.template._subscriptions ){
this._cacheEach( view );
this._unsubscribeEach(view, _.map(view.template._subscriptionMap,function(sub){return sub.args;}));
}
};
TemplateSubscriptions = new TemplateSubscriptionsImpl();
TemplateSubscriptions.init();