最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3利用組合式函數(shù)和Shared Worker實現(xiàn)后臺分片上傳

 更新時間:2023年10月27日 07:46:37   作者:Aoba_xu  
這篇文章主要為大家詳細介紹了Vue3如何利用組合式函數(shù)和Shared Worker實現(xiàn)后臺分片上傳(帶哈希計算),感興趣的小伙伴可以跟隨小編一起學習一下

1.背景

最近項目需求里有個文件上傳功能,而客戶需求里的文件基本上是比較大的,基本上得有 1 GiB 以上的大小,而上傳大文件尤其是讀大文件,可能會造成卡 UI 或者說點不動的問題。而用后臺的 Worker 去實現(xiàn)是一個比較不錯的解決辦法。

2.原理講解

2.1Shared Worker

Shared Worker 的好處是可以從幾個瀏覽上下文中訪問,例如幾個窗口、iframe 或其他 worker。這樣我們可以保證全局的頁面上傳任務都在我們的控制之下,甚至可以防止重復提交等功能。

2.2組合式函數(shù)

組合式函數(shù)的好處是在 Vue 3 是可以在任何 *.vue 文件中使用,并且是響應式方法,可以偵聽 pinia 內(nèi) token 等的變化,傳遞給 Worker

2.3簡單流程設計

3.代碼

upload-worker.ts 代碼

import { sha256 } from '@noble/hashes/sha256';
import { bytesToHex as toHex } from '@noble/hashes/utils';
interface SharedWorkerGlobalScope {
  onconnect: (event: MessageEvent<any>) => void;
}
const _self: SharedWorkerGlobalScope = self as any;
/**
 * 分片大小
 */
const pieceSize = 1024 * 1024;
/**
 * 消息參數(shù)
 */
interface MessageArg<T> {
  /**
   * 函數(shù)名
   */
  func: string;
  /**
   * 參數(shù)
   */
  arg: T;
}
/**
 * 上傳任務信息
 */
interface UploadTaskInfo {
  /**
   * 文件名
   */
  fileName: string;
  /**
   * 上傳路徑
   */
  uploadPath: string;
  /**
   * 任務 id
   */
  id: string;
  /**
   * 文件大小
   */
  size: number;
  /**
   * 上傳進度
   */
  progress: number;
  /**
   * 上傳速度
   */
  speed?: number;
  /**
   * 任務狀態(tài)
   */
  status: 'uploading' | 'paused' | 'canceled' | 'done' | 'error' | 'waiting';
  /**
   * 開始時間
   */
  startTime?: Date;
  /**
   * 結束時間
   */
  endTime?: Date;
  /**
   * 錯誤信息
   */
  errorMessage?: string;
}
/**
 * 上傳任務
 */
interface UploadTask extends UploadTaskInfo {
  file: File;
  pieces: Array<boolean>;
  abort?: AbortController;
}
/**
 * 任務/哈希值映射
 */
const hashs = new Map();
/**
 * 上傳任務列表
 */
const uploadTasks: Array<UploadTask> = [];
/**
 * 狀態(tài)接收器
 */
const statusReceivers = new Map<string, MessagePort>();
/**
 * token 倉庫
 */
const tokenStore = {
  /**
   * token
   */
  BearerToken: '',
};
/**
 * 返回上傳狀態(tài)
 * @param task 上傳任務
 */
const updateStatus = (task: UploadTaskInfo) => {
  const taskInfo: UploadTaskInfo = {
    fileName: task.fileName,
    uploadPath: task.uploadPath,
    id: task.id,
    size: task.size,
    progress: task.progress,
    speed: task.speed,
    status: task.status,
    startTime: task.startTime,
    endTime: task.endTime,
    errorMessage: task.errorMessage,
  };
  statusReceivers.forEach((item) => {
    item.postMessage(taskInfo);
  });
};
/**
 * 運行上傳任務
 * @param task 上傳任務
 */
