Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shared Resources] dart-sass implementation #2134

Merged
merged 13 commits into from
Jan 18, 2024
31 changes: 20 additions & 11 deletions lib/src/js/compiler.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
import 'dart:js_util';

import 'package:async/async.dart';
import 'package:node_interop/js.dart';

import 'compile.dart';
import 'compile_options.dart';
import 'reflection.dart';
import 'utils.dart';

/// The Dart Compiler class.
class Compiler {
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
/// A flag signifying whether the instance has been disposed.
bool _disposed = false;

/// Checks if `dispose()` has been called on this instance, and throws an
/// error if it has. Used to verify that compilation methods are not called
/// after disposal.
void throwIfDisposed() {
if (_disposed) {
jsThrow(JsError('Compiler has already been disposed.'));
}
}
}

/// The Dart Async Compiler class.
class AsyncCompiler extends Compiler {
final Set<Promise> _compilations = {};
/// A set of all compilations, tracked to ensure all compilations settle
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
/// before async disposal resolves.
final FutureGroup<dynamic> compilations = FutureGroup();
jamesnw marked this conversation as resolved.
Show resolved Hide resolved

/// 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);
/// Adds a compilation to the FutureGroup.
void addCompilation(Promise compilation) {
Future<dynamic> comp = promiseToFuture(compilation);
comp.catchError((err) {
return;
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
});
compilations.add(comp);
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -65,20 +73,21 @@ final JSClass asyncCompilerClass = () {
[CompileOptions? options]) {
self.throwIfDisposed();
var compilation = compileAsync(path, options);
self._addCompilation(compilation);
self.addCompilation(compilation);
return compilation;
},
'compileStringAsync': (AsyncCompiler self, String source,
[CompileStringOptions? options]) {
self.throwIfDisposed();
var compilation = compileStringAsync(source, options);
self._addCompilation(compilation);
self.addCompilation(compilation);
return compilation;
},
'dispose': (AsyncCompiler self) {
self._disposed = true;
return futureToPromise((() async {
await Future.wait(self._compilations.map(promiseToFuture<Object>));
self.compilations.close();
await self.compilations.future;
})());
}
});
Expand Down