@lp-labs/async-barrier 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +75 -2
- package/dist/cjs/index.d.ts +21 -0
- package/dist/cjs/index.js +79 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/index.d.ts +21 -0
- package/dist/esm/index.js +74 -0
- package/package.json +61 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Libin Prasanth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lp-labs/async-barrier
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Cyclic fixed-party rendezvous with explicit broken generations.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @lp-labs/async-barrier
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Zero runtime, peer, or sibling dependencies. Node 18+; strict TypeScript declarations and independent ESM/CommonJS builds. Version 0.1.0, MIT.
|
|
12
|
+
|
|
13
|
+
## Quickstart
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { AsyncBarrier } from '@lp-labs/async-barrier';
|
|
17
|
+
const barrier = new AsyncBarrier(2);
|
|
18
|
+
const passes = await Promise.all([barrier.wait(), barrier.wait()]);
|
|
19
|
+
console.log(passes.map((pass) => pass.generation)); // [0, 0]
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## API and contracts
|
|
23
|
+
|
|
24
|
+
Each wait is one arrival; callers must ensure one arrival per logical party per generation. The final arrival resolves all with zero-based arrival index and generation, then automatically advances generation. Abort breaks the entire current generation, not merely its ticket. BarrierBrokenError retains cause (including undefined) and generation; break is idempotent and subsequent wait rejects until reset. reset rejects pending old arrivals and advances even an empty/broken generation. Abort after a successful pass cannot affect the next generation. parties is a positive safe integer or RangeError. No worker identity tracking, timeout scheduler, or task execution. O(parties) memory and release work; no timers.
|
|
25
|
+
|
|
26
|
+
## Full declarations
|
|
27
|
+
|
|
28
|
+
The complete public declarations below are generated from the package's strict source. All runtime exports, interfaces, options, return types and readonly state are listed here.
|
|
29
|
+
|
|
30
|
+
<!-- declarations -->
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
export interface BarrierPass {
|
|
34
|
+
readonly generation: number;
|
|
35
|
+
readonly index: number;
|
|
36
|
+
}
|
|
37
|
+
export declare class BarrierBrokenError extends Error {
|
|
38
|
+
readonly generation: number;
|
|
39
|
+
constructor(generation: number, reason: unknown);
|
|
40
|
+
}
|
|
41
|
+
export declare class AsyncBarrier {
|
|
42
|
+
readonly parties: number;
|
|
43
|
+
private current;
|
|
44
|
+
private arrivals;
|
|
45
|
+
private failure;
|
|
46
|
+
constructor(parties: number);
|
|
47
|
+
get generation(): number;
|
|
48
|
+
get waiting(): number;
|
|
49
|
+
get broken(): boolean;
|
|
50
|
+
break(reason?: unknown): void;
|
|
51
|
+
reset(reason?: unknown): void;
|
|
52
|
+
wait(signal?: AbortSignal): Promise<BarrierPass>;
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
<!-- end declarations -->
|
|
57
|
+
|
|
58
|
+
## Development
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
npm ci --offline
|
|
62
|
+
npm run build
|
|
63
|
+
npm run typecheck
|
|
64
|
+
npm test
|
|
65
|
+
npm run format:check
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
All tests use deterministic clocks, immediate injected sleeps, or explicit promise gates rather than timing guesses. Package exports select ESM for import and CommonJS for require, with separate declaration trees. No runtime configuration files or global hooks.
|
|
69
|
+
|
|
70
|
+
## Support
|
|
71
|
+
|
|
72
|
+
If this library is useful to you, consider supporting its maintenance with a [donation via PayPal](https://www.paypal.me/LibinPrasanth).
|
|
73
|
+
|
|
74
|
+
## Author
|
|
75
|
+
|
|
76
|
+
Created by [Libin Prasanth](https://www.linkedin.com/in/libinprasanth/). Connect on LinkedIn for updates and discussion.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface BarrierPass {
|
|
2
|
+
readonly generation: number;
|
|
3
|
+
readonly index: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class BarrierBrokenError extends Error {
|
|
6
|
+
readonly generation: number;
|
|
7
|
+
constructor(generation: number, reason: unknown);
|
|
8
|
+
}
|
|
9
|
+
export declare class AsyncBarrier {
|
|
10
|
+
readonly parties: number;
|
|
11
|
+
private current;
|
|
12
|
+
private arrivals;
|
|
13
|
+
private failure;
|
|
14
|
+
constructor(parties: number);
|
|
15
|
+
get generation(): number;
|
|
16
|
+
get waiting(): number;
|
|
17
|
+
get broken(): boolean;
|
|
18
|
+
break(reason?: unknown): void;
|
|
19
|
+
reset(reason?: unknown): void;
|
|
20
|
+
wait(signal?: AbortSignal): Promise<BarrierPass>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AsyncBarrier = exports.BarrierBrokenError = void 0;
|
|
4
|
+
class BarrierBrokenError extends Error {
|
|
5
|
+
generation;
|
|
6
|
+
constructor(generation, reason) {
|
|
7
|
+
super('Barrier generation is broken', { cause: reason });
|
|
8
|
+
this.name = 'BarrierBrokenError';
|
|
9
|
+
this.generation = generation;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.BarrierBrokenError = BarrierBrokenError;
|
|
13
|
+
class AsyncBarrier {
|
|
14
|
+
parties;
|
|
15
|
+
current = 0;
|
|
16
|
+
arrivals = [];
|
|
17
|
+
failure;
|
|
18
|
+
constructor(parties) {
|
|
19
|
+
if (!Number.isSafeInteger(parties) || parties < 1)
|
|
20
|
+
throw new RangeError('parties must be a positive safe integer');
|
|
21
|
+
this.parties = parties;
|
|
22
|
+
}
|
|
23
|
+
get generation() {
|
|
24
|
+
return this.current;
|
|
25
|
+
}
|
|
26
|
+
get waiting() {
|
|
27
|
+
return this.arrivals.length;
|
|
28
|
+
}
|
|
29
|
+
get broken() {
|
|
30
|
+
return this.failure !== undefined;
|
|
31
|
+
}
|
|
32
|
+
break(reason) {
|
|
33
|
+
if (this.failure)
|
|
34
|
+
return;
|
|
35
|
+
this.failure = new BarrierBrokenError(this.current, reason);
|
|
36
|
+
const arrivals = this.arrivals;
|
|
37
|
+
this.arrivals = [];
|
|
38
|
+
for (const arrival of arrivals) {
|
|
39
|
+
arrival.cleanup();
|
|
40
|
+
arrival.reject(this.failure);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
reset(reason) {
|
|
44
|
+
this.break(reason);
|
|
45
|
+
this.failure = undefined;
|
|
46
|
+
this.current++;
|
|
47
|
+
}
|
|
48
|
+
wait(signal) {
|
|
49
|
+
if (this.failure)
|
|
50
|
+
return Promise.reject(this.failure);
|
|
51
|
+
if (signal?.aborted) {
|
|
52
|
+
this.break(signal.reason);
|
|
53
|
+
return Promise.reject(this.failure);
|
|
54
|
+
}
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const generation = this.current;
|
|
57
|
+
const abort = () => {
|
|
58
|
+
if (this.current === generation)
|
|
59
|
+
this.break(signal?.reason);
|
|
60
|
+
};
|
|
61
|
+
this.arrivals.push({
|
|
62
|
+
resolve,
|
|
63
|
+
reject,
|
|
64
|
+
cleanup: () => signal?.removeEventListener('abort', abort),
|
|
65
|
+
});
|
|
66
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
67
|
+
if (this.arrivals.length === this.parties) {
|
|
68
|
+
const arrivals = this.arrivals;
|
|
69
|
+
this.arrivals = [];
|
|
70
|
+
this.current++;
|
|
71
|
+
arrivals.forEach((arrival, position) => {
|
|
72
|
+
arrival.cleanup();
|
|
73
|
+
arrival.resolve(Object.freeze({ generation, index: position }));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.AsyncBarrier = AsyncBarrier;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface BarrierPass {
|
|
2
|
+
readonly generation: number;
|
|
3
|
+
readonly index: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class BarrierBrokenError extends Error {
|
|
6
|
+
readonly generation: number;
|
|
7
|
+
constructor(generation: number, reason: unknown);
|
|
8
|
+
}
|
|
9
|
+
export declare class AsyncBarrier {
|
|
10
|
+
readonly parties: number;
|
|
11
|
+
private current;
|
|
12
|
+
private arrivals;
|
|
13
|
+
private failure;
|
|
14
|
+
constructor(parties: number);
|
|
15
|
+
get generation(): number;
|
|
16
|
+
get waiting(): number;
|
|
17
|
+
get broken(): boolean;
|
|
18
|
+
break(reason?: unknown): void;
|
|
19
|
+
reset(reason?: unknown): void;
|
|
20
|
+
wait(signal?: AbortSignal): Promise<BarrierPass>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export class BarrierBrokenError extends Error {
|
|
2
|
+
generation;
|
|
3
|
+
constructor(generation, reason) {
|
|
4
|
+
super('Barrier generation is broken', { cause: reason });
|
|
5
|
+
this.name = 'BarrierBrokenError';
|
|
6
|
+
this.generation = generation;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class AsyncBarrier {
|
|
10
|
+
parties;
|
|
11
|
+
current = 0;
|
|
12
|
+
arrivals = [];
|
|
13
|
+
failure;
|
|
14
|
+
constructor(parties) {
|
|
15
|
+
if (!Number.isSafeInteger(parties) || parties < 1)
|
|
16
|
+
throw new RangeError('parties must be a positive safe integer');
|
|
17
|
+
this.parties = parties;
|
|
18
|
+
}
|
|
19
|
+
get generation() {
|
|
20
|
+
return this.current;
|
|
21
|
+
}
|
|
22
|
+
get waiting() {
|
|
23
|
+
return this.arrivals.length;
|
|
24
|
+
}
|
|
25
|
+
get broken() {
|
|
26
|
+
return this.failure !== undefined;
|
|
27
|
+
}
|
|
28
|
+
break(reason) {
|
|
29
|
+
if (this.failure)
|
|
30
|
+
return;
|
|
31
|
+
this.failure = new BarrierBrokenError(this.current, reason);
|
|
32
|
+
const arrivals = this.arrivals;
|
|
33
|
+
this.arrivals = [];
|
|
34
|
+
for (const arrival of arrivals) {
|
|
35
|
+
arrival.cleanup();
|
|
36
|
+
arrival.reject(this.failure);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
reset(reason) {
|
|
40
|
+
this.break(reason);
|
|
41
|
+
this.failure = undefined;
|
|
42
|
+
this.current++;
|
|
43
|
+
}
|
|
44
|
+
wait(signal) {
|
|
45
|
+
if (this.failure)
|
|
46
|
+
return Promise.reject(this.failure);
|
|
47
|
+
if (signal?.aborted) {
|
|
48
|
+
this.break(signal.reason);
|
|
49
|
+
return Promise.reject(this.failure);
|
|
50
|
+
}
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
const generation = this.current;
|
|
53
|
+
const abort = () => {
|
|
54
|
+
if (this.current === generation)
|
|
55
|
+
this.break(signal?.reason);
|
|
56
|
+
};
|
|
57
|
+
this.arrivals.push({
|
|
58
|
+
resolve,
|
|
59
|
+
reject,
|
|
60
|
+
cleanup: () => signal?.removeEventListener('abort', abort),
|
|
61
|
+
});
|
|
62
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
63
|
+
if (this.arrivals.length === this.parties) {
|
|
64
|
+
const arrivals = this.arrivals;
|
|
65
|
+
this.arrivals = [];
|
|
66
|
+
this.current++;
|
|
67
|
+
arrivals.forEach((arrival, position) => {
|
|
68
|
+
arrival.cleanup();
|
|
69
|
+
arrival.resolve(Object.freeze({ generation, index: position }));
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lp-labs/async-barrier",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Cyclic fixed-party rendezvous with explicit broken generations.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/cjs/index.js",
|
|
8
|
+
"module": "./dist/esm/index.js",
|
|
9
|
+
"types": "./dist/esm/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": {
|
|
13
|
+
"types": "./dist/esm/index.d.ts",
|
|
14
|
+
"default": "./dist/esm/index.js"
|
|
15
|
+
},
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./dist/cjs/index.d.ts",
|
|
18
|
+
"default": "./dist/cjs/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=18"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "node scripts/build.mjs",
|
|
34
|
+
"typecheck": "npm run build && node scripts/check-types.mjs",
|
|
35
|
+
"test": "npm run build && node --test tests/*.test.mjs",
|
|
36
|
+
"format": "prettier --write src scripts tests *.json README.md",
|
|
37
|
+
"format:check": "prettier --check src scripts tests *.json README.md",
|
|
38
|
+
"prepack": "npm run build"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": "^5.9.3",
|
|
42
|
+
"prettier": "^3.6.2"
|
|
43
|
+
},
|
|
44
|
+
"prettier": {
|
|
45
|
+
"singleQuote": true,
|
|
46
|
+
"semi": true,
|
|
47
|
+
"trailingComma": "all",
|
|
48
|
+
"printWidth": 100,
|
|
49
|
+
"endOfLine": "lf"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public",
|
|
53
|
+
"registry": "https://registry.npmjs.org/"
|
|
54
|
+
},
|
|
55
|
+
"author": {
|
|
56
|
+
"name": "Libin Prasanth",
|
|
57
|
+
"url": "https://www.linkedin.com/in/libinprasanth/"
|
|
58
|
+
},
|
|
59
|
+
"funding": {
|
|
60
|
+
"type": "paypal",
|
|
61
|
+
"url": "https://www.paypal.me/LibinPrasanth"
|
|
62
|
+
}
|
|
63
|
+
}
|