Skip to content

Commit

Permalink
feat(facade): added support for observables
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Mar 24, 2015
1 parent 43f4374 commit 9b3b3d3
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 4 deletions.
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ var _HTML_DEFAULT_SCRIPTS_JS = [
{src: 'node_modules/zone.js/long-stack-trace-zone.js', mimeType: 'text/javascript', copy: true},
{src: 'node_modules/systemjs/dist/system.src.js', mimeType: 'text/javascript', copy: true},
{src: 'node_modules/systemjs/lib/extension-register.js', mimeType: 'text/javascript', copy: true},
{src: 'node_modules/systemjs/lib/extension-cjs.js', mimeType: 'text/javascript', copy: true},
{src: 'node_modules/rx/dist/rx.all.js', mimeType: 'text/javascript', copy: true},
{src: 'tools/build/snippets/runtime_paths.js', mimeType: 'text/javascript', copy: true},
{
inline: 'System.import(\'$MODULENAME$\').then(function(m) { m.main(); }, console.error.bind(console))',
Expand Down
2 changes: 2 additions & 0 deletions karma-js.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module.exports = function(config) {
// Including systemjs because it defines `__eval`, which produces correct stack traces.
'node_modules/systemjs/dist/system.src.js',
'node_modules/systemjs/lib/extension-register.js',
'node_modules/systemjs/lib/extension-cjs.js',
'node_modules/rx/dist/rx.all.js',
'node_modules/zone.js/zone.js',
'node_modules/zone.js/long-stack-trace-zone.js',

Expand Down
1 change: 1 addition & 0 deletions modules/angular2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"traceur": "<%= packageJson.dependencies.traceur %>",
"rtts_assert": "<%= packageJson.version %>",
"rx": "<%= packageJson.dependencies['rx'] %>",
"zone.js": "<%= packageJson.dependencies['zone.js'] %>"
},
"devDependencies": <%= JSON.stringify(packageJson.devDependencies) %>
Expand Down
28 changes: 27 additions & 1 deletion modules/angular2/src/facade/async.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library angular.core.facade.async;

import 'dart:async';
export 'dart:async' show Future;
export 'dart:async' show Future, Stream, StreamController, StreamSubscription;

class PromiseWrapper {
static Future resolve(obj) => new Future.value(obj);
Expand Down Expand Up @@ -32,6 +32,32 @@ class PromiseWrapper {
}
}

class ObservableWrapper {
static StreamSubscription subscribe(Stream s, Function onNext, [onError, onComplete]) {
return s.listen(onNext, onError: onError, onDone: onComplete, cancelOnError: true);
}

static StreamController createController() {
return new StreamController.broadcast();
}

static Stream createObservable(StreamController controller) {
return controller.stream;
}

static void callNext(StreamController controller, value) {
controller.add(value);
}

static void callThrow(StreamController controller, error) {
controller.addError(error);
}

static void callReturn(StreamController controller) {
controller.close();
}
}

class _Completer {
final Completer c;

Expand Down
47 changes: 46 additions & 1 deletion modules/angular2/src/facade/async.es6
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {int, global} from 'angular2/src/facade/lang';
import {int, global, isPresent} from 'angular2/src/facade/lang';
import {List} from 'angular2/src/facade/collection';
import Rx from 'rx/dist/rx.all';

export var Promise = global.Promise;

Expand Down Expand Up @@ -51,3 +52,47 @@ export class PromiseWrapper {
return maybePromise instanceof Promise;
}
}


/**
* Use Rx.Observable but provides an adapter to make it work as specified here:
* https://github.com/jhusain/observable-spec
*
* Once a reference implementation of the spec is available, switch to it.
*/
export var Observable = Rx.Observable;
export var ObservableController = Rx.Subject;

export class ObservableWrapper {
static createController():Rx.Subject {
return new Rx.Subject();
}

static createObservable(subject:Rx.Subject):Observable {
return subject;
}

static subscribe(observable:Observable, generatorOrOnNext, onThrow = null, onReturn = null) {
if (isPresent(generatorOrOnNext.next)) {
return observable.observeOn(Rx.Scheduler.timeout).subscribe(
(value) => generatorOrOnNext.next(value),
(error) => generatorOrOnNext.throw(error),
() => generatorOrOnNext.return()
);
} else {
return observable.observeOn(Rx.Scheduler.timeout).subscribe(generatorOrOnNext, onThrow, onReturn);
}
}

static callNext(subject:Rx.Subject, value:any) {
subject.onNext(value);
}

static callThrow(subject:Rx.Subject, error:any) {
subject.onError(error);
}

static callReturn(subject:Rx.Subject) {
subject.onCompleted();
}
}
99 changes: 99 additions & 0 deletions modules/angular2/test/facade/async_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el,
SpyObject, AsyncTestCompleter, inject, IS_DARTIUM} from 'angular2/test_lib';

import {ObservableWrapper, Observable, ObservableController, PromiseWrapper} from 'angular2/src/facade/async';

export function main() {
describe('Observable', () => {
var obs:Observable;
var controller:ObservableController;

beforeEach(() => {
controller = ObservableWrapper.createController();
obs = ObservableWrapper.createObservable(controller);
});

it("should call the next callback", inject([AsyncTestCompleter], (async) => {
ObservableWrapper.subscribe(obs, (value) => {
expect(value).toEqual(99);
async.done();
});

ObservableWrapper.callNext(controller, 99);
}));

it("should call the throw callback", inject([AsyncTestCompleter], (async) => {
ObservableWrapper.subscribe(obs, (_) => {}, (error) => {
expect(error).toEqual("Boom");
async.done();
});
ObservableWrapper.callThrow(controller, "Boom");
}));

it("should call the return callback", inject([AsyncTestCompleter], (async) => {
ObservableWrapper.subscribe(obs, (_) => {}, (_) => {}, () => {
async.done();
});

ObservableWrapper.callReturn(controller);
}));

it("should subscribe to the wrapper asynchronously", () => {
var called = false;
ObservableWrapper.subscribe(obs, (value) => {
called = true;
});

ObservableWrapper.callNext(controller, 99);
expect(called).toBe(false);
});

if (!IS_DARTIUM) {
// See here: https://github.com/jhusain/observable-spec
describe("Generator", () => {
var generator;

beforeEach(() => {
generator = new SpyObject();
generator.spy("next");
generator.spy("throw");
generator.spy("return");
});

it("should call next on the given generator", inject([AsyncTestCompleter], (async) => {
generator.spy("next").andCallFake((value) => {
expect(value).toEqual(99);
async.done();
});

ObservableWrapper.subscribe(obs, generator);
ObservableWrapper.callNext(controller, 99);
}));

it("should call throw on the given generator", inject([AsyncTestCompleter], (async) => {
generator.spy("throw").andCallFake((error) => {
expect(error).toEqual("Boom");
async.done();
});
ObservableWrapper.subscribe(obs, generator);
ObservableWrapper.callThrow(controller, "Boom");
}));

it("should call return on the given generator", inject([AsyncTestCompleter], (async) => {
generator.spy("return").andCallFake(() => {
async.done();
});
ObservableWrapper.subscribe(obs, generator);
ObservableWrapper.callReturn(controller);
}));
});
}

//TODO: vsavkin: add tests cases
//should call dispose on the subscription if generator returns {done:true}
//should call dispose on the subscription on throw
//should call dispose on the subscription on return
});
}

//make sure rx observables are async
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"es6-module-loader": "^0.9.2",
"systemjs": "^0.9.1",
"traceur": "0.0.82",
"rx": "2.4.6",
"which": "~1",
"zone.js": "0.4.0",
"googleapis": "1.0.x",
Expand Down
4 changes: 3 additions & 1 deletion test-main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Use "register" extension from systemjs.
// That's what Traceur outputs: `System.register()`.
register(System);
cjs(System);

jasmine.DEFAULT_TIMEOUT_INTERVAL = 50;

Expand All @@ -14,7 +15,8 @@ System.baseURL = '/base/modules/';
// So that we can import packages like `core/foo`, instead of `core/src/foo`.
System.paths = {
'*': './*.js',
'transpiler/*': '../tools/transpiler/*.js'
'transpiler/*': '../tools/transpiler/*.js',
'rx/*': '../node_modules/rx/*.js'
}

// Import all the specs, execute their `main()` method and kick off Karma (Jasmine).
Expand Down
5 changes: 4 additions & 1 deletion tools/build/snippets/runtime_paths.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
System.paths = {
'*': '/*.js'
'*': '/*.js',
'rx/dist/*': '*.js'
};
register(System);
cjs(System);

2 changes: 2 additions & 0 deletions tools/transpiler/src/type_mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export var typeMapping = {
'string': 'String',
'any': 'dynamic',
'Promise': 'Future',
'Observable': 'Stream',
'ObservableController': 'StreamController',
'Date': 'DateTime',
'StringMap': 'Map'
};

0 comments on commit 9b3b3d3

Please sign in to comment.