-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsyncSettingManager.js
More file actions
289 lines (260 loc) · 8.76 KB
/
syncSettingManager.js
File metadata and controls
289 lines (260 loc) · 8.76 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// 同步状态管理器
class SyncStatusManager {
static SYNC_STATUS_KEY = 'sync_status';
static SYNC_PROCESS_KEY = 'sync_process';
// 默认状态
static DEFAULT_STATUS = {
cloud: {
lastSync: null,
lastSyncResult: null
},
webdav: {
lastSync: null,
lastSyncResult: null,
metadata: null,
}
};
// 获取所有同步状态
static async getStatus() {
try {
const storedStatus = await LocalStorageMgr.get(this.SYNC_STATUS_KEY) || {};
// 合并默认状态
const statusCache = { ...this.DEFAULT_STATUS, ...storedStatus };
return statusCache;
} catch (error) {
logger.error('获取同步状态失败:', error);
return this.DEFAULT_STATUS;
}
}
// 获取特定服务的同步状态
static async getServiceStatus(service) {
try {
const status = await this.getStatus();
return status[service];
} catch (error) {
logger.error(`获取同步服务[${service}]状态失败:`, error);
return this.DEFAULT_STATUS[service];
}
}
// 更新同步状态
static async updateStatus(service, status) {
try {
const currentStatus = await this.getStatus();
const newStatus = {
...currentStatus,
[service]: status
};
logger.debug('更新同步状态:', {
currentStatus,
newStatus
});
await LocalStorageMgr.set(this.SYNC_STATUS_KEY, newStatus);
return newStatus;
} catch (error) {
logger.error(`更新同步状态失败[${service}]:`, error);
throw error;
}
}
static async hasSyncError() {
const status = await this.getStatus();
if (status.cloud.lastSyncResult && status.cloud.lastSyncResult !== 'success') {
return true;
}
if (status.webdav.lastSyncResult && status.webdav.lastSyncResult !== 'success') {
return true;
}
return false;
}
static async updateSyncProcess(process) {
try {
await LocalStorageMgr.set(this.SYNC_PROCESS_KEY, process);
} catch (error) {
logger.error(`更新同步过程失败:`, error);
}
}
static async getSyncProcess() {
try {
const result = await LocalStorageMgr.get(this.SYNC_PROCESS_KEY);
return result || {};
} catch (error) {
logger.error(`获取同步过程失败:`, error);
return {};
}
}
static async isSyncing() {
const process = await this.getSyncProcess();
const now = Date.now();
const startTime = process.startTime || 0;
const timeSinceLastSync = (now - startTime) / 1000; // 转换为秒
if (timeSinceLastSync < 300) { // 5分钟 = 300秒
return true;
}
return false;
}
}
// 同步服务管理器
class SyncSettingsManager {
static syncConfigCache = null;
static SYNC_CONFIG_KEY = 'sync_config';
// 默认配置
static DEFAULT_CONFIG = {
cloud: {
autoSync: true,
},
webdav: {
server: {
url: '',
username: '',
password: '',
folder: '/bookmarks' // 默认文件夹路径
},
syncData: {
bookmarks: true, // 同步书签
settings: true, // 同步设置
filters: true, // 同步自定义标签
services: true // 同步API服务配置
},
syncStrategy: {
autoSync: false, // 自动同步
interval: 15, // 同步间隔(分钟)
mechanism: 'merge', // 同步机制:merge(合并), override(覆盖)
}
}
};
static async init() {
await this.getConfig();
this.setupStorageListener();
}
static setupStorageListener() {
chrome.storage.onChanged.addListener(async (changes, areaName) => {
if (areaName === 'local' && changes[this.SYNC_CONFIG_KEY]) {
logger.debug('同步配置发生变化, 清除缓存');
this.syncConfigCache = null;
}
});
}
// 获取所有同步配置
static async getConfig() {
try {
if (this.syncConfigCache) {
return this.syncConfigCache;
}
const storedConfig = await LocalStorageMgr.get(this.SYNC_CONFIG_KEY) || {};
// 使用深度合并确保所有层级的默认值都被正确应用
const configCache = this.deepMerge(this.DEFAULT_CONFIG, storedConfig);
this.syncConfigCache = configCache;
return configCache;
} catch (error) {
logger.error('获取同步配置失败:', error);
return this.DEFAULT_CONFIG;
}
}
// 获取特定同步服务的配置
static async getServiceConfig(service) {
try {
const config = await this.getConfig();
return config[service];
} catch (error) {
logger.error(`获取同步服务[${service}]配置失败:`, error);
return this.DEFAULT_CONFIG[service];
}
}
// 更新配置
static async updateConfig(updates) {
try {
const currentConfig = await this.getConfig();
const newConfig = this.deepMerge(currentConfig, updates);
logger.debug('更新同步配置:', {
currentConfig,
updates,
newConfig
});
await LocalStorageMgr.set(this.SYNC_CONFIG_KEY, newConfig);
this.syncConfigCache = null;
return newConfig;
} catch (error) {
logger.error('更新同步配置失败:', error);
throw error;
}
}
// 更新特定同步服务的配置
static async updateServiceConfig(service, config) {
try {
const update = {
[service]: config
};
return await this.updateConfig(update);
} catch (error) {
logger.error(`更新同步服务[${service}]配置失败:`, error);
throw error;
}
}
static async isAutoSyncEnabled(service) {
const config = await this.getServiceConfig(service);
switch (service) {
case 'cloud':
return config.autoSync;
case 'webdav':
if (!config.syncStrategy.autoSync) {
return false;
}
if (!config.server.url || !config.server.username || !config.server.password) {
return false;
}
return true;
default:
return false;
}
}
// 重置所有配置
static async resetConfig() {
try {
await LocalStorageMgr.set(this.SYNC_CONFIG_KEY, this.DEFAULT_CONFIG);
this.syncConfigCache = null;
return this.DEFAULT_CONFIG;
} catch (error) {
logger.error('重置同步配置失败:', error);
throw error;
}
}
// 深度合并对象
static deepMerge(target, source) {
const result = { ...target };
for (const key in source) {
if (Array.isArray(source[key])) {
result[key] = [...source[key]];
}
else if (source[key] instanceof Object && key in target) {
result[key] = this.deepMerge(target[key], source[key]);
}
else {
result[key] = source[key];
}
}
return result;
}
/**
* 验证WebDAV配置是否有效
* @param {Object} config - WebDAV配置
* @returns {boolean} 配置是否有效
*/
static validateWebDAVConfig(config) {
// 验证必要的服务器信息
if (!config.server.url || !config.server.username || !config.server.password) {
return false;
}
// 验证同步间隔
if (config.syncStrategy.autoSync) {
const interval = config.syncStrategy.interval;
if (isNaN(interval) || interval < 5 || interval > 1440) {
return false;
}
}
// 验证至少选择了一项要同步的数据
const hasSelectedData = Object.values(config.syncData).some(value => value);
if (!hasSelectedData) {
return false;
}
return true;
}
}