index.js
1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { storageApi } from "@/utils/cache";
class RecordManager {
constructor() {}
/**
* 将一条新记录添加到当前对象的记录列表中
*
* @param {Object} record - 要添加的新记录对象
* @returns {Array} - 包含新记录的更新后的记录列表
*/
addRecord(record) {
const records = this.getRecords();
records.push(record);
this.setRecords(records);
return records;
}
/**
* 移除一个ID对应的记录
*
* @param {number} id - 要移除的记录的ID
* @returns {Array} - 移除记录后的更新后的记录列表
*/
removeRecord(id) {
let records = this.getRecords();
records = records.filter((i) => i.id !== id);
this.setRecords(records);
return records;
}
/**
* 获取并移除当前对象记录列表中的最后一条记录
*
* @returns {Object} - 被移除的最后一条记录对象,如果记录列表为空,则返回 undefined
*/
getRecord() {
const records = this.getRecords();
return records.pop();
}
/**
* 根据给定的 ID 获取一条记录
*
* @param {number} id - 要获取的记录的 ID
* @returns {Object} - 具有给定 ID 的记录对象,如果没有找到符合条件的记录,则返回 undefined
*/
getRecordById(id) {
const records = this.getRecords();
return records.find((i) => i.id === id);
}
/**
* 获取记录列表
*
* @returns {Array} - 从 storageApi 获取的记录列表
*/
getRecords() {
return storageApi.getRecords();
}
/**
* 设置记录列表
*
* @param {Array} records - 要设置的记录列表
* @returns {void} - 此函数不返回任何值
*/
setRecords(records) {
storageApi.setRecords(records);
}
/**
* 清除所有记录
*
* @returns {void} - 此函数不返回任何值
*/
clearRecords() {
storageApi.removeRecords();
}
}
export default RecordManager;