@rocicorp/lock 1.0.4 → 2.0.0
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/out/lock.d.ts +2 -4
- package/out/lock.js +17 -18
- package/package.json +28 -34
package/out/lock.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
export declare class Lock {
|
|
2
|
-
private
|
|
2
|
+
#private;
|
|
3
3
|
lock(): Promise<() => void>;
|
|
4
4
|
withLock<R>(f: () => R | Promise<R>): Promise<R>;
|
|
5
5
|
}
|
|
6
6
|
export declare class RWLock {
|
|
7
|
-
private
|
|
8
|
-
private _writeP;
|
|
9
|
-
private _readP;
|
|
7
|
+
#private;
|
|
10
8
|
read(): Promise<() => void>;
|
|
11
9
|
withRead<R>(f: () => R | Promise<R>): Promise<R>;
|
|
12
10
|
write(): Promise<() => void>;
|
package/out/lock.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { resolver } from '@rocicorp/resolver';
|
|
2
1
|
export class Lock {
|
|
3
|
-
|
|
2
|
+
#lockP = null;
|
|
4
3
|
async lock() {
|
|
5
|
-
const previous = this
|
|
6
|
-
const { promise, resolve } =
|
|
7
|
-
this
|
|
4
|
+
const previous = this.#lockP;
|
|
5
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
6
|
+
this.#lockP = promise;
|
|
8
7
|
await previous;
|
|
9
8
|
return resolve;
|
|
10
9
|
}
|
|
@@ -13,14 +12,14 @@ export class Lock {
|
|
|
13
12
|
}
|
|
14
13
|
}
|
|
15
14
|
export class RWLock {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
#lock = new Lock();
|
|
16
|
+
#writeP = null;
|
|
17
|
+
#readP = [];
|
|
19
18
|
read() {
|
|
20
|
-
return this.
|
|
21
|
-
await this
|
|
22
|
-
const { promise, resolve } =
|
|
23
|
-
this.
|
|
19
|
+
return this.#lock.withLock(async () => {
|
|
20
|
+
await this.#writeP;
|
|
21
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
22
|
+
this.#readP.push(promise);
|
|
24
23
|
return resolve;
|
|
25
24
|
});
|
|
26
25
|
}
|
|
@@ -28,12 +27,12 @@ export class RWLock {
|
|
|
28
27
|
return run(this.read(), f);
|
|
29
28
|
}
|
|
30
29
|
async write() {
|
|
31
|
-
return await this.
|
|
32
|
-
await this
|
|
33
|
-
await Promise.all(this
|
|
34
|
-
const { promise, resolve } =
|
|
35
|
-
this
|
|
36
|
-
this
|
|
30
|
+
return await this.#lock.withLock(async () => {
|
|
31
|
+
await this.#writeP;
|
|
32
|
+
await Promise.all(this.#readP);
|
|
33
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
34
|
+
this.#writeP = promise;
|
|
35
|
+
this.#readP = [];
|
|
37
36
|
return resolve;
|
|
38
37
|
});
|
|
39
38
|
}
|
package/package.json
CHANGED
|
@@ -1,46 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rocicorp/lock",
|
|
3
|
+
"version": "2.0.0",
|
|
3
4
|
"description": "Implements Lock and RWLock synchronization primitives.",
|
|
4
|
-
"version": "1.0.4",
|
|
5
|
-
"repository": "github:rocicorp/lock",
|
|
6
5
|
"license": "Apache-2.0",
|
|
7
|
-
"
|
|
8
|
-
|
|
6
|
+
"repository": "github:rocicorp/lock",
|
|
7
|
+
"files": [
|
|
8
|
+
"out/*",
|
|
9
|
+
"!out/*.test.*"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./out/lock.js",
|
|
13
|
+
"module": "./out/lock.js",
|
|
14
|
+
"types": "out/lock.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./out/lock.js"
|
|
9
17
|
},
|
|
10
18
|
"scripts": {
|
|
11
|
-
"test": "
|
|
12
|
-
"
|
|
13
|
-
"format": "
|
|
14
|
-
"check-format": "
|
|
15
|
-
"
|
|
19
|
+
"test": "vitest run src/",
|
|
20
|
+
"fmt": "oxfmt",
|
|
21
|
+
"format": "oxfmt",
|
|
22
|
+
"check-format": "oxfmt --check",
|
|
23
|
+
"check-types": "tsc",
|
|
24
|
+
"check-types:watch": "tsc --watch",
|
|
25
|
+
"lint": "oxlint src/",
|
|
16
26
|
"build": "rm -rf out && tsc",
|
|
17
|
-
"prepack": "
|
|
18
|
-
},
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"@rocicorp/resolver": "^1.0.2"
|
|
27
|
+
"prepack": "pnpm run lint && pnpm run test"
|
|
21
28
|
},
|
|
22
29
|
"devDependencies": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"chai": "^4.3.8",
|
|
29
|
-
"eslint": "^8.49.0",
|
|
30
|
-
"mocha": "^10.2.0",
|
|
31
|
-
"prettier": "^3.0.3",
|
|
32
|
-
"sinon": "^15.2.0",
|
|
33
|
-
"typescript": "^5.2.2"
|
|
30
|
+
"oxfmt": "^0.51.0",
|
|
31
|
+
"oxlint": "^1.66.0",
|
|
32
|
+
"oxlint-tsgolint": "^0.23.0",
|
|
33
|
+
"typescript": "^6.0.3",
|
|
34
|
+
"vitest": "^4.1.6"
|
|
34
35
|
},
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
"module": "./out/lock.js",
|
|
38
|
-
"main": "./out/lock.js",
|
|
39
|
-
"exports": {
|
|
40
|
-
".": "./out/lock.js"
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=22.0.0"
|
|
41
38
|
},
|
|
42
|
-
"
|
|
43
|
-
"out/*",
|
|
44
|
-
"!out/*.test.*"
|
|
45
|
-
]
|
|
39
|
+
"packageManager": "pnpm@11.2.2+sha512.36e6621fad506178936455e70247b8808ef4ec25797a9f437a93281a020484e2607f6a469a22e982987c3dbb8866e3071514ab10a4a1749e06edcd1ec118436f"
|
|
46
40
|
}
|