Commit 28273b4a by yixinjj

优化一些逻辑和补充注释

1 parent 521c4c00
module.exports = {
env: {
es2021: true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:vue/recommended',
'plugin:prettier/recommended',
],
overrides: [],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['vue'],
rules: {
'no-underscore-dangle': 'off',
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-unused-vars': 'warn',
'vue/component-name-in-template-casing': ['warn', 'kebab-case'],
'vue/multi-word-component-names': 'off',
'prefer-destructuring': 'off',
'no-plusplus': 'off',
},
globals: {
uni: 'readonly',
plus: 'readonly',
getCurrentPages: 'readonly',
getApp: 'readonly',
},
ignorePatterns: ['./pages.json', 'uni_modules', 'unpackage'],
};
\ No newline at end of file
{
"printWidth": 800,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "none",
"semi": false,
"wrap_line_length": 820,
"wrap_attributes": "auto",
"proseWrap": "always",
"arrowParens": "avoid",
"bracketSpacing": true,
"htmlWhitespaceSensitivity": "ignore",
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
"jsxBracketSameLine": true,
"files.insertFinalNewline": true,
"prettier.ignorePath": ".prettierignore",
"useTabs": false,
"eslintIntegration": false,
"overrides": [
{
"files": ".prettierrc",
"options": {
"parser": "json"
}
}
],
"endOfLine": "auto"
}
This diff could not be displayed because it is too large.
{
"dependencies": {
"dayjs": "^1.11.11",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"hammerjs": "^2.0.8",
"prettier": "^3.3.2",
"webpack-spritesmith": "^1.1.0"
}
}
import { storageApi } from "./cache";
import utils from "./utils";
import { storageApi } from './cache'
import utils from './utils'
/**
* 记录管理器类
* 用于管理记录列表
......@@ -13,9 +13,9 @@ class RecordManager {
// this.fileSaveFolderOnClient = utils.getFileSaveFolderOnClient();
}
// 单例模式
static instance = null;
static instance = null
// 读写锁
locker = false;
locker = false
/**
* 获取 RecordManager 的单例实例
* 如果 instance 不存在,则创建一个新实例并赋值给 instance
......@@ -23,9 +23,9 @@ class RecordManager {
*/
static getInstance() {
if (!RecordManager.instance) {
RecordManager.instance = new RecordManager();
RecordManager.instance = new RecordManager()
}
return RecordManager.instance;
return RecordManager.instance
}
/**
* 异步获取锁
......@@ -34,22 +34,22 @@ class RecordManager {
* @return {Promise} 一个 Promise,在锁可用时兑现,并返回一个解锁函数
*/
async lock() {
return new Promise((resolve) => {
return new Promise(resolve => {
if (this.locker) {
setTimeout(() => {
this.lock();
}, 100);
this.lock()
}, 100)
} else {
this.locker = true;
resolve(this.unlock.bind(this));
this.locker = true
resolve(this.unlock.bind(this))
}
});
})
}
/**
* 释放本地存储的读写锁
*/
unlock() {
this.locker = false;
this.locker = false
}
/**
......@@ -59,10 +59,10 @@ class RecordManager {
* @returns {Array} - 包含新记录的更新后的记录列表
*/
async addRecord(record) {
const records = this.getRecords() || [];
records.push(record);
this.setRecords(records);
return records;
const records = this.getRecords() || []
records.push(record)
this.setRecords(records)
return records
}
/**
* 移除一个ID对应的记录
......@@ -71,10 +71,10 @@ class RecordManager {
* @returns {Array} - 移除记录后的更新后的记录列表
*/
async removeRecord(id) {
let records = this.getRecords();
records = records.filter((i) => i.id !== id);
this.setRecords(records);
return records;
let records = this.getRecords()
records = records.filter(i => i.id !== id)
this.setRecords(records)
return records
}
/**
* 获取并移除当前对象记录列表中的最后一条记录
......@@ -82,8 +82,8 @@ class RecordManager {
* @returns {Object} - 被移除的最后一条记录对象,如果记录列表为空,则返回 undefined
*/
getRecord() {
const records = this.getRecords();
return records.pop();
const records = this.getRecords()
return records.pop()
}
/**
* 根据给定的 ID 获取一条记录
......@@ -93,8 +93,8 @@ class RecordManager {
*
*/
getRecordById(id) {
const records = this.getRecords();
return records.find((i) => i.id === id);
const records = this.getRecords()
return records.find(i => i.id === id)
}
/**
* 获取记录列表
......@@ -102,7 +102,7 @@ class RecordManager {
* @returns {Array} - 从 storageApi 获取的记录列表
*/
getRecords() {
return storageApi.getRecords();
return storageApi.getRecords()
}
/**
* 设置记录列表
......@@ -111,41 +111,63 @@ class RecordManager {
* @returns {void} - 此函数不返回任何值
*/
setRecords(records) {
storageApi.setRecords(records);
storageApi.setRecords(records)
}
/**
* 更新记录数组中的记录
* 这个函数用于更新给定记录数组中的特定记录
* 它接受一个参数 record,该参数是要更新的记录对象
*
* @param {Object} record - 要更新的记录对象,必须包含一个唯一的 id 属性
* @throws {Error} 如果记录数组中找不到具有给定 id 的记录,将抛出错误
*/
updateRecord(record) {
const records = this.getRecords();
const index = records.findIndex((i) => i.id === record.id);
const records = this.getRecords()
const index = records.findIndex(i => i.id === record.id)
if (index !== -1) {
records[index] = record;
this.setRecords(records);
records[index] = record
this.setRecords(records)
}
}
/**
* 兼容模式的异步处理函数
* 这个函数用于处理在兼容模式下的文件路径转换和记录属性的修正
* 它首先获取一个锁,以确保在操作过程中数据的一致性
* 然后,它获取客户端文件保存文件夹的路径,并检查是否获取成功
* 接着,它获取所有的记录,并对每个记录执行以下操作:
* - 检查记录的 recordUrlList 是否存在且不为空,对于每个 URL,判断其是否以 'file://' 开头
* - 如果不是,使用路径中的文件名构建一个新的客户端文件路径,并替换掉原来的 URL
* - 如果记录没有 id 属性,随机生成一个 UUID 并设置为记录的 id
*
* 在处理过程中,如果发生任何错误,它将抛出该错误
* 无论成功或失败,最终都会执行 finally 块中的代码。这里首先释放记录锁,然后调用 polling 方法,可能是为了继续下一次文件上传。
*
* @async
* @throws {Error} 如果获取记录失败或发生任何其他错误,将抛出错误
*/
async compatible() {
const unlock = await this.lock();
let path = await utils.getFileSaveFolderOnClient();
const unlock = await this.lock()
let path = await utils.getFileSaveFolderOnClient()
try {
let records = this.getRecords();
records.forEach((i) => {
let records = this.getRecords()
records.forEach(i => {
if (i.recordUrlList && i.recordUrlList.length) {
i.recordUrlList.forEach(async (j) => {
if (!j.file.startsWith("file://")) {
const fileName = j.file.split("/").pop();
j.file = path + fileName;
i.recordUrlList.forEach(async j => {
if (!j.file.startsWith('file://')) {
const fileName = j.file.split('/').pop()
j.file = path + fileName
}
});
})
}
if (!i.id) {
i.id = utils.uuid();
i.id = utils.uuid()
}
});
console.log(records);
this.setRecords(records);
})
this.setRecords(records)
} catch (e) {
throw e;
throw e
} finally {
unlock();
unlock()
}
}
......@@ -155,7 +177,7 @@ class RecordManager {
* @returns {void} - 此函数不返回任何值
*/
clearRecords() {
storageApi.removeRecords();
storageApi.removeRecords()
}
}
export default RecordManager;
export default RecordManager
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!