本文整理匯總了TypeScript中@reactivex/rxjs.Observable.from方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Observable.from方法的具體用法?TypeScript Observable.from怎麽用?TypeScript Observable.from使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@reactivex/rxjs.Observable
的用法示例。
在下文中一共展示了Observable.from方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1:
this.action$ = this.inStream$.flatMap(v => {
if (v instanceof Rx.Observable) {
return v;
} else {
return Rx.Observable.from([v]);
}
}).share();
示例2: Subject
import { Observable, Subject } from '@reactivex/rxjs';
const subject = new Subject();
const tick = Observable.from([1, 2, 3]);
const multicasted = tick.multicast(subject).refCount(); // 引用計數
multicasted.subscribe(v => console.log(`A: ${v}`));
multicasted.subscribe(v => console.log(`B: ${v}`), null, () => console.log('complete'));
// 顯式調用
// multicasted.connect();
示例3:
import { Observable } from '@reactivex/rxjs'
/*
A ------> |
B ------> | -> E
C -> D -> |
*/
const A$ = Observable.interval(1000)
const B$ = Observable.of(3)
const C$ = Observable.from([5, 6, 7])
const D$ = C$.toArray().map(arr => arr.reduce((acc, val) => acc + val, 0))
const E$ = Observable.combineLatest(A$, B$, D$).map(arr => arr.reduce((acc, val) => acc + val, 0))
E$.subscribe(observer => {
console.log(observer)
})
示例4: Promise
/**
* @author happygrammer
*/
import * as Rx from "@reactivex/rxjs";
Rx.Observable.from([1, 2, 3]).subscribe(val => console.log(val));
Rx.Observable.from(new Promise(resolve => resolve("Hello!"))).subscribe(val => console.log(val));
const map = new Map();
map.set(1, 'a');
map.set(2, 'b');
Rx.Observable.from(map).subscribe(val => console.log(val));
示例5:
/**
* @author happygrammer
*/
import * as Rx from "@reactivex/rxjs";
const streamData$ = Rx.Observable.from([1, 2, 3, 4, 5]);
const map = streamData$.map(val => val + 10);
const mapSubscribe = map.subscribe(val => console.log(val));
示例6:
interface ClickEvent {
event: string
}
interface Person {
name: string,
html_url: string,
login: string
}
var refreshClickStream = Rx.Observable.fromEvent<ClickEvent>(refreshButton, 'click');
var close1ClickStream = Rx.Observable.fromEvent<ClickEvent>(closeButton1, 'click');
var close2ClickStream = Rx.Observable.fromEvent<ClickEvent>(closeButton2, 'click');
var close3ClickStream = Rx.Observable.fromEvent<ClickEvent>(closeButton3, 'click');
var startupRequestStream = Rx.Observable.from([url]);
var requestStream = refreshClickStream.startWith({event: 'first click'})
.map(function() {
var randomOffset = Math.floor(Math.random()*500);
return '/api?since=' + randomOffset;
});
var responseStream = requestStream
.flatMap(function(requestUrl) {
return Rx.Observable.fromPromise(fetch(requestUrl)
.then(function(response) {
return response.json();
}));
})