【问题标题】:RxJS Subject of Subjects主题的 RxJS 主题
【发布时间】:2018-07-30 02:49:34
【问题描述】:

我正在尝试创建一个下载资产列表的主题,该列表为每个资产发送操作,如果可能的话,使用主题的主题?

   export function onDownloadGuide(action$,store){
  return action$.ofType(DOWNLOAD_GUIDE)
    .mergeMap(() => downloadGuideAssets().map(res => downloadGuideAssetProgress(res)))
}

function downloadGuideAssets(){
  const subject$ = new Subject()
  getAssetList().map((asset) => downloadAsset(asset).map(res => {console.log(res);subject$.next(res)}))
  return subject$.asObservable()
}

function downloadAsset({id,src}){

  const subject$ = new Subject()

  window.resolveLocalFileSystemURL(cordova.file.dataDirectory, dirEntry => {
    dirEntry.getFile(src.substring(src.lastIndexOf('/')+1),{create:true, exclusive:true}, f => {
      fetch(src).then(fetchProgress({onProgress(progress) {console.log('progressevent');subject$.next({id,progress})}}))
        .then(res => res.blob())
        .then(blob =>
          f.createWriter(writer => {
            writer.onwriteend = ()=> subject$.next({id,complete:true})
            writer.write(blob)
          }))
        .catch(err => subject$.next({id,error:err}))

    }, err => subject$.next({id,error:err}) )
  })

  return subject$.asObservable()
}

文件似乎正在被下载等。我可以通过控制台记录进度 - 当我尝试映射结果时,没有任何东西被触发。

这是因为我是从订阅开始的吗?

【问题讨论】:

    标签: rxjs redux-thunk subject-observer


    【解决方案1】:

    可以像下面这样进一步简化并像downloadGuideAssets().subscribe()一样使用它,你真的不需要主题

    function downloadGuideAssets(){
      return getAssetList().map((asset) => downloadAsset(asset))
    }
    
    function downloadAsset({id,src}){
    return Observable.create(obs=>{
      window.resolveLocalFileSystemURL(cordova.file.dataDirectory, dirEntry => {
        dirEntry.getFile(src.substring(src.lastIndexOf('/')+1),{create:true, exclusive:false}, f => {
          fetch(src).then(fetchProgress({onProgress(progress) {subject$.next({id,progress})}}))
            .then(res => res.blob())
            .then(blob =>
              f.createWriter(writer => {
                writer.onwriteend = ()=> obs.next({id,complete:true})
                writer.write(blob)
              }))
            .catch(err => obs.error({id,error:err}))
        }, err => obs.error({id,error:err}) )
      })
    })
    }
    

    【讨论】:

      【解决方案2】:

      我最终制作了一个主题并返回了该主题

      function downloadGuideAssets(){
        const subject$ = new Subject()
        getAssetList().map((asset) => downloadAsset(asset,subject$))
        return subject$.asObservable()
      }
      
      function downloadAsset({id,src},subject$){
      
        window.resolveLocalFileSystemURL(cordova.file.dataDirectory, dirEntry => {
          dirEntry.getFile(src.substring(src.lastIndexOf('/')+1),{create:true, exclusive:false}, f => {
            fetch(src).then(fetchProgress({onProgress(progress) {subject$.next({id,progress})}}))
              .then(res => res.blob())
              .then(blob =>
                f.createWriter(writer => {
                  writer.onwriteend = ()=> subject$.next({id,complete:true})
                  writer.write(blob)
                }))
              .catch(err => subject$.next({id,error:err}))
          }, err => subject$.next({id,error:err}) )
        })
      }
      

      【讨论】:

        猜你喜欢
        • 2019-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-07
        • 2021-12-30
        • 2016-12-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多