-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
async.ts
33 lines (24 loc) Β· 878 Bytes
/
async.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
import {setFactory, getInstance, tryInstance} from './instance';
import {Libzip, makeInterface} from './makeInterface';
export * from './common';
let promise: Promise<Libzip> | undefined;
setFactory(() => {
throw new Error(`Zip methods must be called within the context of getLibzipPromise when operating under async-only environments`);
});
export function getLibzipSync() {
return getInstance();
}
export async function getLibzipPromise() {
const instance = tryInstance();
if (typeof instance !== `undefined`)
return instance;
if (typeof promise !== `undefined`)
return promise;
return promise = import(`./libzipAsync`).then(async ({default: createModule}) => {
const emZip = await createModule();
const libzip = makeInterface(emZip);
promise = undefined;
setFactory(() => libzip);
return getInstance();
});
}