@opendaw/lib-fusion 0.0.27 → 0.0.29

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.
@@ -3,13 +3,15 @@ import { OpfsProtocol } from "./OpfsProtocol";
3
3
  import "../types";
4
4
  export declare namespace OpfsWorker {
5
5
  const init: (messenger: Messenger) => Communicator.Executor<{
6
+ readonly "__#private@#locks": Map<string, Promise<void>>;
6
7
  write(path: string, data: Uint8Array): Promise<void>;
7
8
  read(path: string): Promise<Uint8Array>;
8
9
  delete(path: string): Promise<void>;
9
10
  list(path: string): Promise<ReadonlyArray<OpfsProtocol.Entry>>;
10
11
  clear(): Promise<void>;
11
- #resolveFile(path: string, options?: FileSystemGetDirectoryOptions): Promise<FileSystemSyncAccessHandle>;
12
- #resolveFolder(segments: ReadonlyArray<string>, options?: FileSystemGetDirectoryOptions): Promise<FileSystemDirectoryHandle>;
12
+ "__#private@#acquireLock"<T>(path: string, operation: () => Promise<T>): Promise<T>;
13
+ "__#private@#resolveFile"(path: string, options?: FileSystemGetDirectoryOptions): Promise<FileSystemSyncAccessHandle>;
14
+ "__#private@#resolveFolder"(segments: ReadonlyArray<string>, options?: FileSystemGetDirectoryOptions): Promise<FileSystemDirectoryHandle>;
13
15
  }>;
14
16
  }
15
17
  //# sourceMappingURL=OpfsWorker.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"OpfsWorker.d.ts","sourceRoot":"","sources":["../../src/opfs/OpfsWorker.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,YAAY,EAAE,SAAS,EAAW,MAAM,sBAAsB,CAAA;AACtE,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAA;AAC3C,OAAO,UAAU,CAAA;AAEjB,yBAAiB,UAAU,CAAC;IAKjB,MAAM,IAAI,GAAI,WAAW,SAAS;oBAEf,MAAM,QAAQ,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;mBAWzC,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;qBAY1B,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;mBAOxB,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;iBAWrD,OAAO,CAAC,IAAI,CAAC;2BAWH,MAAM,YAAY,6BAA6B,GAAG,OAAO,CAAC,0BAA0B,CAAC;iCAO/E,aAAa,CAAC,MAAM,CAAC,YACrB,6BAA6B,GAAG,OAAO,CAAC,yBAAyB,CAAC;MAKnG,CAAA;CAMT"}
