forked from pathable/reloader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
reloader-cordova.js
294 lines (249 loc) · 8.26 KB
/
reloader-cordova.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import {Meteor} from 'meteor/meteor';
import {Tracker} from 'meteor/tracker';
import {ReactiveVar} from 'meteor/reactive-var';
import {LaunchScreen} from 'meteor/launch-screen';
import {settings, debugFn, PACKAGE_NAME} from './common';
const DEFAULT_OPTIONS = {
idleCutoff: 1000 * 60 * 5, // 5 minutes
automaticInitialization: true,
launchScreenDelay: 0,
};
debugFn('starting - DEFAULT_OPTIONS', {DEFAULT_OPTIONS});
debugFn('starting - settings', {settings});
const options = Object.assign({}, DEFAULT_OPTIONS, settings);
debugFn('starting - options', {options});
const defaultRetry = () => console.log(`[${PACKAGE_NAME}] no retry function yet`);
const launchScreen = LaunchScreen.hold();
let initialized = false;
const Reloader = {
_options: {},
// eslint-disable-next-line no-console
_retry: defaultRetry,
updateAvailable: new ReactiveVar(false),
isChecked: new ReactiveVar(false),
debug(message, context) {
debugFn(message, {
...context,
updateAvailable: this.updateAvailable.get(),
isChecked: this.isChecked.get(),
_options: this._options,
});
},
initialize(optionsParam = {}) {
if (initialized) {
return;
}
initialized = true;
this._options = Object.assign({}, options, optionsParam);
this._onPageLoad();
},
prepareToReload() {
this.debug('prereload - show splashscreen');
// Show the splashscreen
navigator.splashscreen.show();
const currentDate = Date.now();
this.debug('prereload - reloaderWasRefreshed', {currentDate});
// Set the refresh flag
localStorage.setItem('reloaderWasRefreshed', currentDate);
},
reloadNow() {
this.debug('reloadNow');
if (this._isCheckBeforeReload() && !this.isChecked.get()) {
this.debug(
'not reloading because beforeReload is provided and it is not checked yet'
);
return;
}
this.prepareToReload();
// We'd like to make the browser reload the page using location.replace()
// instead of location.reload(), because this avoids validating assets
// with the server if we still have a valid cached copy. This doesn't work
// when the location contains a hash however, because that wouldn't reload
// the page and just scroll to the hash location instead.
if (window.location.hash || window.location.href.endsWith('#')) {
this.debug('reloadNow - reload');
window.location.reload();
} else {
this.debug('reloadNow - replace');
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() {
this.debug('_shouldCheckForUpdateOnStart');
const isColdStart = !localStorage.getItem('reloaderWasRefreshed');
const reloaderLastStart = localStorage.getItem('reloaderLastStart');
this.debug('_shouldCheckForUpdateOnStart - info', {
isColdStart,
check: this._options.check,
reloaderLastStart,
});
const should =
isColdStart;
this.debug('_shouldCheckForUpdateOnStart - should', {should});
return should;
},
// Check if the idleCutoff is set AND we exceeded the idleCutOff limit AND the everyStart check is set
_shouldCheckForUpdateOnResume() {
this.debug('_shouldCheckForUpdateOnResume');
const reloaderLastPause = localStorage.getItem('reloaderLastPause');
// In case a pause event was missed, assume it didn't make the cutoff
if (!reloaderLastPause) {
this.debug('_shouldCheckForUpdateOnResume no reloaderLastPause');
return false;
}
// Grab the last time we paused
const lastPause = Number(reloaderLastPause);
// Calculate the cutoff timestamp
const idleCutoffAt = Number(Date.now() - this._options.idleCutoff);
this.debug('_shouldCheckForUpdateOnResume - info', {
idleCutoff: this._options.idleCutoff,
check: this._options.check,
lastPause,
idleCutoffAt,
});
return (
this._options.idleCutoff &&
lastPause < idleCutoffAt
);
},
_waitForUpdate(computation) {
this.debug('_waitForUpdate');
// 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.debug('_waitForUpdate - reloadNow');
this.reloadNow();
} else {
// Stop waiting for update
if (computation) {
computation.stop();
}
this.debug('prereload - release launchScreen');
launchScreen.release();
if (
!navigator ||
!navigator.splashscreen ||
!navigator.splashscreen.hide
) {
console.warn(
`[${PACKAGE_NAME}] navigator.splashscreen.hide not available`
);
return;
}
this.debug('prereload - hide splashscreen');
navigator.splashscreen.hide();
}
}, 0);
},
_checkForUpdate() {
this.debug('_checkForUpdate');
if (this.updateAvailable.get()) {
// Check for an even newer update
this.debug('_checkForUpdate - 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.debug('_checkForUpdate - reloadNow');
this.reloadNow();
}
this._waitForUpdate(c);
});
}
},
_onPageLoad() {
this.debug('_onPageLoad');
if (this._shouldCheckForUpdateOnStart()) {
this._checkForUpdate();
} else {
Meteor.setTimeout(() => {
this.debug('_onPageLoad - release launchScreen');
launchScreen.release();
// Reset the reloaderWasRefreshed flag
localStorage.removeItem('reloaderWasRefreshed');
}, this._options.launchScreenDelay); // Short delay helps with white flash
}
},
_onResume() {
this.debug('_onResume');
const shouldCheck = this._shouldCheckForUpdateOnResume();
localStorage.removeItem('reloaderLastPause');
if (shouldCheck) {
// Show the splashscreen if available (this has been causing issues on iOS)
if (navigator && navigator.splashscreen && navigator.splashscreen.show) {
this.debug('_onResume - show splashscreen');
navigator.splashscreen.show();
}
this._checkForUpdate();
return;
}
},
_isCheckBeforeReload() {
this.debug('_isCheckBeforeReload');
return !!this._options.beforeReload && typeof this._options.beforeReload === 'function';
},
_callBeforeLoad() {
this.debug('_callBeforeLoad');
const updateApp = () => {
this.debug('_callBeforeLoad set isChecked to true');
this.isChecked.set(true);
this._retry();
};
const holdAppUpdate = () => {
this.debug('_callBeforeLoad set isChecked to false');
this.isChecked.set(false);
};
this._options.beforeReload(updateApp, holdAppUpdate);
},
// https://github.com/meteor/meteor/blob/devel/packages/reload/reload.js#L104-L122
_onMigrate(retry) {
this.debug('_onMigrate');
this._retry = retry || this._retry;
if (!this._isCheckBeforeReload() || this.isChecked.get()) {
// we are calling prepareToReload because as we are returning true the reload
// will happen in the reload package then we are updating our timestamps
this.prepareToReload();
this.isChecked.set(false);
return [true, {}];
}
if (this._isCheckBeforeReload()) {
this._callBeforeLoad();
}
// Set the flag
this.updateAvailable.set(true);
// Don't refresh yet
return [false];
},
};
if (options.automaticInitialization) {
Reloader.initialize();
}
// Set the last start flag
localStorage.setItem('reloaderLastStart', Date.now());
// Watch for the app resuming
document.addEventListener(
'resume',
() => {
Reloader._onResume();
},
false
);
localStorage.removeItem('reloaderLastPause');
// Watch for the device pausing
document.addEventListener(
'pause',
() => {
// Save to localStorage
localStorage.setItem('reloaderLastPause', Date.now());
},
false
);
// Capture the reload
// import { Reload } from 'meteor/reload' is not working
// eslint-disable-next-line no-undef
Reload._onMigrate(`${PACKAGE_NAME}`, retry => Reloader._onMigrate(retry));
export {Reloader};