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


TypeScript Observable.fromEvent方法代碼示例

本文整理匯總了TypeScript中rx-lite.Observable.fromEvent方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Observable.fromEvent方法的具體用法?TypeScript Observable.fromEvent怎麽用?TypeScript Observable.fromEvent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rx-lite.Observable的用法示例。


在下文中一共展示了Observable.fromEvent方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: getDataObservable

  private getDataObservable() {
    let click = Rx.Observable.fromEvent($("#openFile"), "click");

    // fromCallback() converts a function that takes a callback into a new
    // function that takes all of the original function's arguments except the
    // callback and returns an Observable of the callback's return value.
    let opener = Rx.Observable.fromCallback(dialog.showOpenDialog);
    let reader = Rx.Observable.fromCallback(fs.readFile);
    let isValid: boolean;

    // Pipeline of observables to convert a stream of clicks into a stream of
    // validated file data
    return click
      .flatMap(() => opener({ properties: ["openFile"] }) // returns the selected filename (as one-item array)
        .filter(f => f) // Don't emit event if user didn't select a file
        .flatMap(f => reader(f[0], "utf-8")) // map file name stream to file data stream
        .map(d => d[1]) // Hack: Observable-wrapped readFile returns [null, fileData] (?)
        .do(d => {
          isValid = Parser.validateData(d); // put validity info in outer scope...
          if (!isValid) alert("Invalid data");
        })
        .filter(() => isValid)); // ...so .filter() doesn't have to call validateData() again
  }
開發者ID:Ethan826,項目名稱:tablexi-coding-challenge,代碼行數:23,代碼來源:browser.ts


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