forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
388 lines (319 loc) · 8.8 KB
/
Copy pathconfig.js
File metadata and controls
388 lines (319 loc) · 8.8 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Provides Firebase Remote config. This has side-effects.
*
* This should only run on the prod domain. This assumes that "firebase" is a global already
* available in the global scope.
*/
import {frame} from '../lib/promises.js';
import {localStorage} from '../storage.js';
const firebase = window.firebase;
const remoteConfig = firebase.remoteConfig();
const listeners = new Set();
const memoized = {
'videos': [],
'sceneLock': {},
'sceneRedirect': {},
'nav': [],
'featured': {},
};
const isProd = (window.location.hostname === 'santatracker.google.com');
let isOutOfDate = false;
/**
* @return {boolean} whether the site is out of date and needs a reload
*/
export function siteExpired() {
return isOutOfDate;
}
function checkUpgrade() {
const siteVersion = window.santaConfig.version;
const upgradeToVersion = remoteConfig.getString('upgradeToVersion');
if (!upgradeToVersion || upgradeToVersion < siteVersion) {
isOutOfDate = false;
delete localStorage['upgradeToVersion'];
return; // nothing to do
}
// otherwise, reload or complain
console.warn('got out-of-date version, have', siteVersion, 'want', upgradeToVersion);
if (!isProd || localStorage['upgradeToVersion'] === upgradeToVersion) {
ga('send', 'event', 'site', 'upgrade-warn', upgradeToVersion);
isOutOfDate = true;
notifyListeners();
} else {
ga('send', 'event', 'site', 'upgrade-attempt', upgradeToVersion);
localStorage['upgradeToVersion'] = upgradeToVersion;
window.location.href = window.location.href;
}
}
checkUpgrade();
window._check = checkUpgrade;
function refreshMemoized() {
for (const key in memoized) {
let cand;
try {
cand = JSON.parse(remoteConfig.getString(key));
} catch (e) {
continue; // do nothing
}
memoized[key] = Object.freeze(cand);
}
}
refreshMemoized();
function expontentialDelay(range, failures = 0) {
return Math.pow(1.0 + (range * Math.random()), failures + 1) - (range / 2);
}
/*
* Queues up a refresh every 'refreshEvery' time.
*/
(function() {
let failedRefreshCount = 0;
let refreshTimeout = 0;
async function runRefresh() {
try {
await refresh();
failedRefreshCount = 0;
} catch (e) {
++failedRefreshCount;
console.warn('failed to refresh Remote Config', err);
}
queueRefresh();
}
function queueRefresh() {
// For sanity, ensure that refresh only happens every 60 seconds.
const refreshEveryMinimum = 60;
const seconds = Math.max(refreshEveryMinimum, remoteConfig.getNumber('refreshEvery') || 0);
const delay = 1000 * seconds * expontentialDelay(0.2, failedRefreshCount);
window.clearTimeout(refreshTimeout);
// Values become "invalid" after half this time. This probably doesn't matter because we're not
// triggering a fetch anywhere else?
remoteConfig.settings.minimumFetchIntervalMillis = delay / 2;
// Delay by ~seconds +/- 10%, but only enact a new fetch if the window is in the foreground.
const localTimeout = window.setTimeout(() => {
window.requestAnimationFrame(() => {
if (localTimeout !== refreshTimeout) {
return; // preempted before rAF occurred
}
runRefresh();
});
}, delay);
refreshTimeout = localTimeout;
}
queueRefresh();
})();
/**
* Adds a listener to the config.
*
* @param {function(!Object): void} callback
*/
export function listen(callback) {
listeners.add(callback);
}
/**
* Removes a listener from the config.
*
* @param {function(!Object): void)} callback
*/
export function remove(callback) {
listeners.delete(callback);
}
/**
* Notify all listeners that something has changed.
*/
function notifyListeners() {
const nonce = new Object();
listeners.forEach((listener) => {
Promise.resolve().then(() => listener(nonce));
});
}
/**
* Notifies all listeners on local device date change. Useful for locking or unlocking scenes.
*/
(function() {
let previousDate = (new Date()).toDateString();
function checkDate() {
// check every ~minute on rAF
const next = 60 * 1000 * expontentialDelay(0.2);
frame(next).then(checkDate);
const currentDate = (new Date()).toDateString();
if (currentDate !== previousDate) {
previousDate = currentDate;
notifyListeners();
}
}
checkDate();
})();
/**
* Performs a refresh of Remote Config. This might be a no-op if the values are yet to be marked
* invalid.
*
* @param {boolean=} invalidateNow whether to invalidate all values immediately
* @return {!Promise<!Object<string, *>>}
*/
export async function refresh(invalidateNow = false) {
if (invalidateNow) {
remoteConfig.settings.minimumFetchIntervalMillis = 0;
}
await remoteConfig.fetchAndActivate();
refreshMemoized();
checkUpgrade();
// nb. It's not clear RC tells us if it changes or not. Just notify everyone anyway.
notifyListeners();
return values();
}
/**
* @return {!Object<string, *>}
*/
export function values() {
return remoteConfig.getAll();
}
/**
* @return {boolean} whether to switch off and load an error page.
*/
export function switchOff() {
return remoteConfig.getBoolean('switchOff');
}
/**
* @return {?string} today's featured route
*/
export function featuredRoute() {
const today = new Date();
if (today.getMonth() !== 11) {
return null;
}
return memoized.featured[`${today.getDate()}`] || null;
}
/**
* @return {!Object} misc tracker flags, subset of Firebase config
*/
export function trackerFlags() {
return {
routeUrl: remoteConfig.getString('routeUrl'),
showTracker: remoteConfig.getBoolean('showTracker'),
useGeoIP: remoteConfig.getBoolean('useGeoIP'),
routeJitter: remoteConfig.getNumber('routeJitter'),
};
}
/**
* @return {string} featureCard
*/
export function loudCard() {
return remoteConfig.getString('loudCard');
}
/**
* @param {?string} route to check
* @param {boolean} fallback whether we are fallback mode
* @return {?string} route to serve
*/
export function sceneForRoute(route, fallback) {
if (!route || route === 'index') {
return indexScene(fallback);
}
// Actually lock scenes in prod.
if (isLocked(route)) {
if (isProd) {
return null;
}
console.debug('failing open for scene locked in prod', route);
}
const v = videos();
if (v.indexOf(route) !== -1) {
return 'video';
}
return route;
}
/**
* @param {string} route to redirect
* @return {string|undefined} optional updated route
*/
export function redirectRoute(route) {
if (route in memoized.sceneRedirect) {
return memoized.sceneRedirect[route];
}
if (route === 'index') {
return '';
}
}
/**
* @return {!Array<string>} video IDs
*/
export function videos() {
return memoized.videos;
}
/**
* @return {!Array<string>} nav bar scene contents
*/
export function nav() {
return memoized.nav;
}
/**
* @param {string} route to check if locked
* @return {boolean} if locked
*/
export function isLocked(route) {
const {sceneLock} = memoized;
if (!(route in sceneLock)) {
return false;
}
const value = sceneLock[route];
if (!value) {
return true; // always locked
}
const today = new Date();
if (today.getMonth() === 10) {
return true; // locked throughout Nov
} else if (today.getMonth() !== 11) {
return false; // nothing locked outside Nov-Dec
}
return value > today.getDate();
}
export function routesSnapshot() {
const {sceneLock, videos} = memoized;
const out = {};
for (const route in sceneLock) {
out[route] = {
locked: lockedTo(route),
video: false,
};
}
videos.forEach((video) => {
if (!(video in out)) {
out[video] = {
locked: undefined,
};
}
out[video].video = true;
});
return out;
}
/**
* @param {string} route to return date locked until
* @return {number|undefined} date within Dec locked until (0 for always)
*/
export function lockedTo(route) {
if (!isLocked(route)) {
return undefined;
}
const {sceneLock} = memoized;
return sceneLock[route];
}
/**
* @param {boolean} fallback whether to show old codebase
* @return {string} the scene to show for "/" or "index"
*/
export function indexScene(fallback) {
return remoteConfig.getString(fallback ? 'fallbackIndexScene' : 'indexScene') || 'index';
}