-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.js
More file actions
65 lines (57 loc) · 1.97 KB
/
complex.js
File metadata and controls
65 lines (57 loc) · 1.97 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
// @ts-check
/// <reference path="./typings/complex.d.ts" />
/**
* @fileoverview This file imports Complex class & makes it globally available.
*/
/**
* Represents the default export of the './complex.mjs' module, which is the
* 'Complex' class constructor.
* @typedef {typeof import('./complex.mjs').default} Ctor
*/
import('./complex.mjs').then(({ default: c }) => {
/**
* An immutable complex number class with real and imaginary parts.
* @global
* @var {Ctor} Complex
*/
var Complex = c;
globalThis.Complex = Complex;
});
/**
* This callback is invoked when the 'Complex' class is ready & available
* in the global scope.
*
* @callback complexReadyCallback
* @param {Ctor} Complex The 'Complex' class constructor when ready.
* @return {void}
*/
/**
* This function checks if the 'Complex' class is available in the global
* scope. If it is, it calls the provided callback function with the 'Complex'
* class as the argument. If it's not available, it sets a timeout to check
* again after 1 second.
*
* @param {complexReadyCallback=} callback The callback function to be called
* when 'Complex' is ready.
* @return {Promise<Ctor>} A `Promise` which resolves as the 'Complex' class.
*/
function callMeWhenComplexReady(callback) {
/**
* This inner function checks if the 'Complex' class is available in the
* global scope. If it is, it resolves the promise with the 'Complex' class
* and then calls the provided callback function with the 'Complex' class
* as the argument. Otherwise, it sets a timeout to check again after 1s.
* @param {(value: Ctor) => void} res
* @return {void}
*/
function executor(res) {
if ('Complex' in globalThis) return res(Complex), callback?.(Complex);
setTimeout(executor, 1000, res);
}
return new Promise(executor);
}
/*
void function checkComplex(fun) {
'Complex' in globalThis ? fun() : setTimeout(checkComplex, 1000, fun);
}(startCallback);
*/