MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / callback

Function callback

src/pkg/utils/file-tracker.ts:27–67  ·  view source on GitHub ↗
(records: FileSystemChangeRecord[], observer: FileSystemObserverInstance)

Source from the content-addressed store, hash-verified

25};
26
27const callback = async (records: FileSystemChangeRecord[], observer: FileSystemObserverInstance) => {
28 try {
29 for (const record of records) {
30 const { root, type } = record;
31 if (!(root instanceof FileSystemFileHandle)) continue;
32 // 只要 FileSystemObserver 侦测到档案改变,就试一下找记录和读档
33 const ftInfo = await getHandleRecord(root, observer);
34 // 如没有记录则忽略
35 if (!ftInfo) continue;
36 let file: File | null = null;
37 try {
38 const fRead = await root.getFile();
39 if (fRead && fRead.lastModified > 0 && fRead.size > 0) {
40 // 有档案内容读取权限,排除空档案
41 file = fRead;
42 }
43 } catch (e) {
44 // 档案改名或删掉时,或会被此捕捉(预期报错)
45 console.warn(e);
46 unmountFileTrack(root);
47 ftInfo.onFileError();
48 }
49 // 如读档失败则忽略
50 if (!file) continue;
51 // 如成功读档但显示为失败,则重新 observe
52 if (type === "errored") {
53 observer.observe(root);
54 }
55 // 以 lastModified 判断避免重复更新
56 if (ftInfo.lastModified === file.lastModified) continue;
57 ftInfo.lastModified = file.lastModified;
58 const code = await file.text();
59 if (code && typeof code === "string") {
60 ftInfo.setCode(code, false);
61 }
62 }
63 } catch (e) {
64 // 捕捉非预期错误
65 console.warn(e);
66 }
67};
68
69export const startFileTrack = (fileHandle: FileSystemFileHandle, ftInfo: FTInfo) => {
70 const fileObserver = new FileSystemObserver(callback);

Callers 15

setMethod · 0.85
removeMethod · 0.85
clearMethod · 0.85
getBytesInUseMethod · 0.85
configureWorldMethod · 0.85
getScriptsMethod · 0.85
executeMethod · 0.85
registerMethod · 0.85
unregisterMethod · 0.85
updateMethod · 0.85
updateSessionRulesMethod · 0.85

Calls 5

getHandleRecordFunction · 0.85
unmountFileTrackFunction · 0.85
warnMethod · 0.80
observeMethod · 0.80
textMethod · 0.45

Tested by

no test coverage detected