Skip to content

Commit

Permalink
Add requireAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
RumovZ committed May 18, 2024
1 parent f634606 commit 35f0b7d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions ts/lib/tslib/runtime-require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type AnkiPackages =
| "anki/location"
| "anki/surround"
| "anki/ui"
| "anki/reviewer";
| "anki/reviewer"
| "anki/ExportPage";
type PackageDeprecation<T extends Record<string, unknown>> = {
[key in keyof T]?: string;
};
Expand Down Expand Up @@ -77,6 +78,24 @@ function require<T extends AnkiPackages>(name: T): Record<string, unknown> | und
}
}

// Resolves as soon as the requested package is available.
function requireAsync<T extends AnkiPackages>(name: T): Promise<Record<string, unknown>> {
return new Promise((resolve) => {
const runtimePackage = runtimePackages[name];
if (runtimePackage !== undefined) {
resolve(runtimePackage);
} else {
const intervalId = setInterval(() => {
const runtimePackage = runtimePackages[name];
if (runtimePackage !== undefined) {
clearInterval(intervalId);
resolve(runtimePackage);
}
}, 50);
}
});
}

function listPackages(): string[] {
return Object.keys(runtimePackages);
}
Expand All @@ -91,8 +110,8 @@ function hasPackages(...names: string[]): boolean {
return true;
}

// Export require() as a global.
Object.assign(globalThis, { require });
// Export require() and requireAsync() as globals.
Object.assign(globalThis, { require, requireAsync });

registerPackage("anki/packages", {
// We also register require here, so add-ons can have a type-save variant of require (TODO, see AnkiPackages above)
Expand Down

0 comments on commit 35f0b7d

Please sign in to comment.