electron#DownloadItem TypeScript Examples

The following examples show how to use electron#DownloadItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: helper.ts    From electron-playground with MIT License 5 votes vote down vote up
getDownloadItem = (data: IDownloadFile[], id: string): DownloadItem | null => {
  const newData = data.filter(item => item.id === id)

  if (!newData.length) return null
  return newData[0]?._sourceItem || null
}
Example #2
Source File: index.ts    From electron-playground with MIT License 5 votes vote down vote up
listenerDownload = async (
  event: Event,
  item: DownloadItem,
  webContents: WebContents,
): Promise<void> => {
  // 新建下载为空时,会执行 electron 默认的下载处理
  if (!newDownloadItem) return

  let prevReceivedBytes = 0 // 记录上一次下载的字节数据
  // 添加下载项
  const downloadItem: IDownloadFile = await addDownloadItem({
    item,
    downloadIds: tempDownloadItemIds,
    data: downloadItemData,
    newDownloadItem,
  })

  setTaskbar(downloadItemData, downloadCompletedIds, -1, win)

  // 新下载任务创建完成,渲染进程监听该事件,添加到下载管理器列表
  webContents.send('newDownloadItem', { ...downloadItem, _sourceItem: null })

  // 更新下载
  item.on('updated', (e, state) => {
    const receivedBytes = updateDownloadItem({
      item,
      downloadItem,
      data: downloadItemData,
      prevReceivedBytes,
      state,
    })
    prevReceivedBytes = receivedBytes

    // 获取所有下载中的接受字节和总字节数据
    const bytes = getDownloadBytes(downloadItemData)
    // 更新任务栏进度
    win?.setProgressBar(bytes.receivedBytes / bytes.totalBytes)
    // 通知渲染进程,更新下载状态
    webContents.send('downloadItemUpdate', { ...downloadItem, _sourceItem: null })
  })

  // 下载完成
  item.on('done', (e, state) => {
    downloadItem.state = state
    downloadItem.receivedBytes = item.getReceivedBytes()

    if (state !== 'cancelled') {
      downloadCompletedIds.push(downloadItem.id)
    }

    setTaskbar(downloadItemData, downloadCompletedIds, 0, win)
    // 下载成功
    if (state === 'completed' && process.platform === 'darwin') {
      app.dock.downloadFinished(downloadItem.path)
    }

    setDownloadStore(downloadItemData)
    // 通知渲染进程,更新下载状态
    webContents.send('downloadItemDone', { ...downloadItem, _sourceItem: null })
  })
}