@barnum/barnum 0.0.0-main-bf5fee51 → 0.0.0-main-37d1e610
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.
|
@@ -2,22 +2,21 @@ import { type Pipeable, type TypedAction } from "../ast.js";
|
|
|
2
2
|
/**
|
|
3
3
|
* RAII-style resource management combinator.
|
|
4
4
|
*
|
|
5
|
-
* Runs `create` to acquire a resource, then
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* Runs `create` to acquire a resource, then passes `[TResource, TIn]`
|
|
6
|
+
* as a tuple to the action. After the action completes, `dispose`
|
|
7
|
+
* receives the resource for cleanup. The overall combinator returns
|
|
8
|
+
* the action's output.
|
|
9
9
|
*
|
|
10
10
|
* ```
|
|
11
11
|
* TIn → create → TResource
|
|
12
|
-
* →
|
|
13
|
-
* → action(TResource & TIn) → TOut
|
|
12
|
+
* → action([TResource, TIn]) → TOut
|
|
14
13
|
* → dispose(TResource) → (discarded)
|
|
15
14
|
* → TOut
|
|
16
15
|
* ```
|
|
17
16
|
*/
|
|
18
|
-
export declare function withResource<TIn
|
|
17
|
+
export declare function withResource<TIn, TResource, TOut, TDisposeOut = unknown>({ create, action, dispose, }: {
|
|
19
18
|
create: Pipeable<TIn, TResource>;
|
|
20
|
-
action: Pipeable<TResource
|
|
19
|
+
action: Pipeable<[TResource, TIn], TOut>;
|
|
21
20
|
dispose: Pipeable<TResource, TDisposeOut>;
|
|
22
21
|
}): TypedAction<TIn, TOut>;
|
|
23
22
|
//# sourceMappingURL=with-resource.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-resource.d.ts","sourceRoot":"","sources":["../../src/builtins/with-resource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAY,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"with-resource.d.ts","sourceRoot":"","sources":["../../src/builtins/with-resource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAY,MAAM,WAAW,CAAC;AAUtE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,WAAW,GAAG,OAAO,EAAE,EACxE,MAAM,EACN,MAAM,EACN,OAAO,GACR,EAAE;IACD,MAAM,EAAE,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;IACjC,MAAM,EAAE,QAAQ,CAAC,CAAC,SAAS,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;IACzC,OAAO,EAAE,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;CAC3C,GAAG,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,CA6BzB"}
|
|
@@ -2,7 +2,6 @@ import { toAction } from "../ast.js";
|
|
|
2
2
|
import { chain } from "../chain.js";
|
|
3
3
|
import { all } from "../all.js";
|
|
4
4
|
import { identity } from "./scalar.js";
|
|
5
|
-
import { merge } from "./struct.js";
|
|
6
5
|
import { getIndex } from "./array.js";
|
|
7
6
|
// ---------------------------------------------------------------------------
|
|
8
7
|
// WithResource — RAII-style create/action/dispose
|
|
@@ -10,26 +9,26 @@ import { getIndex } from "./array.js";
|
|
|
10
9
|
/**
|
|
11
10
|
* RAII-style resource management combinator.
|
|
12
11
|
*
|
|
13
|
-
* Runs `create` to acquire a resource, then
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
12
|
+
* Runs `create` to acquire a resource, then passes `[TResource, TIn]`
|
|
13
|
+
* as a tuple to the action. After the action completes, `dispose`
|
|
14
|
+
* receives the resource for cleanup. The overall combinator returns
|
|
15
|
+
* the action's output.
|
|
17
16
|
*
|
|
18
17
|
* ```
|
|
19
18
|
* TIn → create → TResource
|
|
20
|
-
* →
|
|
21
|
-
* → action(TResource & TIn) → TOut
|
|
19
|
+
* → action([TResource, TIn]) → TOut
|
|
22
20
|
* → dispose(TResource) → (discarded)
|
|
23
21
|
* → TOut
|
|
24
22
|
* ```
|
|
25
23
|
*/
|
|
26
24
|
export function withResource({ create, action, dispose, }) {
|
|
27
|
-
// Step 1: all(create, identity) → [TResource, TIn]
|
|
28
|
-
const
|
|
29
|
-
// Step 2: all(action,
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
// Step 1: all(create, identity) → [TResource, TIn]
|
|
26
|
+
const acquireParallel = all(create, identity());
|
|
27
|
+
// Step 2: all(action, getIndex(0)) → [TOut, TResource]
|
|
28
|
+
// Run action on the tuple, keep raw TResource for dispose
|
|
29
|
+
const runActionKeepResource = all(toAction(action), toAction(getIndex(0).unwrap()));
|
|
30
|
+
// Step 3: all(getIndex(0) → TOut, getIndex(1) → dispose) → [TOut, TDisposeOut]
|
|
32
31
|
const disposeAndKeepResult = all(toAction(getIndex(0).unwrap()), chain(toAction(getIndex(1).unwrap()), toAction(dispose)));
|
|
33
32
|
// Step 4: getIndex(0).unwrap() → TOut
|
|
34
|
-
return chain(toAction(chain(toAction(chain(toAction(
|
|
33
|
+
return chain(toAction(chain(toAction(chain(toAction(acquireParallel), toAction(runActionKeepResource))), toAction(disposeAndKeepResult))), toAction(getIndex(0).unwrap()));
|
|
35
34
|
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,6 @@ import { type Pipeable, type TypedAction, toAction } from "../ast.js";
|
|
|
2
2
|
import { chain } from "../chain.js";
|
|
3
3
|
import { all } from "../all.js";
|
|
4
4
|
import { identity } from "./scalar.js";
|
|
5
|
-
import { merge } from "./struct.js";
|
|
6
5
|
import { getIndex } from "./array.js";
|
|
7
6
|
|
|
8
7
|
// ---------------------------------------------------------------------------
|
|
@@ -12,43 +11,38 @@ import { getIndex } from "./array.js";
|
|
|
12
11
|
/**
|
|
13
12
|
* RAII-style resource management combinator.
|
|
14
13
|
*
|
|
15
|
-
* Runs `create` to acquire a resource, then
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
14
|
+
* Runs `create` to acquire a resource, then passes `[TResource, TIn]`
|
|
15
|
+
* as a tuple to the action. After the action completes, `dispose`
|
|
16
|
+
* receives the resource for cleanup. The overall combinator returns
|
|
17
|
+
* the action's output.
|
|
19
18
|
*
|
|
20
19
|
* ```
|
|
21
20
|
* TIn → create → TResource
|
|
22
|
-
* →
|
|
23
|
-
* → action(TResource & TIn) → TOut
|
|
21
|
+
* → action([TResource, TIn]) → TOut
|
|
24
22
|
* → dispose(TResource) → (discarded)
|
|
25
23
|
* → TOut
|
|
26
24
|
* ```
|
|
27
25
|
*/
|
|
28
|
-
export function withResource<
|
|
29
|
-
TIn extends Record<string, unknown>,
|
|
30
|
-
TResource extends Record<string, unknown>,
|
|
31
|
-
TOut,
|
|
32
|
-
TDisposeOut = unknown,
|
|
33
|
-
>({
|
|
26
|
+
export function withResource<TIn, TResource, TOut, TDisposeOut = unknown>({
|
|
34
27
|
create,
|
|
35
28
|
action,
|
|
36
29
|
dispose,
|
|
37
30
|
}: {
|
|
38
31
|
create: Pipeable<TIn, TResource>;
|
|
39
|
-
action: Pipeable<TResource
|
|
32
|
+
action: Pipeable<[TResource, TIn], TOut>;
|
|
40
33
|
dispose: Pipeable<TResource, TDisposeOut>;
|
|
41
34
|
}): TypedAction<TIn, TOut> {
|
|
42
|
-
// Step 1: all(create, identity) → [TResource, TIn]
|
|
43
|
-
const
|
|
44
|
-
toAction(all(create, identity())),
|
|
45
|
-
toAction(merge()),
|
|
46
|
-
);
|
|
35
|
+
// Step 1: all(create, identity) → [TResource, TIn]
|
|
36
|
+
const acquireParallel = all(create, identity());
|
|
47
37
|
|
|
48
|
-
// Step 2: all(action,
|
|
49
|
-
|
|
38
|
+
// Step 2: all(action, getIndex(0)) → [TOut, TResource]
|
|
39
|
+
// Run action on the tuple, keep raw TResource for dispose
|
|
40
|
+
const runActionKeepResource = all(
|
|
41
|
+
toAction(action),
|
|
42
|
+
toAction(getIndex(0).unwrap()),
|
|
43
|
+
);
|
|
50
44
|
|
|
51
|
-
// Step 3: all(getIndex(0)
|
|
45
|
+
// Step 3: all(getIndex(0) → TOut, getIndex(1) → dispose) → [TOut, TDisposeOut]
|
|
52
46
|
const disposeAndKeepResult = all(
|
|
53
47
|
toAction(getIndex(0).unwrap()),
|
|
54
48
|
chain(toAction(getIndex(1).unwrap()), toAction(dispose)),
|
|
@@ -59,7 +53,7 @@ export function withResource<
|
|
|
59
53
|
toAction(
|
|
60
54
|
chain(
|
|
61
55
|
toAction(
|
|
62
|
-
chain(toAction(
|
|
56
|
+
chain(toAction(acquireParallel), toAction(runActionKeepResource)),
|
|
63
57
|
),
|
|
64
58
|
toAction(disposeAndKeepResult),
|
|
65
59
|
),
|