| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- // import { saveData, getData } from "@/utils/indexedDb";
- // import { stringify, parse } from "flatted"; // 用于处理循环引用 压缩数据处理
- // import pako from "pako";
- // import { Message } from "element-ui";
- // // 通用分片函数
- // const splitIntoChunks = (data, chunkSize) => {
- // const chunks = [];
- // for (let i = 0; i < data.length; i += chunkSize) {
- // chunks.push(data.slice(i, i + chunkSize));
- // }
- // return chunks;
- // };
- // // 修改后的 openDB 方法
- // const openDB = (dbName, version) => {
- // return new Promise((resolve, reject) => {
- // const request = indexedDB.open(dbName, version);
- // request.onsuccess = () => {
- // resolve(request.result);
- // };
- // request.onerror = (error) => {
- // reject(error);
- // };
- // request.onupgradeneeded = (event) => {
- // const db = event.target.result;
- // // 创建对象存储(如果不存在)
- // if (!db.objectStoreNames.contains("dragChart_chunk_meta")) {
- // db.createObjectStore("dragChart_chunk_meta");
- // }
- // if (!db.objectStoreNames.contains("dragChart_compressed")) {
- // db.createObjectStore("dragChart_compressed");
- // }
- // if (!db.objectStoreNames.contains("myIndexedDB")) {
- // db.createObjectStore("myIndexedDB");
- // }
- // };
- // });
- // };
- // // vuex 插件
- // const vuexIndexedDBPlugin = (store) => {
- // const dbName = "vuexDB";
- // const keyName = "vuexState";
- // // 过滤 Vuex 状态,去掉不可序列化的部分
- // const filterState = (state) => {
- // const clonedState = JSON.parse(
- // JSON.stringify(state, (key, value) => {
- // // 如果是函数或无法克隆的对象,返回 undefined
- // if (typeof value === "function") return undefined;
- // return value;
- // })
- // );
- // return clonedState;
- // };
- // // 初始化 IndexedDB 并加载状态
- // openDB(dbName, 1).then(() => {
- // getData(dbName, keyName).then((savedState) => {
- // if (savedState) {
- // store.replaceState({
- // ...store.state,
- // ...savedState,
- // });
- // }
- // });
- // });
- // // 订阅 Vuex mutations,每次状态变更时保存到 IndexedDB
- // store.subscribe((mutation, state) => {
- // if (mutation.type.startsWith("dragChart/")) {
- // const dragChartState = state.dragChart;
- // try {
- // const cleanedState = JSON.parse(
- // JSON.stringify(dragChartState, (key, value) => {
- // if (
- // typeof value === "function" ||
- // value instanceof HTMLElement ||
- // value === undefined
- // ) {
- // return undefined;
- // }
- // return value;
- // })
- // );
- // // 压缩或分片存储
- // const chunkSize = 500;
- // if (
- // cleanedState.currentChartList &&
- // Array.isArray(cleanedState.currentChartList)
- // ) {
- // const chunks = splitIntoChunks(
- // cleanedState.currentChartList,
- // chunkSize
- // );
- // chunks.forEach((chunk, index) => {
- // const chunkKey = `dragChart_chunk_${index}`;
- // saveData("myIndexedDB", chunkKey, chunk)
- // .then(() => {
- // console.log(`Chunk ${index} saved successfully`);
- // })
- // .catch((error) =>
- // console.error(`Failed to save chunk ${index}:`, error)
- // );
- // });
- // saveData("myIndexedDB", "dragChart_chunk_meta", {
- // totalChunks: chunks.length,
- // });
- // }
- // // 存储压缩的元数据
- // const compressedData = pako.deflate(stringify(cleanedState), {
- // to: "string",
- // });
- // saveData("myIndexedDB", "dragChart_compressed", compressedData)
- // .then(() => {
- // //console.log("Compressed metadata saved")
- // })
- // .catch((error) =>
- // console.error("Failed to save compressed metadata:", error)
- // );
- // } catch (error) {
- // console.error("Error processing dragChart state:", error);
- // Message({
- // message: "数据存储已满,请刷新页面进行数据清除" + error,
- // type: "error",
- // });
- // }
- // }
- // });
- // };
- // export default vuexIndexedDBPlugin;
- import { saveData, getData } from "@/utils/indexedDb";
- import { Message } from "element-ui";
- // 通用的过滤 Vuex 状态,去掉不可序列化的部分
- const filterState = (state) => {
- const clonedState = JSON.parse(
- JSON.stringify(state, (key, value) => {
- // 如果是函数或无法克隆的对象,返回 undefined
- if (
- typeof value === "function" ||
- value instanceof HTMLElement ||
- value === undefined
- ) {
- return undefined;
- }
- return value;
- })
- );
- return clonedState;
- };
- // // 修改后的 openDB 方法
- // const openDB = (dbName, version) => {
- // return new Promise((resolve, reject) => {
- // const request = indexedDB.open(dbName, version);
- // request.onsuccess = () => {
- // resolve(request.result);
- // };
- // request.onerror = (error) => {
- // reject(error);
- // };
- // request.onupgradeneeded = (event) => {
- // const db = event.target.result;
- // // 创建对象存储(如果不存在)
- // if (!db.objectStoreNames.contains("myIndexedDB")) {
- // db.createObjectStore("myIndexedDB");
- // }
- // };
- // });
- // };
- const openDB = (dbName, version) => {
- return new Promise((resolve, reject) => {
- const request = indexedDB.open(dbName, version);
- request.onsuccess = () => {
- resolve(request.result);
- };
- request.onerror = (error) => {
- reject(error);
- };
- request.onupgradeneeded = (event) => {
- const db = event.target.result;
- // 创建对象存储,确保名称为 vuexData(与getData中的名称一致)
- if (!db.objectStoreNames.contains("vuexData")) {
- db.createObjectStore("vuexData");
- }
- };
- });
- };
- // vuex 插件
- const vuexIndexedDBPlugin = (store) => {
- const dbName = "vuexDB";
- const keyName = "vuexState";
- // 初始化 IndexedDB 并加载状态
- openDB(dbName, 1).then(() => {
- getData(dbName, keyName).then((savedState) => {
- if (savedState) {
- store.replaceState({
- ...store.state,
- ...savedState,
- });
- }
- });
- });
- // 订阅 Vuex mutations,每次状态变更时保存到 IndexedDB
- store.subscribe((mutation, state) => {
- if (mutation.type.startsWith("dragChart/")) {
- const dragChartState = state.dragChart;
- try {
- // 过滤不需要保存的内容
- const cleanedState = filterState(dragChartState);
- // 直接保存整个状态到 IndexedDB
- saveData("vuexData", keyName, cleanedState)
- .then(() => {
- console.log("State saved successfully");
- })
- .catch((error) => {
- console.error("Failed to save state:", error);
- Message({
- message: "数据存储失败:" + error,
- type: "error",
- });
- });
- } catch (error) {
- console.error("Error processing dragChart state:", error);
- Message({
- message: "数据存储失败,请刷新页面进行数据清除:" + error,
- type: "error",
- });
- }
- }
- });
- };
- export default vuexIndexedDBPlugin;
|