-
Notifications
You must be signed in to change notification settings - Fork 2
/
SubsManager.ts
194 lines (169 loc) · 5.02 KB
/
SubsManager.ts
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
import { Meteor } from "meteor/meteor";
import { Tracker } from "meteor/tracker";
interface Sub {
args: Parameters<typeof Meteor.subscribe>;
hash: string;
ready?: boolean;
updated?: number;
identifier?: any[];
}
class SubsManager {
cacheLimit: number;
expireIn: number;
_ready: boolean;
dep: Tracker.Dependency;
_cacheList: Sub[];
_cacheMap: Record<string, Sub>;
computation: Tracker.Computation;
constructor() {
// maxiumum number of subscriptions are cached
this.cacheLimit ||= 10;
// maximum time, subscription stay in the cache
this.expireIn ||= 50;
this._cacheMap = {};
this._cacheList = [];
this._ready = false;
this.dep = new Tracker.Dependency();
this.computation = this._registerComputation();
}
subscribe(...args: Parameters<typeof Meteor.subscribe>) {
if (Meteor.isClient) {
this._addSub(args);
return {
ready: () => {
this.dep.depend();
return this._ready;
},
stop: () => {},
};
}
return Meteor.subscribe(...args);
}
_addSub(args: Parameters<typeof Meteor.subscribe>) {
const hash = JSON.stringify(args);
if (!this._cacheMap[hash]) {
const sub = { args, hash };
this._handleError(sub);
this._cacheMap[hash] = sub;
this._cacheList.push(sub);
this._ready = false;
// to notify the global ready()
this._notifyChanged();
// no need to interfere with the current computation
this._reRunSubs();
}
// add the current sub to the top of the list
const sub = this._cacheMap[hash]!;
sub.updated = new Date().getTime();
this._cacheList.splice(this._cacheList.indexOf(sub), 1);
this._cacheList.push(sub);
}
_reRunSubs() {
if (Tracker.currentComputation) {
Tracker.afterFlush(() => {
this.computation.invalidate();
});
} else {
this.computation.invalidate();
}
}
_notifyChanged() {
if (Tracker.currentComputation) {
setTimeout(() => this.dep.changed(), 0);
} else {
this.dep.changed();
}
}
_applyCacheLimit() {
const overflow = this._cacheList.length - this.cacheLimit;
if (overflow > 0) {
this._cacheList
.splice(0, overflow)
.forEach((sub) => delete this._cacheMap[sub.hash]);
}
}
_applyExpirations() {
const newCacheList: (typeof this)["_cacheList"] = [];
const expirationTime = new Date().getTime() - this.expireIn * 60 * 1000;
this._cacheList.forEach((sub) => {
if (sub.updated && sub.updated >= expirationTime) {
newCacheList.push(sub);
} else {
delete this._cacheMap[sub.hash];
}
});
this._cacheList = newCacheList;
}
_registerComputation() {
return Tracker.autorun(() => {
this._applyExpirations();
this._applyCacheLimit();
let ready = true;
this._cacheList.forEach((sub) => {
sub.ready = Meteor.subscribe(...sub.args).ready();
ready = ready && sub.ready;
});
if (ready) {
this._ready = true;
this._notifyChanged();
}
});
}
_createIdentifier = (args: Sub["args"]) =>
args.map((value) => (typeof value == "string" ? '"' + value + '"' : value));
_handleError(sub: Sub) {
const args = sub.args;
const lastElement = args[args.length - 1];
sub.identifier = this._createIdentifier(args);
if (!lastElement) {
args.push({ onError: errorHandlingLogic });
} else if (typeof lastElement == "function") {
args.pop();
args.push({ onReady: lastElement, onError: errorHandlingLogic });
} else if (typeof lastElement.onError == "function") {
const originalOnError = lastElement.onError;
lastElement.onError = (err: any) => {
errorHandlingLogic(err);
originalOnError(err);
};
} else if (typeof lastElement.onReady == "function") {
lastElement.onError = errorHandlingLogic;
} else {
args.push({ onError: errorHandlingLogic });
}
function errorHandlingLogic(err: any) {
console.log(
"Error invoking SubsManager.subscribe(%s): ",
sub.identifier,
err.reason,
);
// expire this sub right away.
// Then expiration machanism will take care of the sub removal
sub.updated = new Date(1).getTime();
}
}
reset() {
const oldComputation = this.computation;
this.computation = this._registerComputation();
// invalidate the new compuation and it will fire new subscriptions
this.computation.invalidate();
// after above invalidation completed, fire stop the old computation
// which then send unsub messages
// mergeBox will correct send changed data and there'll be no flicker
Tracker.afterFlush(() => {
oldComputation.stop();
});
}
clear() {
this._cacheList = [];
this._cacheMap = {};
this._reRunSubs();
}
ready() {
this.dep.depend();
// if there are no items in the cacheList we are not ready yet.
if (this._cacheList.length === 0) return false;
return this._ready;
}
}
export default new SubsManager();