const runUpload = async (task: UploadTask) => {
  task.status = 'uploading';
  const hash = hashs.get(task.id) || sha256.create();
  hashs.set(task.id, hash);
  let retryCount = 0;
  const abort = new AbortController();
  task.abort = abort;
  while (task.status === 'uploading') {
    const startTime = Date.now();
    const index = task.pieces.findIndex((item) => !item);
    if (index === -1) {
      try {
        const response: { code: number; message: string } = await fetch(
          '/api/File/Upload',
          {
            method: 'PUT',
            headers: {
              Authorization: tokenStore.BearerToken,
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({
              id: task.id,
              fileHash: toHex(hash.digest()),
              filePath: task.uploadPath,
            }),
          }
        ).then((res) => res.json());
        if (response.code !== 200) {
          throw new Error(response.message);
        }
        task.status = 'done';
        task.endTime = new Date();
        updateStatus(task);
      } catch (e: any) {
        task.status = 'error';
        task.errorMessage = e.toString();
        task.endTime = new Date();
        deleteUpload(task.id);
        updateStatus(task);
      }
      break;
    }
    const start = index * pieceSize;
    const end = start + pieceSize >= task.size ? task.size : start + pieceSize;
    const buffer = task.file.slice(index * pieceSize, end);
    hash.update(new Uint8Array(await buffer.arrayBuffer()));
    const form = new FormData();
    form.append('file', buffer);
    let isTimeout = false;
    try {
      const timer = setTimeout(() => {
        isTimeout = true;
        abort.abort();
      }, 8000);
      const response: { code: number; message: string } = await fetch(
        `/api/File/Upload?id=${task.id}&offset=${start}`,
        {
          method: 'POST',
          body: form,
          headers: {
            Authorization: tokenStore.BearerToken,
          },
          signal: abort.signal,
        }
      ).then((res) => res.json());
      clearTimeout(timer);
      if (response.code !== 200) {
        throw new Error(response.message);
      }
      task.pieces[index] = true;
      task.progress =
        task.pieces.filter((item) => item).length / task.pieces.length;
      task.speed = (pieceSize / (Date.now() - startTime)) * 1000;
      updateStatus(task);
    } catch (e: any) {
      retryCount++;
      if (retryCount > 3) {
        task.status = 'error';
        if (isTimeout) {
          task.errorMessage = 'UploadTimeout';
        } else {
          task.errorMessage = e.toString();
        }
        task.endTime = new Date();
        deleteUpload(task.id);
        updateStatus(task);
      }
    }
    runNextUpload();
  }
};
/**
 * 運行下一個上傳任務
 */
const runNextUpload = async () => {
  if (uploadTasks.filter((item) => item.status === 'uploading').length > 3) {
    return;
  }
  const task = uploadTasks.find((item) => item.status === 'waiting');
  if (task) {
    await runUpload(task);
  }
};
/**
 * 排隊上傳
 * @param e 消息事件
 */
const queueUpload = async (
  e: MessageEvent<
    MessageArg<{
      id: string;
      file: File;
      uploadPath: string;
    }>
  >
) => {
  uploadTasks.push({
    file: e.data.arg.file,
    fileName: e.data.arg.file.name,
    id: e.data.arg.id,
    uploadPath: e.data.arg.uploadPath,
    size: e.data.arg.file.size,
    progress: 0,
    speed: 0,
    status: 'waiting',
    pieces: new Array(Math.ceil(e.data.arg.file.size / pieceSize)).fill(false),
    errorMessage: undefined,
  });
  updateStatus(uploadTasks[uploadTasks.length - 1]);
  await runNextUpload();
};
/**
 * 注冊狀態(tài)接收器
 * @param e 消息事件
 * @param sender 發(fā)送者
 */
const registerStatusReceiver = (
  e: MessageEvent<MessageArg<string>>,
  sender?: MessagePort
) => {
  if (sender) statusReceivers.set(e.data.arg, sender);
};
/**
 * 注銷狀態(tài)接收器
 * @param e 消息事件
 */
const unregisterStatusReceiver = (e: MessageEvent<MessageArg<string>>) => {
  statusReceivers.delete(e.data.arg);
};
/**
 * 更新 token
 * @param e 消息事件
 */
const updateToken = (e: MessageEvent<MessageArg<string>>) => {
  tokenStore.BearerToken = 'Bearer ' + e.data.arg;
};
/**
 * 暫停上傳
 * @param e 消息事件
 */
const pauseUpload = (e: MessageEvent<MessageArg<string>>) => {
  const task = uploadTasks.find((item) => item.id === e.data.arg);
  if (task) {
    task.status = 'paused';
    if (task.abort) {
      task.abort.abort();
    }
    updateStatus(task);
  }
};
/**
 * 取消上傳
 * @param e 消息事件
 */
const cancelUpload = (e: MessageEvent<MessageArg<string>>) => {
  const task = uploadTasks.find((item) => item.id === e.data.arg);
  if (task) {
    task.status = 'canceled';
    if (task.abort) {
      task.abort.abort();
    }
    deleteUpload(task.id);
    updateStatus(task);
  }
};
/**
 * 刪除上傳
 * @param id 任務 id
 */
const deleteUpload = async (id: string) => {
  uploadTasks.splice(
    uploadTasks.findIndex((item) => item.id === id),
    1
  );
  hashs.delete(id);
  await fetch(`/api/File/Upload?id=${id}`, {
    method: 'DELETE',
    headers: {
      Authorization: tokenStore.BearerToken,
    },
  }).then((res) => res.json());
};
/**
 * 消息路由
 */
const messageRoute = new Map<
  string,
  (e: MessageEvent<MessageArg<any>>, sender?: MessagePort) => void
>([
  ['queueUpload', queueUpload],
  ['registerStatusReceiver', registerStatusReceiver],
  ['updateToken', updateToken],
  ['pauseUpload', pauseUpload],
  ['cancelUpload', cancelUpload],
  ['unregisterStatusReceiver', unregisterStatusReceiver],
]);
// 監(jiān)聽連接
_self.onconnect = (e) => {
  const port = e.ports[0];
  port.onmessage = async (e) => {
    // 調(diào)用函數(shù)
    const func = messageRoute.get(e.data.func);
    if (func) {
      func(e, port);
    }
  };
  port.start();
};

upload-service.ts 代碼

import UploadWorker from './upload-worker?sharedworker';
import { onUnmounted, ref, watch } from 'vue';
import { storeToRefs } from 'pinia';
import { useAuthStore } from 'src/stores/auth';
/**
 * 上傳任務信息
 */
interface UploadTaskInfo {
  /**
   * 文件名
   */
  fileName: string;
  /**
   * 上傳路徑
   */
  uploadPath: string;
  /**
   * 任務 id
   */
  id: string;
  /**
   * 文件大小
   */
  size: number;
  /**
   * 上傳進度
   */
  progress: number;
  /**
   * 上傳速度
   */
  speed?: number;
  /**
   * 任務狀態(tài)
   */
  status: 'uploading' | 'paused' | 'canceled' | 'done' | 'error' | 'waiting';
  /**
   * 開始時間
   */
  startTime?: Date;
  /**
   * 結束時間
   */
  endTime?: Date;
  /**
   * 錯誤信息
   */
  errorMessage?: string;
}
/**
 * 上傳服務
 */
export const useUploadService = () => {
  const store = storeToRefs(useAuthStore());
  // 創(chuàng)建共享 worker
  const worker = new UploadWorker();
  /**
   * 上傳任務列表
   */
  const uploadTasks = ref<Map<string, UploadTaskInfo>>(
    new Map<string, UploadTaskInfo>()
  );
  // 是否已注冊狀態(tài)接收器
  const isRegistered = ref(false);
  // 服務 id
  const serviceId = crypto.randomUUID();
  // 監(jiān)聽上傳任務列表變化(只有在注冊狀態(tài)接收器后才會收到消息)
  worker.port.onmessage = (e: MessageEvent<UploadTaskInfo>) => {
    uploadTasks.value.set(e.data.id, e.data);
  };
  // 更新 token
  worker.port.postMessage({
    func: 'updateToken',
    arg: store.token.value,
  });
  watch(store.token, (token) => {
    worker.port.postMessage({
      func: 'updateToken',
      arg: token,
    });
  });
  /**
   * 排隊上傳
   * @param file 文件
   * @param uploadPath 上傳路徑
   */
  const queueUpload = (file: File, uploadPath: string) => {
    worker.port.postMessage({
      func: 'queueUpload',
      arg: {
        id: crypto.randomUUID(),
        file: file,
        uploadPath: uploadPath,
      },
    });
  };
  /**
   * 暫停上傳
   * @param id 任務 id
   */
  const pauseUpload = (id: string) => {
    worker.port.postMessage({
      func: 'pauseUpload',
      arg: id,
    });
  };
  /**
   * 取消上傳
   * @param id 任務 id
   */
  const cancelUpload = (id: string) => {
    worker.port.postMessage({
      func: 'cancelUpload',
      arg: id,
    });
  };
  /**
   * 注冊狀態(tài)接收器
   */
  const registerStatusReceiver = () => {
    worker.port.postMessage({
      func: 'registerStatusReceiver',
      arg: serviceId,
    });
    isRegistered.value = true;
  };
  /**
   * 注銷狀態(tài)接收器
   */
  const unregisterStatusReceiver = () => {
    worker.port.postMessage({
      func: 'unregisterStatusReceiver',
      arg: serviceId,
    });
    isRegistered.value = false;
  };
  onUnmounted(() => {
    unregisterStatusReceiver();
    worker.port.close();
  });
  return {
    uploadTasks,
    queueUpload,
    pauseUpload,
    cancelUpload,
    registerStatusReceiver,
    unregisterStatusReceiver,
  };
};

4.用法

// 引入組合式函數(shù)
const uploadService = useUploadService();
// 注冊狀態(tài)接收器
uploadService.registerStatusReceiver();
// 表單綁定上傳方法
const upload = (file: File, filePath: string) => {
  uploadService.queueUpload(file, filePath);
}
// 監(jiān)聽上傳進度,當然也可以直接展示在界面,畢竟是 Ref
watch(uploadService.uploadTasks, console.log);

以上就是Vue3利用組合式函數(shù)和Shared Worker實現(xiàn)后臺分片上傳的詳細內(nèi)容,更多關于Vue3分片上傳的資料請關注腳本之家其它相關文章!

相關文章

  • vue element 表頭添加斜線的實現(xiàn)代碼

    vue element 表頭添加斜線的實現(xiàn)代碼

    這篇文章主要介紹了vue element 表頭添加斜線的實現(xiàn)代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-11-11
  • vue插件draggable實現(xiàn)拖拽移動圖片順序

    vue插件draggable實現(xiàn)拖拽移動圖片順序

    這篇文章主要為大家詳細介紹了vue插件draggable實現(xiàn)拖拽移動圖片順序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 詳解Vue2.x-directive的學習筆記

    詳解Vue2.x-directive的學習筆記

    這篇文章主要介紹了詳解Vue2.x-directive的學習筆記,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • vue移動端使用appClound拉起支付寶支付的實現(xiàn)方法

    vue移動端使用appClound拉起支付寶支付的實現(xiàn)方法

    這篇文章主要介紹了vue移動端使用appClound拉起支付寶支付的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • vue 中的 render 函數(shù)作用詳解

    vue 中的 render 函數(shù)作用詳解

    這篇文章主要介紹了vue 中的 render 函數(shù)作用,通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • vue封裝jquery修改自身及兄弟元素的方法

    vue封裝jquery修改自身及兄弟元素的方法

    本文主要介紹了vue封裝jquery修改自身及兄弟元素的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • 前端報錯npm ERR! cb() never called!問題解決辦法

    前端報錯npm ERR! cb() never called!問題解決辦法

    最近接手了一個前臺項目,執(zhí)行npm install的時候一直報錯,所以這里就給大家總結下,這篇文章主要給給大家介紹了關于前端報錯npm?ERR! cb() never called!問題的解決辦法,需要的朋友可以參考下
    2024-05-05
  • vue 使用ref 讓父組件調(diào)用子組件的方法

    vue 使用ref 讓父組件調(diào)用子組件的方法

    這篇文章主要介紹了vue 使用ref 讓父組件調(diào)用子組件的方法,需要的朋友可以參考下
    2018-02-02
  • vue 點擊按鈕增加一行的方法

    vue 點擊按鈕增加一行的方法

    今天小編就為大家分享一篇vue 點擊按鈕增加一行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue3 如何使用CryptoJS加密

    Vue3 如何使用CryptoJS加密

    CryptoJS是一個強大的JavaScript庫,它提供了多種加密解密功能,尤其是AES加密方法,使用CryptoJS,我們可以有效地保護數(shù)據(jù)安全,防止信息泄露,通過簡單的安裝和函數(shù)編寫,本文給大家介紹Vue3 如何使用CryptoJS加密,感興趣的朋友一起看看吧
    2024-10-10

最新評論

永州市| 湘乡市| 东乌珠穆沁旗| 建湖县| 甘孜| 临沂市| 招远市| 乌兰浩特市| 泗洪县| 洪江市| 洪泽县| 横山县| 肇源县| 旬阳县| 黔东| 赤壁市| 西充县| 云龙县| 康保县| 商河县| 高青县| 昆山市| 兴和县| 来宾市| 丹巴县| 柳江县| 荔浦县| 北京市| 凌源市| 南安市| 安宁市| 临猗县| 舞钢市| 五原县| 华安县| 贵南县| 海伦市| 西平县| 渭源县| 循化| 神池县|