1
+ {"version":3,"file":"OpfsWorker.d.ts","sourceRoot":"","sources":["../../src/opfs/OpfsWorker.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,YAAY,EAAE,SAAS,EAAW,MAAM,sBAAsB,CAAA;AACtE,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAA;AAC3C,OAAO,UAAU,CAAA;AAEjB,yBAAiB,UAAU,CAAC;IAGjB,MAAM,IAAI,GAAI,WAAW,SAAS;;oBAIf,MAAM,QAAQ,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;mBAczC,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;qBAe1B,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;mBASxB,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;iBAWrD,OAAO,CAAC,IAAI,CAAC;kCAWT,CAAC,QAAQ,MAAM,aAAa,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;wCAkBnD,MAAM,YAAY,6BAA6B,GAAG,OAAO,CAAC,0BAA0B,CAAC;8CAO/E,aAAa,CAAC,MAAM,CAAC,YACrB,6BAA6B,GAAG,OAAO,CAAC,yBAAyB,CAAC;MAKnG,CAAA;CAMT"}
@@ -1,44 +1,53 @@
1
- import { Arrays, asDefined } from "@opendaw/lib-std";
1
+ import { Arrays, asDefined, panic } from "@opendaw/lib-std";
2
2
  import { Communicator, Promises } from "@opendaw/lib-runtime";
3
3
  import "../types";
4
4
  export var OpfsWorker;
5
5
  (function (OpfsWorker) {
6
6
  const DEBUG = true;
7
- const readLimiter = new Promises.Limit(1);
8
- const writeLimiter = new Promises.Limit(1);
9
7
  OpfsWorker.init = (messenger) => Communicator.executor(messenger.channel("opfs"), new class {
8
+ #locks = new Map();
10
9
  async write(path, data) {
11
- if (DEBUG) {
12
- console.debug(`write ${data.length}b to ${path}`);
13
- }
14
- return writeLimiter.add(() => this.#resolveFile(path, { create: true })
15
- .then(handle => {
16
- handle.truncate(data.length);
17
- handle.write(data.buffer, { at: 0 });
18
- handle.flush();
19
- handle.close();
20
- }));
10
+ await this.#acquireLock(path, async () => {
11
+ if (DEBUG) {
12
+ console.debug(`write ${data.length}b to ${path}`);
13
+ }
14
+ const handle = await this.#resolveFile(path, { create: true });
15
+ try {
16
+ handle.truncate(data.length);
17
+ handle.write(data.buffer, { at: 0 });
18
+ handle.flush();
19
+ }
20
+ finally {
21
+ handle.close();
22
+ }
23
+ });
21
24
  }
22
25
  async read(path) {
23
- if (DEBUG) {
24
- console.debug(`read ${path}`);
25
- }
26
- return readLimiter.add(() => this.#resolveFile(path)
27
- .then(handle => {
28
- const size = handle.getSize();
29
- const buffer = new Uint8Array(size);
30
- handle.read(buffer);
31
- handle.close();
32
- return buffer;
33
- }));
26
+ return await this.#acquireLock(path, async () => {
27
+ if (DEBUG) {
28
+ console.debug(`read ${path}`);
29
+ }
30
+ const handle = await this.#resolveFile(path);
31
+ try {
32
+ const size = handle.getSize();
33
+ const buffer = new Uint8Array(size);
34
+ handle.read(buffer);
35
+ return buffer;
36
+ }
37
+ finally {
38
+ handle.close();
39
+ }
40
+ });
34
41
  }
35
42
  async delete(path) {
36
- const segments = pathToSegments(path);
37
- if (segments.length === 0) {
38
- return this.clear();
39
- }
40
- return this.#resolveFolder(segments.slice(0, -1))
41
- .then(folder => folder.removeEntry(asDefined(segments.at(-1)), { recursive: true }));
43
+ await this.#acquireLock(path, async () => {
44
+ const segments = pathToSegments(path);
45
+ if (segments.length === 0) {
46
+ return this.clear();
47
+ }
48
+ return this.#resolveFolder(segments.slice(0, -1))
49
+ .then(folder => folder.removeEntry(asDefined(segments.at(-1)), { recursive: true }));
50
+ });
42
51
  }
43
52
  async list(path) {
44
53
  const segments = pathToSegments(path);
@@ -63,11 +72,29 @@ export var OpfsWorker;
63
72
  }
64
73
  }
65
74
  }
75
+ async #acquireLock(path, operation) {
76
+ const existingLock = this.#locks.get(path);
77
+ if (existingLock) {
78
+ await existingLock;
79
+ }
80
+ let releaseLock = () => panic("Lock not acquired");
81
+ const lockPromise = new Promise(resolve => releaseLock = resolve);
82
+ this.#locks.set(path, lockPromise);
83
+ try {
84
+ return await operation();
85
+ }
86
+ finally {
87
+ releaseLock();
88
+ if (this.#locks.get(path) === lockPromise) {
89
+ this.#locks.delete(path);
90
+ }
91
+ }
92
+ }
66
93
  async #resolveFile(path, options) {
67
94
  const segments = pathToSegments(path);
68
- return this.#resolveFolder(segments.slice(0, -1), options)
69
- .then((folder) => folder.getFileHandle(asDefined(segments.at(-1)), options)
70
- .then(handle => handle.createSyncAccessHandle()));
95
+ const folder = await this.#resolveFolder(segments.slice(0, -1), options);
96
+ const fileHandle = await folder.getFileHandle(asDefined(segments.at(-1)), options);
97
+ return await fileHandle.createSyncAccessHandle();
71
98
  }
72
99
  async #resolveFolder(segments, options) {
73
100
  let folder = await navigator.storage.getDirectory();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opendaw/lib-fusion",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "license": "LGPL-3.0-or-later",
@@ -22,13 +22,13 @@
22
22
  "test": "echo \"No tests to run\""
23
23
  },
24
24
  "dependencies": {
25
- "@opendaw/lib-box": "^0.0.27",
26
- "@opendaw/lib-runtime": "^0.0.27",
27
- "@opendaw/lib-std": "^0.0.27"
25
+ "@opendaw/lib-box": "^0.0.29",
26
+ "@opendaw/lib-runtime": "^0.0.29",
27
+ "@opendaw/lib-std": "^0.0.29"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@opendaw/eslint-config": "^0.0.19",
31
31
  "@opendaw/typescript-config": "^0.0.20"
32
32
  },
33
- "gitHead": "e94fe3bfdfa1c24ba8d16a695ebf7345286f3a32"
33
+ "gitHead": "9cad98b0a3858f3093aa42528b0b01c77f22cbff"
34
34
  }