Skip to content

Commit

Permalink
feat(compilers): allow compiler factory to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Mar 6, 2024
1 parent 7d8bbd0 commit d76b351
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/livecodes/compiler/compile.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let baseUrl: string | undefined;
const worker: Worker = self as any;
(self as any).window = self;

const loadLanguageCompiler = (
const loadLanguageCompiler = async (
language: LanguageOrProcessor,
config: Config,
baseUrl: string | undefined,
Expand All @@ -29,22 +29,22 @@ const loadLanguageCompiler = (
}

if (languageCompiler.dependencies && languageCompiler.dependencies.length > 0) {
languageCompiler.dependencies.forEach((dependency) => {
loadLanguageCompiler(dependency, config, baseUrl);
});
for (const dependency of languageCompiler.dependencies) {
await loadLanguageCompiler(dependency, config, baseUrl);
}
}

if (typeof languageCompiler.fn !== 'function') {
if (languageCompiler.aliasTo && typeof compilers[languageCompiler.aliasTo]?.fn === 'function') {
languageCompiler.fn = compilers[languageCompiler.aliasTo].fn;
} else {
let tries = 3;
const load = () => {
const load = async () => {
try {
if (languageCompiler.url) {
importScripts(languageCompiler.url);
}
languageCompiler.fn = languageCompiler.factory(config, baseUrl);
languageCompiler.fn = await languageCompiler.factory(config, baseUrl);
if (languageCompiler.aliasTo) {
compilers[languageCompiler.aliasTo].fn = languageCompiler.fn;
}
Expand All @@ -62,7 +62,7 @@ const loadLanguageCompiler = (
}
};

load();
await load();
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ worker.addEventListener(

if (message.type === 'load' || message.type === 'compileInCompiler') {
const { language, config } = message.payload;
loadLanguageCompiler(language, config, baseUrl);
await loadLanguageCompiler(language, config, baseUrl);
}

if (message.type === 'compile' || message.type === 'compileInCompiler') {
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ export interface Compiler {
dependencies?: Language[];
url?: string;
fn?: CompilerFunction;
factory: (config: Config, baseUrl: string) => CompilerFunction;
factory: (config: Config, baseUrl: string) => CompilerFunction | Promise<CompilerFunction>;
runOutsideWorker?: CompilerFunction;
editors?: EditorId[];
styles?:
Expand Down

0 comments on commit d76b351

Please sign in to comment.