當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Observable.from方法代碼示例

本文整理匯總了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();
開發者ID:tomachalek,項目名稱:kontext,代碼行數:8,代碼來源:dispatcher.ts

示例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();
開發者ID:taochunyu,項目名稱:learn,代碼行數:11,代碼來源:subject.ts

示例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)
})
開發者ID:taochunyu,項目名稱:learn,代碼行數:17,代碼來源:combe.ts

示例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));
開發者ID:chun4foryou,項目名稱:develop,代碼行數:12,代碼來源:from.ts

示例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));
開發者ID:chun4foryou,項目名稱:develop,代碼行數:8,代碼來源:map.ts

示例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();
      }));
  })
開發者ID:skseth,項目名稱:tasks,代碼行數:31,代碼來源:app.ts


注:本文中的@reactivex/rxjs.Observable.from方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。