forked from mathjax/MathJax-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.ts
426 lines (389 loc) · 12.6 KB
/
package.ts
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*************************************************************
*
* Copyright (c) 2018-2022 The MathJax Consortium
*
* 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 Implements component Package object for handling
* dynamic loading of components.
*
* @author dpvc@mathjax.org (Davide Cervone)
*/
import {CONFIG, Loader} from './loader.js';
/*
* The browser document (for creating scripts to load components)
*/
declare var document: Document;
/**
* A map of package names to Package instances
*/
export type PackageMap = Map<string, Package>;
/**
* An error class that includes the package name
*/
export class PackageError extends Error {
/* tslint:disable:jsdoc-require */
public package: string;
constructor(message: string, name: string) {
super(message);
this.package = name;
}
/* tslint:enable */
}
/**
* Types for ready() and failed() functions and for promises
*/
export type PackageReady = (name: string) => string | void;
export type PackageFailed = (message: PackageError) => void;
export type PackagePromise = (resolve: PackageReady, reject: PackageFailed) => void;
/**
* The configuration data for a package
*/
export interface PackageConfig {
ready?: PackageReady; // Function to call when package is loaded successfully
failed?: PackageFailed; // Function to call when package fails to load
checkReady?: () => Promise<void>; // Function called to see if package is fully loaded
// (may cause additional packages to load, for example)
}
/**
* The Package class for handling individual components
*/
export class Package {
/**
* The set of packages being used
*/
public static packages: PackageMap = new Map();
/**
* The package name
*/
public name: string;
/**
* True when the package has been loaded successfully
*/
public isLoaded: boolean = false;
/**
* A promise that resolves when the package is loaded successfully and rejects when it fails to load
*/
public promise: Promise<string>;
/**
* True when the package is being loaded but hasn't yet finished loading
*/
protected isLoading: boolean = false;
/**
* True if the package has failed to load
*/
protected hasFailed: boolean = false;
/**
* True if this package should be loaded automatically (e.g., it was created in reference
* to a MathJax.loader.ready() call when the package hasn't been requested to load)
*/
protected noLoad: boolean;
/**
* The function that resolves the package's promise
*/
protected resolve: PackageReady;
/**
* The function that rejects the package's promise
*/
protected reject: PackageFailed;
/**
* The packages that require this one
*/
protected dependents: Package[] = [];
/**
* The packages that this one depends on
*/
protected dependencies: Package[] = [];
/**
* The number of dependencies that haven't yet been loaded
*/
protected dependencyCount: number = 0;
/**
* The sub-packages that this one provides
*/
protected provided: Package[] = [];
/**
* @return {boolean} True when the package can be loaded (i.e., its dependencies are all loaded,
* it is allowed to be loaded, isn't already loading, and hasn't failed to load
* in the past)
*/
get canLoad(): boolean {
return this.dependencyCount === 0 && !this.noLoad && !this.isLoading && !this.hasFailed;
}
/**
* Compute the path for a package using the loader's path filters
*
* @param {string} name The name of the package to resolve
* @param {boolean} addExtension True if .js should be added automatically
* @return {string} The path (file or URL) for this package
*/
public static resolvePath(name: string, addExtension: boolean = true): string {
const data = {name, original: name, addExtension};
Loader.pathFilters.execute(data);
return data.name;
}
/**
* Attempt to load all packages that are ready to be loaded
* (i.e., that have no unloaded dependencies, and that haven't
* already been loaded, and that aren't in process of being
* loaded, and that aren't marked as noLoad).
*/
public static loadAll() {
for (const extension of this.packages.values()) {
if (extension.canLoad) {
extension.load();
}
}
}
/**
* @param {string} name The name of the package
* @param {boolean} noLoad True when the package is just for reference, not loading
*/
constructor(name: string, noLoad: boolean = false) {
this.name = name;
this.noLoad = noLoad;
Package.packages.set(name, this);
this.promise = this.makePromise(this.makeDependencies());
}
/**
* @return {Promise<string>[]} The array of promises that must be resolved before this package
* can be loaded
*/
protected makeDependencies(): Promise<string>[] {
const promises = [] as Promise<string>[];
const map = Package.packages;
const noLoad = this.noLoad;
const name = this.name;
//
// Get the dependencies for this package
//
const dependencies = [] as string[];
if (CONFIG.dependencies.hasOwnProperty(name)) {
dependencies.push(...CONFIG.dependencies[name]);
} else if (name !== 'core') {
dependencies.push('core'); // Add 'core' dependency by default
}
//
// Add all the dependencies (creating them, if needed)
// and record the promises of unloaded ones
//
for (const dependent of dependencies) {
const extension = map.get(dependent) || new Package(dependent, noLoad);
if (this.dependencies.indexOf(extension) < 0) {
extension.addDependent(this, noLoad);
this.dependencies.push(extension);
if (!extension.isLoaded) {
this.dependencyCount++;
promises.push(extension.promise);
}
}
}
//
// Return the collected promises
//
return promises;
}
/**
* @param {Promise<string>[]} promises The array or promises that must be resolved before
* this package can load
*/
protected makePromise(promises: Promise<string>[]) {
//
// Make a promise and save its resolve/reject functions
//
let promise = new Promise<string>(((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
}) as PackagePromise);
//
// If there is a ready() function in the configuration for this package,
// Add running that to the promise
//
const config = (CONFIG[this.name] || {}) as PackageConfig;
if (config.ready) {
promise = promise.then((_name: string) => config.ready(this.name)) as Promise<string>;
}
//
// If there are promises for dependencies,
// Add the one for loading this package and create a promise for all of them
// (That way, if any of them fail to load, our promise will reject automatically)
//
if (promises.length) {
promises.push(promise);
promise = Promise.all(promises).then((names: string[]) => names.join(', '));
}
//
// If there is a failed() function in the configuration for this package,
// Add a catch to handle the error
//
if (config.failed) {
promise.catch((message: string) => config.failed(new PackageError(message, this.name)));
}
//
// Return the promise that represents when this file is loaded
//
return promise;
}
/**
* Attempt to load this package
*/
public load() {
if (!this.isLoaded && !this.isLoading && !this.noLoad) {
this.isLoading = true;
const url = Package.resolvePath(this.name);
if (CONFIG.require) {
this.loadCustom(url);
} else {
this.loadScript(url);
}
}
}
/**
* Load using a custom require method (usually the one from node.js)
*/
protected loadCustom(url: string) {
try {
const result = CONFIG.require(url);
if (result instanceof Promise) {
result.then(() => this.checkLoad())
.catch((err) => this.failed('Can\'t load "' + url + '"\n' + err.message.trim()));
} else {
this.checkLoad();
}
} catch (err) {
this.failed(err.message);
}
}
/**
* Load in a browser by inserting a script to load the proper URL
*/
protected loadScript(url: string) {
const script = document.createElement('script');
script.src = url;
script.charset = 'UTF-8';
script.onload = (_event) => this.checkLoad();
script.onerror = (_event) => this.failed('Can\'t load "' + url + '"');
// FIXME: Should there be a timeout failure as well?
document.head.appendChild(script);
}
/**
* Called when the package is loaded.
*
* Mark it as loaded, and tell its dependents that this package
* has been loaded (may cause dependents to load themselves).
* Mark any provided packages as loaded.
* Resolve the promise that says this package is loaded.
*/
public loaded() {
this.isLoaded = true;
this.isLoading = false;
for (const dependent of this.dependents) {
dependent.requirementSatisfied();
}
for (const provided of this.provided) {
provided.loaded();
}
this.resolve(this.name);
}
/**
* Called when the package fails to load for some reason
*
* Mark it as failed to load
* Reject the promise for this package with an error
*
* @param {string} message The error message for a load failure
*/
protected failed(message: string) {
this.hasFailed = true;
this.isLoading = false;
this.reject(new PackageError(message, this.name));
}
/**
* Check if a package is really ready to be marked as loaded
* (When it is loaded, it may set its own checkReady() function
* as a means of loading additional packages. E.g., an output
* jax may load a font package, dependent on its configuration.)
*
* The configuration's checkReady() function returns a promise
* that allows the loader to wait for addition actions to finish
* before marking the file as loaded (or failing to load).
*/
protected checkLoad() {
const config = (CONFIG[this.name] || {}) as PackageConfig;
const checkReady = config.checkReady || (() => Promise.resolve());
checkReady().then(() => this.loaded())
.catch((message) => this.failed(message));
}
/**
* This is called when a dependency loads.
*
* Decrease the dependency count, and try to load this package
* when the dependencies are all loaded.
*/
public requirementSatisfied() {
if (this.dependencyCount) {
this.dependencyCount--;
if (this.canLoad) {
this.load();
}
}
}
/**
* @param {string[]} names The names of the packages that this package provides
*/
public provides(names: string[] = []) {
for (const name of names) {
let provided = Package.packages.get(name);
if (!provided) {
if (!CONFIG.dependencies[name]) {
CONFIG.dependencies[name] = [];
}
CONFIG.dependencies[name].push(name);
provided = new Package(name, true);
provided.isLoading = true;
}
this.provided.push(provided);
}
}
/**
* Add a package as a dependent, and if it is not just for reference,
* check if we need to change our noLoad status.
*
* @param {Package} extension The package to add as a dependent
* @param {boolean} noLoad The noLoad status of the dependent
*/
public addDependent(extension: Package, noLoad: boolean) {
this.dependents.push(extension);
if (!noLoad) {
this.checkNoLoad();
}
}
/**
* If this package is marked as noLoad, change that and check all
* our dependencies to see if they need to change their noLoad
* status as well.
*
* I.e., if there are dependencies that were set up for reference
* and a leaf node needs to be loaded, make sure all parent nodes
* are marked as needing to be loaded as well.
*/
public checkNoLoad() {
if (this.noLoad) {
this.noLoad = false;
for (const dependency of this.dependencies) {
dependency.checkNoLoad();
}
}
}
}