rxjs 5.1.0中的强类型Observable.create

时间:2019-02-20 14:27:06

标签: rxjs

我正在使用Observable.create从xhr引发下载事件。

我有此代码:

return Observable.create((observer) => {
  xhr.open('GET', url);

  xhr.send();

  xhr.addEventListener('progress', (progress) => {
    const percentCompleted = percentCompleted = Math.round((progress.loaded / progress.total) * 100);

    observer.next({ state: DownloadState.progress, progress: `${percentCompleted}%` });
  });

  xhr.addEventListener('load', (e: any) => {
    if (e.target['status'] !== 200) {
      observer.error({ state: DownloadState.failure, error: e.target });
    } else {
      observer.next({ state: DownloadState.progress, response: e.target });
      observer.complete();
    }
  });

  xhr.addEventListener('error', (err) => {
    console.log('upload error', err);

    observer.error({ state: DownloadState.failure, error: err.message });
  });

  xhr.addEventListener('abort', (abort) => {
    console.log('upload abort', abort);

    observer.error({ state: DownloadState.failure, error: 'Transfer aborted by the user' });
  });

  return () => xhr.abort();
});

}

无法在5.1.0中强行输入Observable.create

我应该创建一个BehaviorSubject吗?

如何将我的代码重构为强类型版本?

0 个答案:

没有答案