1 | import { callObjectBundleUrl } from './utils';
|
2 |
|
3 | function prepareDailyConfig(callFrameId) {
|
4 |
|
5 |
|
6 | if (!window._dailyConfig) {
|
7 | window._dailyConfig = {};
|
8 | }
|
9 | window._dailyConfig.callFrameId = callFrameId;
|
10 | }
|
11 |
|
12 | export default class CallObjectLoader {
|
13 | constructor() {
|
14 | this._currentLoad = null;
|
15 | }
|
16 |
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | load(meetingOrBaseUrl, callFrameId, successCallback, failureCallback) {
|
37 | if (this.loaded) {
|
38 | window._dailyCallObjectSetup(callFrameId);
|
39 | successCallback(true);
|
40 | return;
|
41 | }
|
42 |
|
43 | prepareDailyConfig(callFrameId);
|
44 |
|
45 |
|
46 | this._currentLoad && this._currentLoad.cancel();
|
47 |
|
48 |
|
49 | this._currentLoad = new LoadOperation(
|
50 | meetingOrBaseUrl,
|
51 | callFrameId,
|
52 | () => {
|
53 | successCallback(false);
|
54 | },
|
55 | failureCallback
|
56 | );
|
57 | this._currentLoad.start();
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 | cancel() {
|
64 | this._currentLoad && this._currentLoad.cancel();
|
65 | }
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 | get loaded() {
|
72 | return this._currentLoad && this._currentLoad.succeeded;
|
73 | }
|
74 | }
|
75 |
|
76 | const LOAD_ATTEMPTS = 3;
|
77 | const LOAD_ATTEMPT_DELAY = 3 * 1000;
|
78 |
|
79 | class LoadOperation {
|
80 |
|
81 |
|
82 | constructor(meetingOrBaseUrl, callFrameId, successCallback, failureCallback) {
|
83 | this._attemptsRemaining = LOAD_ATTEMPTS;
|
84 | this._currentAttempt = null;
|
85 |
|
86 | this._meetingOrBaseUrl = meetingOrBaseUrl;
|
87 | this._callFrameId = callFrameId;
|
88 | this._successCallback = successCallback;
|
89 | this._failureCallback = failureCallback;
|
90 | }
|
91 |
|
92 | start() {
|
93 |
|
94 | if (this._currentAttempt) {
|
95 | return;
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 | const retryOrFailureCallback = (errorMessage) => {
|
101 | if (this._currentAttempt.cancelled) {
|
102 |
|
103 | return;
|
104 | }
|
105 |
|
106 | this._attemptsRemaining--;
|
107 | this._failureCallback(errorMessage, this._attemptsRemaining > 0);
|
108 | if (this._attemptsRemaining <= 0) {
|
109 |
|
110 |
|
111 | return;
|
112 | }
|
113 |
|
114 | setTimeout(() => {
|
115 | if (this._currentAttempt.cancelled) {
|
116 |
|
117 | return;
|
118 | }
|
119 | this._currentAttempt = new LoadAttempt(
|
120 | this._meetingOrBaseUrl,
|
121 | this._callFrameId,
|
122 | this._successCallback,
|
123 | retryOrFailureCallback
|
124 | );
|
125 | this._currentAttempt.start();
|
126 | }, LOAD_ATTEMPT_DELAY);
|
127 | };
|
128 |
|
129 | this._currentAttempt = new LoadAttempt(
|
130 | this._meetingOrBaseUrl,
|
131 | this._callFrameId,
|
132 | this._successCallback,
|
133 | retryOrFailureCallback
|
134 | );
|
135 | this._currentAttempt.start();
|
136 | }
|
137 |
|
138 | cancel() {
|
139 | this._currentAttempt && this._currentAttempt.cancel();
|
140 | }
|
141 |
|
142 | get cancelled() {
|
143 | return this._currentAttempt && this._currentAttempt.cancelled;
|
144 | }
|
145 |
|
146 | get succeeded() {
|
147 | return this._currentAttempt && this._currentAttempt.succeeded;
|
148 | }
|
149 | }
|
150 |
|
151 | class LoadAttemptAbortedError extends Error {}
|
152 |
|
153 | const LOAD_ATTEMPT_NETWORK_TIMEOUT = 20 * 1000;
|
154 |
|
155 | class LoadAttempt {
|
156 |
|
157 |
|
158 | constructor(meetingOrBaseUrl, callFrameId, successCallback, failureCallback) {
|
159 | this.cancelled = false;
|
160 | this.succeeded = false;
|
161 |
|
162 | this._networkTimedOut = false;
|
163 | this._networkTimeout = null;
|
164 |
|
165 | this._iosCache =
|
166 | typeof iOSCallObjectBundleCache !== 'undefined' &&
|
167 | iOSCallObjectBundleCache;
|
168 | this._refetchHeaders = null;
|
169 |
|
170 | this._meetingOrBaseUrl = meetingOrBaseUrl;
|
171 | this._callFrameId = callFrameId;
|
172 | this._successCallback = successCallback;
|
173 | this._failureCallback = failureCallback;
|
174 | }
|
175 |
|
176 | async start() {
|
177 |
|
178 | const url = callObjectBundleUrl(this._meetingOrBaseUrl);
|
179 | const loadedFromIOSCache = await this._tryLoadFromIOSCache(url);
|
180 | !loadedFromIOSCache && this._loadFromNetwork(url);
|
181 | }
|
182 |
|
183 | cancel() {
|
184 | clearTimeout(this._networkTimeout);
|
185 | this.cancelled = true;
|
186 | }
|
187 |
|
188 | |
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | async _tryLoadFromIOSCache(url) {
|
199 |
|
200 |
|
201 |
|
202 | if (!this._iosCache) {
|
203 |
|
204 | return false;
|
205 | }
|
206 |
|
207 | try {
|
208 | const cacheResponse = await this._iosCache.get(url);
|
209 |
|
210 |
|
211 |
|
212 | if (this.cancelled) {
|
213 | return true;
|
214 | }
|
215 |
|
216 |
|
217 | if (!cacheResponse) {
|
218 |
|
219 | return false;
|
220 | }
|
221 |
|
222 |
|
223 |
|
224 | if (!cacheResponse.code) {
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | this._refetchHeaders = cacheResponse.refetchHeaders;
|
230 | return false;
|
231 | }
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | Function('"use strict";' + cacheResponse.code)();
|
237 | this.succeeded = true;
|
238 | this._successCallback();
|
239 | return true;
|
240 | } catch (e) {
|
241 |
|
242 |
|
243 | return false;
|
244 | }
|
245 | }
|
246 |
|
247 | |
248 |
|
249 |
|
250 |
|
251 | async _loadFromNetwork(url) {
|
252 |
|
253 | this._networkTimeout = setTimeout(() => {
|
254 | this._networkTimedOut = true;
|
255 | this._failureCallback(
|
256 | `Timed out (>${LOAD_ATTEMPT_NETWORK_TIMEOUT} ms) when loading call object bundle ${url}`
|
257 | );
|
258 | }, LOAD_ATTEMPT_NETWORK_TIMEOUT);
|
259 |
|
260 | try {
|
261 | const fetchOptions = this._refetchHeaders
|
262 | ? { headers: this._refetchHeaders }
|
263 | : {};
|
264 | const response = await fetch(url, fetchOptions);
|
265 | clearTimeout(this._networkTimeout);
|
266 |
|
267 |
|
268 | if (this.cancelled || this._networkTimedOut) {
|
269 | throw new LoadAttemptAbortedError();
|
270 | }
|
271 |
|
272 | const code = await this._getBundleCodeFromResponse(url, response);
|
273 |
|
274 |
|
275 | if (this.cancelled) {
|
276 | throw new LoadAttemptAbortedError();
|
277 | }
|
278 |
|
279 |
|
280 | Function('"use strict";' + code)();
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | this._iosCache && this._iosCache.set(url, code, response.headers);
|
286 | this.succeeded = true;
|
287 | this._successCallback();
|
288 | } catch (e) {
|
289 | clearTimeout(this._networkTimeout);
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | if (
|
295 | e instanceof LoadAttemptAbortedError ||
|
296 | this.cancelled ||
|
297 | this._networkTimedOut
|
298 | ) {
|
299 |
|
300 | return;
|
301 | }
|
302 |
|
303 | this._failureCallback(`Failed to load call object bundle ${url}: ${e}`);
|
304 | }
|
305 | }
|
306 |
|
307 | async _getBundleCodeFromResponse(url, response) {
|
308 |
|
309 | if (response.ok) {
|
310 | return await response.text();
|
311 | }
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | if (this._iosCache && response.status === 304) {
|
317 | const cacheResponse = await this._iosCache.renew(url, response.headers);
|
318 | return cacheResponse.code;
|
319 | }
|
320 |
|
321 | throw new Error(`Received ${response.status} response`);
|
322 | }
|
323 | }
|