forked from sass/dart-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NodeJS
run_
API method should return native JavaScript Promise
Currently the `run_` method, exposed through the `dart.sass.js` file, returns a `Future`. The `Future` object is not accessible due to the Dart compilation process and `Future` not being a thing in the JavaScript/NodeJS platform. In order to provide a better interface to the Sass API for JavaScript/NodeJS consumers, a native `Promise` is now returned. Fixes bazelbuild/rules_sass#96.
- Loading branch information
1 parent
ca63b1b
commit 7699029
Showing
4 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2019 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import 'dart:js'; | ||
|
||
import 'package:js/js.dart'; | ||
|
||
/// Describes the native JavaScript `Promise` global. | ||
@JS() | ||
class Promise<T> { | ||
external Promise(void executor(void resolve(T result), Function reject)); | ||
} | ||
|
||
/// Converts a Future to a JavaScript Promise. | ||
Promise<T> futureToPromise<T>(Future<T> future) { | ||
return Promise<T>(allowInterop((resolve, reject) => | ||
future.then(resolve, onError: reject))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2018 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
@TestOn('node') | ||
@Tags(['node']) | ||
|
||
import 'package:js/js.dart'; | ||
import 'package:js/js_util.dart'; | ||
import 'package:sass/src/node/promise.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../ensure_npm_package.dart'; | ||
import 'api.dart'; | ||
import 'utils.dart'; | ||
|
||
/// Describes a JavaScript object. | ||
/// | ||
/// Object's always have a `constructor` property in JavaScript. | ||
@JS() | ||
class ObjectWithConstructor { | ||
Object constructor; | ||
} | ||
|
||
void main() { | ||
setUpAll(ensureNpmPackage); | ||
useSandbox(); | ||
|
||
group('run_ method', () { | ||
test('returns a JavaScript native Promise', () async { | ||
var result = sass.run_([]) as ObjectWithConstructor; | ||
expect(result, isA<Promise<void>>()); | ||
expect(result.constructor.toString(), contains('[native code]')); | ||
await promiseToFuture(result); | ||
}); | ||
}); | ||
} |