Skip to content

Commit

Permalink
Wait for compilations to complete before disposing
Browse files Browse the repository at this point in the history
  • Loading branch information
jerivas committed Nov 22, 2023
1 parent a4e624b commit 31a89c0
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions lib/src/js/compiler.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:js_util';

import 'package:node_interop/js.dart';

import 'compile.dart';
Expand All @@ -15,7 +17,19 @@ class Compiler {
}
}

class AsyncCompiler extends Compiler {}
class AsyncCompiler extends Compiler {
final Set<Promise> _compilations = {};

/// Adds a compilation to the pending set and removes it when it's done.
void _addCompilation(Promise compilation) {
_compilations.add(compilation);
compilation.then((value) {
_compilations.remove(compilation);
}, (error) {
_compilations.remove(compilation);
});
}
}

/// The JS API Compiler class
///
Expand Down Expand Up @@ -60,16 +74,23 @@ final JSClass asyncCompilerClass = () {
'compileAsync': (AsyncCompiler self, String path,
[CompileOptions? options]) {
self.throwIfDisposed();
return compileAsync(path, options);
var compilation = compileAsync(path, options);
self._addCompilation(compilation);
return compilation;
},
'compileStringAsync': (AsyncCompiler self, String source,
[CompileStringOptions? options]) {
self.throwIfDisposed();
return compileStringAsync(source, options);
var compilation = compileStringAsync(source, options);
self._addCompilation(compilation);
return compilation;
},
'dispose': (AsyncCompiler self) async {
'dispose': (AsyncCompiler self) {
self._disposed = true;
},
return futureToPromise((() async {
await Future.wait(self._compilations.map(promiseToFuture<Object>));
})());
}
});

getJSClass(AsyncCompiler()).injectSuperclass(jsClass);
Expand Down

0 comments on commit 31a89c0

Please sign in to comment.