Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 062d814

Browse files
start/Async
1 parent c853c25 commit 062d814

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

src/modular/observable/start.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var toAsync = require('./toasync');
4+
5+
module.exports = function start (func, context, scheduler) {
6+
return toAsync(func, context, scheduler)();
7+
};

src/modular/observable/startasync.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var fromPromise = require('./frompromise');
4+
var throwError = require('./throw');
5+
var tryCatch = require('../internal/trycatchutils').tryCatch;
6+
7+
module.exports = function startAsync(functionAsync) {
8+
var promise = tryCatch(functionAsync)();
9+
if (promise === global._Rx.errorObj) { return throwError(promise.e); }
10+
return fromPromise(promise);
11+
};

src/modular/test/start.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
'use strict';
2+
3+
var test = require('tape');
4+
var Observable = require('../observable');
5+
var TestScheduler = require('../testing/testscheduler');
6+
var reactiveAssert = require('../testing/reactiveassert');
7+
var ReactiveTest = require('../testing/reactivetest');
8+
var onNext = ReactiveTest.onNext,
9+
onError = ReactiveTest.onError,
10+
onCompleted = ReactiveTest.onCompleted;
11+
12+
Observable.addToObject({
13+
start: require('../observable/start'),
14+
startAsync: require('../observable/startAsync')
15+
});
16+
17+
function noop() {}
18+
19+
// Polyfilling
20+
require('lie/polyfill');
21+
22+
test('Observable.startAsync', function (t) {
23+
var source = Observable.startAsync(function () {
24+
return new Promise(function (res) { res(42); });
25+
});
26+
27+
source.subscribe(function (x) {
28+
t.equal(42, x);
29+
t.end();
30+
});
31+
});
32+
33+
test('Observable.startAsync Error', function (t) {
34+
var source = Observable.startAsync(function () {
35+
return new Promise(function (res, rej) { rej(42); });
36+
});
37+
38+
source.subscribe(noop, function (err) {
39+
t.equal(42, err);
40+
t.end();
41+
});
42+
});
43+
44+
test('Observable.start action 2', function (t) {
45+
var scheduler = new TestScheduler();
46+
47+
var done = false;
48+
49+
var results = scheduler.startScheduler(function () {
50+
return Observable.start(function () {
51+
done = true;
52+
}, null, scheduler);
53+
});
54+
55+
reactiveAssert(t, results.messages, [
56+
onNext(200, undefined),
57+
onCompleted(200)
58+
]);
59+
60+
t.ok(done);
61+
62+
t.end();
63+
});
64+
65+
test('Observable.start function 2', function (t) {
66+
var scheduler = new TestScheduler();
67+
68+
var results = scheduler.startScheduler(function () {
69+
return Observable.start(function () {
70+
return 1;
71+
}, null, scheduler);
72+
});
73+
74+
reactiveAssert(t, results.messages, [
75+
onNext(200, 1),
76+
onCompleted(200)
77+
]);
78+
79+
t.end();
80+
});
81+
82+
test('Observable.start with error', function (t) {
83+
var error = new Error();
84+
85+
var scheduler = new TestScheduler();
86+
87+
var results = scheduler.startScheduler(function () {
88+
return Observable.start(function () {
89+
throw error;
90+
}, null, scheduler);
91+
});
92+
93+
reactiveAssert(t, results.messages, [
94+
onError(200, error)
95+
]);
96+
97+
t.end();
98+
});
99+
100+
test('Observable.start with context', function (t) {
101+
var context = { value: 42 };
102+
103+
var scheduler = new TestScheduler();
104+
105+
var results = scheduler.startScheduler(function () {
106+
return Observable.start(function () {
107+
return this.value;
108+
}, context, scheduler);
109+
});
110+
111+
reactiveAssert(t, results.messages, [
112+
onNext(200, 42),
113+
onCompleted(200)
114+
]);
115+
116+
t.end();
117+
});

0 commit comments

Comments
 (0)