124 lines
2.9 KiB
Dart
Raw Normal View History

library angular.core.facade.async;
import 'dart:async';
export 'dart:async' show Future, Stream, StreamController, StreamSubscription;
2014-10-10 15:44:56 -04:00
class PromiseWrapper {
2015-01-29 09:50:49 -08:00
static Future resolve(obj) => new Future.value(obj);
static Future reject(obj, stackTrace) => new Future.error(
obj,
stackTrace != null
? stackTrace
: obj is Error
? obj.stackTrace
: null);
2015-01-29 09:50:49 -08:00
static Future<List> all(List<Future> promises) => Future.wait(promises);
2015-01-29 09:50:49 -08:00
static Future then(Future promise, success(value), Function onError) {
2014-12-01 20:06:21 +01:00
if (success == null) return promise.catchError(onError);
2014-10-10 15:44:56 -04:00
return promise.then(success, onError: onError);
}
// Note: We can't rename this method to `catch`, as this is not a valid
// method name in Dart.
static Future catchError(Future promise, Function onError) {
return promise.catchError(onError);
}
static CompleterWrapper completer() => new CompleterWrapper(new Completer());
// TODO(vic): create a TimerWrapper
static Timer setTimeout(fn(), int millis)
=> new Timer(new Duration(milliseconds: millis), fn);
static void clearTimeout(Timer timer) {
timer.cancel();
}
static Timer setInterval(fn(), int millis) {
var interval = new Duration(milliseconds: millis);
return new Timer.periodic(interval, (Timer timer) { fn(); });
}
static void clearInterval(Timer timer) {
timer.cancel();
}
static bool isPromise(maybePromise) {
return maybePromise is Future;
}
}
class ObservableWrapper {
2015-05-08 19:51:19 -07:00
static StreamSubscription subscribe(Stream s, Function onNext,
[onError, onComplete]) {
return s.listen(onNext,
onError: onError, onDone: onComplete, cancelOnError: true);
}
static bool isObservable(obs) {
return obs is Stream;
}
static void dispose(StreamSubscription s) {
s.cancel();
}
static void callNext(EventEmitter emitter, value) {
emitter.add(value);
}
static void callThrow(EventEmitter emitter, error) {
emitter.addError(error);
}
static void callReturn(EventEmitter emitter) {
emitter.close();
}
}
class EventEmitter extends Stream {
StreamController<String> _controller;
EventEmitter() {
_controller = new StreamController.broadcast();
}
2015-05-08 19:51:19 -07:00
StreamSubscription listen(void onData(String line),
{void onError(Error error), void onDone(), bool cancelOnError}) {
return _controller.stream.listen(onData,
2015-05-08 19:51:19 -07:00
onError: onError, onDone: onDone, cancelOnError: cancelOnError);
}
void add(value) {
_controller.add(value);
}
void addError(error) {
_controller.addError(error);
}
void close() {
_controller.close();
}
}
class CompleterWrapper {
2015-01-29 09:50:49 -08:00
final Completer c;
CompleterWrapper(this.c);
2015-01-29 09:50:49 -08:00
Future get promise => c.future;
void resolve(v) {
c.complete(v);
}
void reject(error, stack) {
if (stack == null && error is Error) {
stack = error.stackTrace;
}
c.completeError(error, stack);
}
}