本文整理汇总了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();
}));
})