0

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

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

这些文件似乎正在下载等。我可以控制台记录进度 - 当我尝试映射结果时,什么都没有被触发。

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

4

2 回答 2

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}) )
  })
})
}
于 2018-07-30T05:21:29.000 回答
0

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

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}) )
  })
}
于 2018-07-30T04:32:13.417 回答