RxJS 主题的主题

RxJS Subject of Subjects

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

   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()
}

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

这是因为我开始订阅了吗?

我最后做了一个主题并返回了那个

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}) )
  })
}

可以像下面这样进一步简化并像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}) )
  })
})
}