tt-help-cli-ycl 1.3.19 → 1.3.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/watch/data-store.js +30 -55
package/package.json
CHANGED
package/src/watch/data-store.js
CHANGED
|
@@ -138,69 +138,44 @@ export function createStore(filePath) {
|
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
// 异步 save:不阻塞事件循环,排队保证顺序
|
|
142
|
-
// 多次 save 调用之间如果已有 save 在进行,只标记需要再次保存
|
|
143
|
-
let saveInFlight = false;
|
|
144
|
-
let pendingSave = false;
|
|
145
|
-
|
|
146
141
|
function save() {
|
|
147
142
|
if (!filePath) return;
|
|
148
|
-
if (saveInFlight) {
|
|
149
|
-
pendingSave = true;
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
doSave();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async function doSave() {
|
|
156
|
-
saveInFlight = true;
|
|
157
143
|
const resolved = path.resolve(filePath);
|
|
158
144
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
active.push(u);
|
|
169
|
-
}
|
|
145
|
+
// 将 done 用户归档到独立文件
|
|
146
|
+
if (doneArchivePath && data.length > 1000) {
|
|
147
|
+
const active = [];
|
|
148
|
+
const toArchive = [];
|
|
149
|
+
for (const u of data) {
|
|
150
|
+
if (u.status === 'done') {
|
|
151
|
+
toArchive.push(u);
|
|
152
|
+
} else {
|
|
153
|
+
active.push(u);
|
|
170
154
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
155
|
+
}
|
|
156
|
+
if (toArchive.length > 0) {
|
|
157
|
+
doneArchive = doneArchive.concat(toArchive);
|
|
158
|
+
data = active;
|
|
159
|
+
rebuildIndex();
|
|
160
|
+
// 重建 done 归档索引
|
|
161
|
+
doneUidIndex = new Map();
|
|
162
|
+
for (let i = 0; i < doneArchive.length; i++) {
|
|
163
|
+
doneUidIndex.set(doneArchive[i].uniqueId, i);
|
|
180
164
|
}
|
|
181
165
|
}
|
|
166
|
+
}
|
|
182
167
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
} catch (e) {
|
|
196
|
-
console.error(`[DataStore] save error: ${e.message}`);
|
|
197
|
-
} finally {
|
|
198
|
-
saveInFlight = false;
|
|
199
|
-
// 如果有排队的 save,继续执行
|
|
200
|
-
if (pendingSave) {
|
|
201
|
-
pendingSave = false;
|
|
202
|
-
doSave();
|
|
203
|
-
}
|
|
168
|
+
const json = JSON.stringify(data);
|
|
169
|
+
const tmpPath = resolved + '.tmp';
|
|
170
|
+
fs.writeFileSync(tmpPath, json, 'utf-8');
|
|
171
|
+
fs.renameSync(tmpPath, resolved);
|
|
172
|
+
|
|
173
|
+
// 写 done 归档
|
|
174
|
+
if (doneArchivePath && doneArchive.length > 0) {
|
|
175
|
+
const doneJson = JSON.stringify(doneArchive);
|
|
176
|
+
const doneTmp = doneArchivePath + '.tmp';
|
|
177
|
+
fs.writeFileSync(doneTmp, doneJson, 'utf-8');
|
|
178
|
+
fs.renameSync(doneTmp, doneArchivePath);
|
|
204
179
|
}
|
|
205
180
|
}
|
|
206
181
|
|