forked from jamielob/reloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reloader.js
227 lines (158 loc) · 5.02 KB
/
reloader.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const launchScreen = LaunchScreen.hold();
Reloader = {
_options: {},
configure(options) {
check(options, {
check: Match.Optional(Match.OneOf('everyStart', 'firstStart', false)),
checkTimer: Match.Optional(Match.Integer),
refresh: Match.Optional(Match.OneOf('startAndResume', 'start', 'instantly')),
idleCutoff: Match.Optional(Match.Integer),
launchScreenDelay: Match.Optional(Match.Integer),
});
_.extend(this._options, options);
},
updateAvailable: new ReactiveVar(false),
prereload() {
// Show the splashscreen
navigator.splashscreen.show();
// Set the refresh flag
localStorage.setItem('reloaderWasRefreshed', Date.now());
},
reload() {
this.prereload()
// https://github.com/meteor/meteor/blob/devel/packages/reload/reload.js#L220
if (window.location.hash || window.location.href.endsWith("#")) {
window.location.reload();
} else {
window.location.replace(window.location.href);
}
},
// Should check if a cold start and (either everyStart is set OR firstStart
// is set and it's our first start)
_shouldCheckForUpdateOnStart() {
const isColdStart = !localStorage.getItem('reloaderWasRefreshed');
return isColdStart &&
(
this._options.check === 'everyStart' ||
(
this._options.check === 'firstStart' &&
!localStorage.getItem('reloaderLastStart')
)
);
},
// Check if the idleCutoff is set AND we exceeded the idleCutOff limit AND the everyStart check is set
_shouldCheckForUpdateOnResume() {
// In case a pause event was missed, assume it didn't make the cutoff
if (!localStorage.getItem('reloaderLastPause')) {
return false;
}
// Grab the last time we paused
const lastPause = Number(localStorage.getItem('reloaderLastPause'));
// Calculate the cutoff timestamp
const idleCutoffAt = Number( Date.now() - this._options.idleCutoff );
return (
this._options.idleCutoff &&
lastPause < idleCutoffAt &&
this._options.check === 'everyStart'
);
},
_waitForUpdate(computation) {
// Check if we have a HCP after the check timer is up
Meteor.setTimeout(() => {
// If there is a new version available
if (this.updateAvailable.get()) {
this.reload();
} else {
// Stop waiting for update
if (computation) {
computation.stop()
}
launchScreen.release();
}
}, this._options.checkTimer );
},
_checkForUpdate() {
if (this.updateAvailable.get()) {
// Check for an even newer update
this._waitForUpdate()
} else {
// Wait until update is available, or give up on timeout
Tracker.autorun((c) => {
if (this.updateAvailable.get()) {
this.reload();
}
this._waitForUpdate(c)
});
}
},
_onPageLoad() {
if (this._shouldCheckForUpdateOnStart()) {
this._checkForUpdate();
} else {
Meteor.setTimeout(function() {
launchScreen.release();
// Reset the reloaderWasRefreshed flag
localStorage.removeItem('reloaderWasRefreshed');
}, this._options.launchScreenDelay); // Short delay helps with white flash
}
},
_onResume() {
const shouldCheck = this._shouldCheckForUpdateOnResume();
localStorage.removeItem('reloaderLastPause');
if (shouldCheck) {
navigator.splashscreen.show();
this._checkForUpdate();
// If we don't need to do an additional check
} else {
// Check if there's a new version available already AND we need to refresh on resume
if ( this.updateAvailable.get() && this._options.refresh === 'startAndResume' ) {
this.reload();
}
}
},
// https://github.com/meteor/meteor/blob/devel/packages/reload/reload.js#L104-L122
_onMigrate(retry) {
if (this._options.refresh === 'instantly') {
this.prereload()
return [true, {}];
} else {
// Set the flag
this.updateAvailable.set(true);
// Don't refresh yet
return [false];
}
}
};
// Set the defaults
Reloader.configure({
check: 'everyStart',
checkTimer: 3000,
refresh: 'startAndResume',
idleCutoff: 1000 * 60 * 10, // 10 minutes
launchScreenDelay: 100
});
Reloader._onPageLoad();
// Set the last start flag
localStorage.setItem('reloaderLastStart', Date.now());
// Watch for the app resuming
document.addEventListener("resume", function () {
Reloader._onResume();
}, false);
localStorage.removeItem('reloaderLastPause');
// Watch for the device pausing
document.addEventListener("pause", function() {
// Save to localStorage
localStorage.setItem('reloaderLastPause', Date.now());
}, false);
// Capture the reload
Reload._onMigrate('jamielob:reloader', function (retry) {
return Reloader._onMigrate(retry);
});
// Update available template helper
Template.registerHelper("updateAvailable", function() {
return Reloader.updateAvailable.get();
});
// Update available event
$(document).on('click', '[reloader-update]', function(event) {
Reloader.reload();
});