vvvfs 0.0.7 → 0.0.8

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/README.md CHANGED
@@ -42,6 +42,9 @@ const vvvfs = new VVVFS("vvvfs", {
42
42
  (async function () {
43
43
  await vvvfs.reset(); // 重置文件系统
44
44
  await vvvfs.init("UserName"); // 初始化文件系统
45
+ await vvvfs.watch("/home/user/Desktop/test.txt", (type) => {
46
+ console.log(type); // 打印文件操作的类型
47
+ });
45
48
  await vvvfs.createDir("/home/user/Desktop"); // 创建目录,返回true和false
46
49
  await vvvfs.writeText("/home/user/Desktop/test.txt", "Hello World!"); // 写入文本文件,写入文件还包括write(path: string, content: Blob)和writeJson(path: string, content: Record<string, any>)方法,返回true和false
47
50
  console.log(await vvvfs.readText("/home/user/Desktop/test.txt")); // 读取文本文件,读取文件还包括read(path: string): Blob | null和readJson(path: string): Record<string, any> | null方法
@@ -78,6 +81,10 @@ const vvvfs = new VVVFS("vvvfs", {
78
81
 
79
82
  ## 更新日志
80
83
 
84
+ ### 0.0.8
85
+
86
+ - 新增 `watch` 方法
87
+
81
88
  ### 0.0.7
82
89
 
83
90
  - AI 了优化一下
package/index.html CHANGED
@@ -16,6 +16,9 @@
16
16
  await vvvfs.reset();
17
17
  await vvvfs.init("IFTC");
18
18
  console.log(vvvfs);
19
+ await vvvfs.watch("/test.txt", (type) => {
20
+ console.log(type);
21
+ });
19
22
  console.log("创建文件", await vvvfs.createFile("/test.txt"));
20
23
  console.log("写入文件", await vvvfs.writeText("/test.txt", "Hello World"));
21
24
  console.log("文件是否存在", await vvvfs.exists("/test.txt"));
package/index.ts CHANGED
@@ -216,6 +216,9 @@ class VVVFSFile {
216
216
  async search(query: string) {
217
217
  return await this._vvvfs.search(this._path, query);
218
218
  }
219
+ async watch(handler: (type: string) => void) {
220
+ return await this._vvvfs.watch(this._path, handler);
221
+ }
219
222
  }
220
223
 
221
224
  const version = packageJson.version;
@@ -803,13 +806,17 @@ class VVVFS {
803
806
  for (const file of files) {
804
807
  await this.delete(joinPath(targetPath, file));
805
808
  }
806
- const dirRecord = await this.db.files.where({ name, path: parent, type: "dir" }).first();
809
+ const dirRecord = await this.db.files
810
+ .where({ name, path: parent, type: "dir" })
811
+ .first();
807
812
  if (dirRecord) {
808
813
  await this.db.files.delete(dirRecord.id!);
809
814
  }
810
815
  return true;
811
816
  } else {
812
- const fileRecord = await this.db.files.where({ name, path: parent, type: "file" }).first();
817
+ const fileRecord = await this.db.files
818
+ .where({ name, path: parent, type: "file" })
819
+ .first();
813
820
  if (fileRecord) {
814
821
  await this.db.files.delete(fileRecord.id!);
815
822
  }
@@ -861,14 +868,18 @@ class VVVFS {
861
868
  for (const child of children) {
862
869
  await this.move(joinPath(sourcePath, child), joinPath(destinationPath, child));
863
870
  }
864
- const dirRecord = await this.db.files.where({ name, path: parent, type: "dir" }).first();
871
+ const dirRecord = await this.db.files
872
+ .where({ name, path: parent, type: "dir" })
873
+ .first();
865
874
  if (dirRecord) {
866
875
  await this.db.files.delete(dirRecord.id!);
867
876
  }
868
877
  return true;
869
878
  } else {
870
879
  await this.createDir(newParent);
871
- const fileRecord = await this.db.files.where({ name, path: parent, type: "file" }).first();
880
+ const fileRecord = await this.db.files
881
+ .where({ name, path: parent, type: "file" })
882
+ .first();
872
883
  if (fileRecord) {
873
884
  await this.db.files.update(fileRecord.id!, { name: newName, path: newParent });
874
885
  return true;
@@ -923,7 +934,9 @@ class VVVFS {
923
934
  }
924
935
  return true;
925
936
  } else {
926
- const fileRecord = await this.db.files.where({ name, path: parent, type: "file" }).first();
937
+ const fileRecord = await this.db.files
938
+ .where({ name, path: parent, type: "file" })
939
+ .first();
927
940
  if (fileRecord) {
928
941
  await this.db.files.add({
929
942
  name: newName,
@@ -993,6 +1006,63 @@ class VVVFS {
993
1006
  return null;
994
1007
  }
995
1008
  }
1009
+ async watch(path: string, handler: (type: string) => void) {
1010
+ const targetPath = joinPath(path);
1011
+
1012
+ const getState = async () => {
1013
+ const exists = await this.exists(targetPath);
1014
+ if (!exists) {
1015
+ return { exists: false, signature: "" };
1016
+ }
1017
+ if (await this.isDir(targetPath)) {
1018
+ const records = await this.db.files
1019
+ .filter(
1020
+ (file) =>
1021
+ file.path === targetPath || file.path.startsWith(targetPath + "/"),
1022
+ )
1023
+ .toArray();
1024
+ const signature = records
1025
+ .map((file) => {
1026
+ const fullPath = joinPath(file.path, file.name);
1027
+ const fileInfo = file.file
1028
+ ? `${file.file.type}:${file.file.size}:${file.file.lastModified}`
1029
+ : "null";
1030
+ return `${fullPath}:${file.type}:${fileInfo}`;
1031
+ })
1032
+ .sort()
1033
+ .join("|");
1034
+ return { exists: true, signature };
1035
+ }
1036
+ const { name, parent } = parsePath(targetPath);
1037
+ const record = await this.db.files.where({ name, path: parent, type: "file" }).first();
1038
+ const signature = record?.file
1039
+ ? `${record.file.type}:${record.file.size}:${record.file.lastModified}`
1040
+ : "";
1041
+ return { exists: true, signature };
1042
+ };
1043
+
1044
+ let previousState = await getState();
1045
+ setInterval(async () => {
1046
+ try {
1047
+ const currentState = await getState();
1048
+ if (
1049
+ previousState.exists !== currentState.exists ||
1050
+ previousState.signature !== currentState.signature
1051
+ ) {
1052
+ if (!previousState.exists && currentState.exists) {
1053
+ handler("create");
1054
+ } else if (previousState.exists && !currentState.exists) {
1055
+ handler("unlink");
1056
+ } else {
1057
+ handler("change");
1058
+ }
1059
+ previousState = currentState;
1060
+ }
1061
+ } catch (error) {
1062
+ console.error("监听失败", error);
1063
+ }
1064
+ });
1065
+ }
996
1066
  }
997
1067
  function parsePath(path: string) {
998
1068
  path = joinPath(path);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vvvfs",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "一个使用IndexDB实现的虚拟文件系统",
5
5
  "keywords": [
6
6
  "Virtual",