forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
148 lines (124 loc) · 3.49 KB
/
common.js
File metadata and controls
148 lines (124 loc) · 3.49 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
/**
* 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 Common calls available to both prod and static code.
*/
export const preloadEvent = '__santa-preload';
export const playEvent = '__santa-play';
export const goEvent = '__santa-go';
export const internalSoundPreload = '__santa-klang-preload';
/**
* Play the audio globally.
*
* @param {...string} args
*/
export function play(...args) {
const ce = new CustomEvent(playEvent, {detail: args});
window.dispatchEvent(ce);
}
/**
* Route to the specified page.
*
* @param {string} route
*/
export function go(route) {
const ce = new CustomEvent(goEvent, {detail: route});
window.dispatchEvent(ce);
}
const wait = (function() {
const tasks = [];
let announce = null;
return (...all) => {
tasks.push(...all);
if (announce !== null || !all.length) {
return; // done
}
// Otherwise, announce after a microtask.
announce = Promise.resolve().then(() => {
announce = null;
const detail = tasks.splice(0, tasks.length); // empty tasks and send copy
const ce = new CustomEvent(preloadEvent, {detail});
window.dispatchEvent(ce);
});
};
}());
export const preload = Object.freeze({
/**
* Waits for the passed promises to resolve before loading.
*
* @param {...!Promise<*>} all
*/
wait,
/**
* Preloads the passed images.
*
* @param {...string} all image URLs to preload
* @return {!Array<!Promise<!Image>>} resolved image instances
*/
images(...all) {
const safe = [];
const preloads = all.map((src) => {
const image = new Image();
image.src = src;
const out = new Promise((resolve, reject) => {
image.onload = () => resolve(image);
image.onerror = reject;
});
safe.push(out.catch(() => image));
return out;
})
wait(...preloads);
// Return the result Image instances, even if they fail to load.
return safe;
},
/**
* Preloads the passed assets (including failure to load).
*
* @param {...(string|!Image)} all
*/
assets(...all) {
const preloads = all.map((cand) => {
let asset;
let loadEvent = 'load';
if (typeof cand === 'string') {
// TODO(samthor): .mp3/.ogg etc
asset = new Image();
asset.src = cand;
} else if (cand instanceof Image) {
asset = cand;
} else if (cand instanceof HTMLMediaElement) {
asset = cand;
loadEvent = 'canplaythrough';
} else {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
asset.addEventListener(loadEvent, () => resolve(asset), {once: true});
asset.addEventListener('error', reject, {once: true});
});
});
wait(...preloads);
},
/**
* Preloads the given Klang groups.
*
* @param {...string} groups
*/
sounds(...groups) {
const ce = new CustomEvent(internalSoundPreload, {detail: groups});
window.dispatchEvent(ce);
},
});