@shivaedev/changes 0.0.0-stage → 0.3.2
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/CHANGELOG.md +75 -0
- package/LICENSE +21 -0
- package/README.md +191 -2
- package/dist/channel.d.ts +26 -0
- package/dist/channel.d.ts.map +1 -0
- package/dist/channel.js +82 -0
- package/dist/channel.js.map +1 -0
- package/dist/frame.d.ts +28 -0
- package/dist/frame.d.ts.map +1 -0
- package/dist/frame.js +64 -0
- package/dist/frame.js.map +1 -0
- package/dist/observe.d.ts +14 -0
- package/dist/observe.d.ts.map +1 -0
- package/dist/observe.js +5 -0
- package/dist/observe.js.map +1 -0
- package/dist/publish.d.ts +5 -0
- package/dist/publish.d.ts.map +1 -0
- package/dist/publish.js +12 -0
- package/dist/publish.js.map +1 -0
- package/package.json +66 -3
- package/src/channel.ts +134 -0
- package/src/frame.ts +98 -0
- package/src/observe.ts +12 -0
- package/src/publish.ts +21 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.3.2 - 2026-10-24
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- Require Effect and its companion packages at exactly 4.0.5. Generated applications use the same versions.
|
|
8
|
+
|
|
9
|
+
## 0.3.1 - 2026-10-18
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- `beforeCommit` preserves services scoped by the committing transaction when supplying its captured dependencies.
|
|
14
|
+
|
|
15
|
+
## 0.3.0 - 2026-10-13
|
|
16
|
+
|
|
17
|
+
### Breaking
|
|
18
|
+
|
|
19
|
+
- Requires stable Effect 4.0.4 in place of 4.0.0-rc.112. Effect 4.0.4 moves the former `effect/unstable/*` modules to `effect/*` and renames constructors such as `Config.string` to `Config.String`.
|
|
20
|
+
|
|
21
|
+
## 0.2.1 - 2026-10-12
|
|
22
|
+
|
|
23
|
+
### Added
|
|
24
|
+
|
|
25
|
+
- `makeChannel` accepts a typed `beforeCommit` hook and exposes a scoped `BeforeCommit` reference. `within` runs it with the deduplicated root changes inside the native transaction; a hook failure rolls back instead of publishing. Direct driver bindings can run `CommitFrame.beforeCommit` before their callback returns.
|
|
26
|
+
- `CommittedSink` can override transaction publication while retaining the ordinary sink for unframed records and batches.
|
|
27
|
+
|
|
28
|
+
### Changed
|
|
29
|
+
|
|
30
|
+
- Explain the package's purpose, transaction model and usage in the README, add agent guidance and package north-star and roadmap docs, and publish `docs/` with the README while keeping agent guidance out of the package.
|
|
31
|
+
- Build package JavaScript, declarations and source maps with TypeScript 7.
|
|
32
|
+
|
|
33
|
+
## 0.2.0 - 2026-10-04
|
|
34
|
+
|
|
35
|
+
### Changed
|
|
36
|
+
|
|
37
|
+
- Breaking: no root entry and no module that re-exports another. `exports` maps one `"./*.ts"` pattern to every module (`src` under the `source` condition, `dist` otherwise), so you import the module that defines a name, and a bundler sees only the modules you use. Where each name now lives:
|
|
38
|
+
- `@shivaedev/effect-changes`: `@shivaedev/effect-changes/channel.ts` (`Channel`, `ChannelOptions`, `makeChannel`), `@shivaedev/effect-changes/frame.ts` (`Frame`, `Outcome`), `@shivaedev/effect-changes/observe.ts` (`Observation`, `Observer`), `@shivaedev/effect-changes/publish.ts` (`Publish`, `PublishFailure`)
|
|
39
|
+
|
|
40
|
+
## 0.1.1 - 2026-10-04
|
|
41
|
+
|
|
42
|
+
### Changed
|
|
43
|
+
|
|
44
|
+
- Imports that leave their folder go through `#` aliases declared in the `imports` field of `package.json`: `#*.ts` resolves to `src` under the `source` condition and to `dist` otherwise, and `#test/*` names a test file. The published modules and declarations name the package's own files through these aliases.
|
|
45
|
+
|
|
46
|
+
- Validate packed consumers and executable bins through the shared workspace gate.
|
|
47
|
+
|
|
48
|
+
- Build with the `@shivaedev/quality` tsconfig presets, which target ESNext and allow only erasable TypeScript syntax.
|
|
49
|
+
|
|
50
|
+
- Type-check with TypeScript 7 only. TypeScript 6 still builds the package.
|
|
51
|
+
|
|
52
|
+
- Lint and format with the `@shivaedev/quality` Biome preset through `quality lint` at the repository root, which replaces the package's `check` script.
|
|
53
|
+
|
|
54
|
+
- Run tests through the `@shivaedev/quality` Vitest projects, which take a test's environment from its file name. Type tests are named `*.typecheck.test.ts`.
|
|
55
|
+
|
|
56
|
+
- Keep `package.json` in sort-package-json key order, checked by `quality lint`.
|
|
57
|
+
|
|
58
|
+
## 0.1.0 - 2026-09-26
|
|
59
|
+
|
|
60
|
+
### Added
|
|
61
|
+
|
|
62
|
+
- Add `makeChannel`: record changes inside a transaction and publish them once,
|
|
63
|
+
deduplicated by key, only after the outermost frame for their owner commits.
|
|
64
|
+
Nested frames merge on commit and are discarded on rollback; frames are kept
|
|
65
|
+
per transaction owner.
|
|
66
|
+
- Wrap exit-reporting native transactions with `within`, Promise-committing
|
|
67
|
+
drivers with `open` and `settle`, and non-transactional work with `batch`.
|
|
68
|
+
- Publish exactly when the database committed, including when the caller is
|
|
69
|
+
interrupted while `COMMIT` is in flight.
|
|
70
|
+
- Log a sink failure after commit, whether a failed Effect, a defect or a
|
|
71
|
+
synchronous throw, and keep the committed result by default;
|
|
72
|
+
`onPublishFailure: "die"` raises a defect instead.
|
|
73
|
+
- Expose `channel.Sink` and `channel.Observer` as `Context.Reference`s: tests
|
|
74
|
+
can swap the sink for a scope and observe every recorded, published and
|
|
75
|
+
discarded change.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ShivaeDev
|
|
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,192 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @shivaedev/changes
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A cache or live view needs to hear about committed writes, but sending from inside a transaction can announce data that never lands. This package collects explicitly recorded changes at an Effect transaction boundary, so a rollback announces nothing and a commit publishes the distinct notifications once.
|
|
4
|
+
|
|
5
|
+
## Why you want this
|
|
6
|
+
|
|
7
|
+
A membership grant affects both its owner and its member. The write names both subjects; the transaction boundary waits until commit before telling readers to refresh:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
yield* Effect.gen(function* () {
|
|
11
|
+
yield* sql`insert into membership (owner, member) values (${owner}, ${member})`;
|
|
12
|
+
yield* membershipChanges.record([
|
|
13
|
+
{ subject: owner, domain: "memberships" },
|
|
14
|
+
{ subject: member, domain: "memberships" },
|
|
15
|
+
]);
|
|
16
|
+
}).pipe(membershipChanges.within(sql.withTransaction));
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`membershipChanges` is the channel defined below. It groups notifications by subject and domain, so another write to the same member inside this transaction does not add a second notification. A nested transaction that rolls back contributes no notification, and a failed outer commit publishes nothing. Feature code describes what changed; the native transaction decides whether it happened.
|
|
20
|
+
|
|
21
|
+
## Using it
|
|
22
|
+
|
|
23
|
+
### How to think about it
|
|
24
|
+
|
|
25
|
+
A **channel** connects recorded changes to one publish function. A **transaction owner** identifies the database or client whose commit those changes belong to. A **transaction frame** is an ordered buffer for that owner's recorded changes: a root commit publishes the buffer, a nested commit merges it into its parent, and a rollback discards it.
|
|
26
|
+
|
|
27
|
+
Owners matter when a program uses more than one database or client. Owner B committing inside owner A's transaction publishes B's changes at B's commit, even if A later rolls back. Re-entering A inside B joins A's existing frame. A record follows the owner selected by its `owner` Effect, rather than whichever transaction is innermost in the program.
|
|
28
|
+
|
|
29
|
+
The channel owns notifications, not writes. Record descriptions of what needs refreshing beside the write that affects it. Choose a key that identifies notifications that can share one refresh; this boundary is for coalescing refresh work, rather than storing every business event.
|
|
30
|
+
|
|
31
|
+
The work has three parts:
|
|
32
|
+
|
|
33
|
+
1. Once per binding, define the change type, owner, key and publish function, and bind the native transaction.
|
|
34
|
+
2. Once per feature, record the subjects affected by each write.
|
|
35
|
+
3. In each operation, run the feature through the transaction boundary or group autocommitted work with a batch.
|
|
36
|
+
|
|
37
|
+
### 1. Once per binding: define the channel
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { makeChannel } from "@shivaedev/changes/channel.ts";
|
|
41
|
+
import { Effect } from "effect";
|
|
42
|
+
import { SqlClient } from "effect/sql";
|
|
43
|
+
|
|
44
|
+
interface MembershipChange {
|
|
45
|
+
readonly subject: string;
|
|
46
|
+
readonly domain: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const published: Array<MembershipChange> = [];
|
|
50
|
+
|
|
51
|
+
const membershipChanges = makeChannel<MembershipChange, SqlClient.SqlClient>({
|
|
52
|
+
name: "MembershipChanges",
|
|
53
|
+
owner: Effect.map(SqlClient.SqlClient, (sql) => sql.transactionService),
|
|
54
|
+
key: (change) => `${change.subject}:${change.domain}`,
|
|
55
|
+
publish: (changes) => Effect.sync(() => {
|
|
56
|
+
published.push(...changes);
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The owner is the native SQL client's transaction-service identity. The key groups each subject and domain. The publish function collects notifications here; replace it with the application's notification or invalidation Effect. The channel's `R` parameter describes the services its owner and publish function require.
|
|
62
|
+
|
|
63
|
+
`within(native)(body)` wraps an Effect transaction combinator. Its native combinator must run the body once and return success only after the root commit or successful native nested outcome. A successful nested scope can leave its savepoint open until the root transaction settles. Use an existing binding when it already owns the job: [sql](https://github.com/ShivaeDev/platform/tree/main/packages/sql#readme) provides the SQL boundary for Reactivity keys.
|
|
64
|
+
|
|
65
|
+
### 2. Once per feature: record the affected subjects
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const grantMembership = (owner: string, member: string) =>
|
|
69
|
+
Effect.gen(function* () {
|
|
70
|
+
const sql = yield* SqlClient.SqlClient;
|
|
71
|
+
yield* sql`insert into membership (owner, member) values (${owner}, ${member})`;
|
|
72
|
+
yield* membershipChanges.record([
|
|
73
|
+
{ subject: owner, domain: "memberships" },
|
|
74
|
+
{ subject: member, domain: "memberships" },
|
|
75
|
+
]);
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
One write can name several subjects. `record` deduplicates the supplied changes by the channel's key, and the transaction frame deduplicates across later records and committed nested frames. Keys retain their first-seen order.
|
|
80
|
+
|
|
81
|
+
Record while the transaction body is still running. A record or a nested frame opened from work that outlives its settled frame dies instead of silently losing the changes. A child frame also cannot merge into a parent that has already settled.
|
|
82
|
+
|
|
83
|
+
### 3. In an operation: use the transaction boundary
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const grant = (owner: string, member: string) =>
|
|
87
|
+
Effect.gen(function* () {
|
|
88
|
+
const sql = yield* SqlClient.SqlClient;
|
|
89
|
+
return yield* grantMembership(owner, member).pipe(
|
|
90
|
+
membershipChanges.within(sql.withTransaction),
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Provide the application's native SQL layer when running `grant`. Publication happens after the native transaction succeeds and before the wrapper returns. A typed failure, defect, body interruption or failed commit discards the root frame. A nested commit merges into its parent; a nested rollback leaves the parent's own changes intact.
|
|
96
|
+
|
|
97
|
+
When interruption arrives while commit is in flight, the boundary waits for the native outcome. A successful commit still publishes its changes, and the caller still sees the interruption.
|
|
98
|
+
|
|
99
|
+
Concurrent records from Effects that inherit the same frame join that frame and publish together. Keep all write-related work within the transaction's lifetime.
|
|
100
|
+
|
|
101
|
+
### Group already-autocommitted writes
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
const grantSeveral = Effect.gen(function* () {
|
|
105
|
+
yield* grantMembership("ada", "bob");
|
|
106
|
+
yield* grantMembership("ada", "cyd");
|
|
107
|
+
}).pipe(membershipChanges.batch);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
This example uses no transaction: the native SQL writes autocommit, while `batch` groups their notifications. Success, typed failure and interruption all publish the batch's recorded changes, because those writes have already landed.
|
|
111
|
+
|
|
112
|
+
A transaction inside a batch merges into the batch when it commits and drops its changes when it rolls back. A batch inside a transaction merges into that transaction, including when the batch fails and its failure is caught; the enclosing transaction decides whether those changes publish.
|
|
113
|
+
|
|
114
|
+
Without a frame or batch, a record publishes immediately. An optional `unowned` guard runs before an unframed record or a root transaction frame opens. A batch is not an owned transaction, so its records and transactions opened inside it still run that guard. A binding can use the guard to refuse a native transaction whose commit the channel cannot observe.
|
|
115
|
+
|
|
116
|
+
### Bind a driver that reports its outcome through a Promise
|
|
117
|
+
|
|
118
|
+
`open` and `Frame` let a driver binding control settlement directly. Open the frame before entering the driver's transaction callback, provide the callback's Effect body with `frame.provide`, and settle only after the driver reports its commit outcome.
|
|
119
|
+
|
|
120
|
+
Capture the application's Effect context before running callback work in another fiber. Its Effect exit describes the body, not whether the driver committed. A binding must make a failed body reject the driver's transaction, wait for its Promise, and translate that driver's result into an `Outcome`.
|
|
121
|
+
|
|
122
|
+
Settle with `"committed"` only after the root commit or successful native nested outcome, and `"rolledBack"` when that operation fails. The first settlement outcome wins, so calling it again cannot publish twice.
|
|
123
|
+
|
|
124
|
+
For an interruptible Promise driver, the binding must keep waiting for the pending transaction result after caller interruption and settle from that result. Do not treat an interrupted wait or a successful body as the driver's commit result.
|
|
125
|
+
|
|
126
|
+
### Decide what a publish failure means
|
|
127
|
+
|
|
128
|
+
A failed publish function runs after the transaction's data has committed. The default `onPublishFailure: "log"` logs an Error with its cause, channel name and change count, while preserving the committed result. This applies to an Effect failure, a defect and a synchronous throw from the publish function.
|
|
129
|
+
|
|
130
|
+
Set `onPublishFailure: "die"` when that same delivery failure should raise a defect. The data remains committed; this policy does not undo the write. Any retry policy belongs in the publish function.
|
|
131
|
+
|
|
132
|
+
### Capture notifications in tests
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
import type { Observation } from "@shivaedev/changes/observe.ts";
|
|
136
|
+
import { Layer } from "effect";
|
|
137
|
+
|
|
138
|
+
const captured: Array<MembershipChange> = [];
|
|
139
|
+
const observations: Array<Observation<MembershipChange>> = [];
|
|
140
|
+
|
|
141
|
+
const captureSink = Layer.succeed(
|
|
142
|
+
membershipChanges.Sink,
|
|
143
|
+
(changes: readonly MembershipChange[]) => Effect.sync(() => {
|
|
144
|
+
captured.push(...changes);
|
|
145
|
+
}),
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const observe = Layer.succeed(
|
|
149
|
+
membershipChanges.Observer,
|
|
150
|
+
(observation: Observation<MembershipChange>) => Effect.sync(() => {
|
|
151
|
+
observations.push(observation);
|
|
152
|
+
}),
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const capturedGrant = grant("ada", "bob").pipe(
|
|
156
|
+
Effect.provide(Layer.merge(captureSink, observe)),
|
|
157
|
+
);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`Sink` and `Observer` are channel-specific `Context.Reference`s. Within the provided scope, the sink receives the changes instead of the configured publish function; the configured sink resumes outside it. The observer receives `Recorded` for the distinct changes in a record call, `Published` for the buffer passed to publication and `Discarded` for a rolled-back frame's changes. `Published` is emitted before the sink runs; it does not prove successful delivery. Discarded parent changes include changes merged from children that committed earlier. A rollback therefore remains visible to a coverage check through its earlier `Recorded` observation.
|
|
161
|
+
|
|
162
|
+
### Work inside the committing transaction
|
|
163
|
+
|
|
164
|
+
`makeChannel<A, R, E>` accepts an optional `beforeCommit: (changes: readonly A[]) => Effect<void, E, R>`. Its scoped `BeforeCommit` reference lets a binding replace that hook. `within` runs the hook after a successful body, with deduplicated changes from successful nested work, before the native root transaction commits. Empty transactions and rolled-back bodies do not run it. A typed hook failure is passed to the native transaction and rolls back the write. The hook does not run for an unframed record or a batch of already committed writes.
|
|
165
|
+
|
|
166
|
+
A direct driver binding using `open` must run `frame.beforeCommit` inside its transaction callback after the body succeeds, before returning to the driver. Then settle from the driver's actual commit result. `CommittedSink` optionally replaces the transaction's after-commit sink; the ordinary `Sink` still handles unframed records and batches. A PostgreSQL binding can use this to deliver through its listener once instead of also publishing locally after commit.
|
|
167
|
+
|
|
168
|
+
### API
|
|
169
|
+
|
|
170
|
+
Import from the module that defines the name; the package has no root entry.
|
|
171
|
+
|
|
172
|
+
| Module | Names |
|
|
173
|
+
| --- | --- |
|
|
174
|
+
| `channel.ts` | `makeChannel`, `ChannelOptions`, `Channel`, `BeforeCommit` |
|
|
175
|
+
| `frame.ts` | `Frame`, `CommitFrame`, `Outcome` |
|
|
176
|
+
| `observe.ts` | `Observation`, `Observer` |
|
|
177
|
+
| `publish.ts` | `Publish`, `PublishFailure` |
|
|
178
|
+
|
|
179
|
+
`key` is optional; for string changes, omitting it deduplicates repeated strings. A custom key is useful when a change is a structured description of an affected subject.
|
|
180
|
+
|
|
181
|
+
### Install, setup and limits
|
|
182
|
+
|
|
183
|
+
```sh
|
|
184
|
+
pnpm add @shivaedev/changes effect@4.0.5
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
`effect` is a peer dependency, so the application's Effect version runs the channel. The package declares Node 24 or newer. The SQL example also needs the application's native SQL driver and layer; neither the channel nor its installation creates a database connection or table.
|
|
188
|
+
|
|
189
|
+
- The channel is an in-process notification boundary. It has no persistent outbox, replay or cross-process transport; deployment delivery requires a separate design.
|
|
190
|
+
- Only explicitly recorded changes reach the channel. Trigger, cascade and raw SQL write discovery belongs to a binding or the application.
|
|
191
|
+
- A native adapter must observe the real commit outcome. Wrapping only the body of a transaction does not establish that outcome.
|
|
192
|
+
- Use a durable event design when every business event must be retained. A deduplicated notification is a request to refresh, not an event journal.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Context, Effect } from "effect";
|
|
2
|
+
import { type CommitFrame } from "./frame.ts";
|
|
3
|
+
import { type Observer } from "./observe.ts";
|
|
4
|
+
import { type Publish, type PublishFailure } from "./publish.ts";
|
|
5
|
+
export type BeforeCommit<A, E = never, R = never> = (changes: readonly A[]) => Effect.Effect<void, E, R>;
|
|
6
|
+
export interface ChannelOptions<A, R, E = never> {
|
|
7
|
+
readonly beforeCommit?: BeforeCommit<A, E, R>;
|
|
8
|
+
readonly key?: (change: A) => unknown;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly onPublishFailure?: PublishFailure;
|
|
11
|
+
readonly owner: Effect.Effect<unknown, never, R>;
|
|
12
|
+
readonly publish: Publish<A, R>;
|
|
13
|
+
readonly unowned?: Effect.Effect<void, never, R>;
|
|
14
|
+
}
|
|
15
|
+
export interface Channel<A, R, THookError = never> {
|
|
16
|
+
readonly BeforeCommit: Context.Reference<BeforeCommit<A, THookError, R>>;
|
|
17
|
+
readonly batch: <TValue, E, TBodyRequirements>(body: Effect.Effect<TValue, E, TBodyRequirements>) => Effect.Effect<TValue, E, R | TBodyRequirements>;
|
|
18
|
+
readonly CommittedSink: Context.Reference<Publish<A, R> | undefined>;
|
|
19
|
+
readonly Observer: Context.Reference<Observer<A>>;
|
|
20
|
+
readonly open: Effect.Effect<CommitFrame<THookError>, never, R>;
|
|
21
|
+
readonly record: (changes: Iterable<A>) => Effect.Effect<void, never, R>;
|
|
22
|
+
readonly Sink: Context.Reference<Publish<A, R>>;
|
|
23
|
+
readonly within: <TValue, E, TBodyRequirements, TNativeError, TNativeRequirements>(native: (body: Effect.Effect<TValue, E | THookError, TBodyRequirements>) => Effect.Effect<TValue, TNativeError, TNativeRequirements>) => (body: Effect.Effect<TValue, E, TBodyRequirements>) => Effect.Effect<TValue, TNativeError, R | TNativeRequirements>;
|
|
24
|
+
}
|
|
25
|
+
export declare function makeChannel<A, R = never, THookError = never>(options: ChannelOptions<A, R, THookError>): Channel<A, R, THookError>;
|
|
26
|
+
//# sourceMappingURL=channel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAQ,MAAM,QAAQ,CAAC;AAC/C,OAAO,EAAoB,KAAK,WAAW,EAAyC,MAAM,YAAY,CAAC;AACvG,OAAO,EAAoB,KAAK,QAAQ,EAAc,MAAM,cAAc,CAAC;AAC3E,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,cAAc,EAAa,MAAM,cAAc,CAAC;AAE5E,MAAM,MAAM,YAAY,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,KAAK,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AAEzG,MAAM,WAAW,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,KAAK;IAC9C,QAAQ,CAAC,YAAY,CAAC,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAC9C,QAAQ,CAAC,GAA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,QAAQ,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;IACzE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAChD,QAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,iBAAiB,EAAE,YAAY,EAAE,mBAAmB,EAChF,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,UAAU,EAAE,iBAAiB,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,EAAE,mBAAmB,CAAC,KAChI,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,iBAAiB,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,EAAE,CAAC,GAAG,mBAAmB,CAAC,CAAC;CACzH;AAID,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,UAAU,GAAG,KAAK,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,CAmGlI"}
|
package/dist/channel.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Context, Effect, Exit } from "effect";
|
|
2
|
+
import { add, keyed, makeBuffer, makeFrame, settled } from "./frame.js";
|
|
3
|
+
import { unobserved } from "./observe.js";
|
|
4
|
+
import { publisher } from "./publish.js";
|
|
5
|
+
let channels = 0;
|
|
6
|
+
export function makeChannel(options) {
|
|
7
|
+
const { name } = options;
|
|
8
|
+
const prefix = `@shivaedev/changes/${name}/${channels}`;
|
|
9
|
+
channels += 1;
|
|
10
|
+
const Frames = Context.Reference(`${prefix}/Frames`, { defaultValue: () => new Map() });
|
|
11
|
+
const BeforeCommit = Context.Reference(`${prefix}/BeforeCommit`, {
|
|
12
|
+
defaultValue: () => options.beforeCommit ?? (() => Effect.void),
|
|
13
|
+
});
|
|
14
|
+
const CommittedSink = Context.Reference(`${prefix}/CommittedSink`, { defaultValue: () => undefined });
|
|
15
|
+
const CurrentSink = Context.Reference(`${prefix}/Sink`, { defaultValue: () => options.publish });
|
|
16
|
+
const CurrentObserver = Context.Reference(`${prefix}/Observer`, { defaultValue: () => unobserved });
|
|
17
|
+
const keyOf = options.key ?? ((change) => change);
|
|
18
|
+
const publish = publisher(name, options.onPublishFailure ?? "log");
|
|
19
|
+
const guard = options.unowned ?? Effect.void;
|
|
20
|
+
function unguarded(frame) {
|
|
21
|
+
return frame === undefined || frame.kind === "batch";
|
|
22
|
+
}
|
|
23
|
+
function observe(observation) {
|
|
24
|
+
return Effect.flatMap(Effect.service(CurrentObserver), (observer) => observer(observation));
|
|
25
|
+
}
|
|
26
|
+
function deliver(changes, committed = false) {
|
|
27
|
+
return Effect.andThen(observe({ _tag: "Published", changes }), Effect.gen(function* () {
|
|
28
|
+
const override = committed ? yield* CommittedSink : undefined;
|
|
29
|
+
yield* publish(override ?? (yield* CurrentSink), changes);
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
const locate = Effect.gen(function* () {
|
|
33
|
+
const owner = yield* options.owner;
|
|
34
|
+
const frames = yield* Frames;
|
|
35
|
+
return { frame: frames.get(owner), frames, owner };
|
|
36
|
+
});
|
|
37
|
+
const record = Effect.fn("Changes.record")(function* (changes) {
|
|
38
|
+
const { frame } = yield* locate;
|
|
39
|
+
if (unguarded(frame)) {
|
|
40
|
+
yield* guard;
|
|
41
|
+
}
|
|
42
|
+
const entries = keyed(changes, keyOf);
|
|
43
|
+
if (frame !== undefined) {
|
|
44
|
+
yield* add(name, frame, entries);
|
|
45
|
+
}
|
|
46
|
+
if (entries.size === 0) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const distinct = [...entries.values()];
|
|
50
|
+
yield* observe({ _tag: "Recorded", changes: distinct });
|
|
51
|
+
if (frame === undefined) {
|
|
52
|
+
yield* deliver(distinct);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
function openAs(kind) {
|
|
56
|
+
return Effect.gen(function* () {
|
|
57
|
+
const { owner, frames, frame: parent } = yield* locate;
|
|
58
|
+
if (parent !== undefined && !parent.open) {
|
|
59
|
+
return yield* settled(name);
|
|
60
|
+
}
|
|
61
|
+
if (kind === "transaction" && unguarded(parent)) {
|
|
62
|
+
yield* guard;
|
|
63
|
+
}
|
|
64
|
+
const buffer = makeBuffer(kind, parent);
|
|
65
|
+
const inner = new Map(frames).set(owner, buffer);
|
|
66
|
+
const context = yield* Effect.context();
|
|
67
|
+
return makeFrame({
|
|
68
|
+
beforeCommit: (changes) => Effect.updateContext(Effect.flatMap(Effect.service(BeforeCommit), (hook) => hook(changes)), (current) => Context.merge(context, current)),
|
|
69
|
+
buffer,
|
|
70
|
+
discard: (changes) => Effect.provideContext(observe({ _tag: "Discarded", changes }), context),
|
|
71
|
+
name,
|
|
72
|
+
provide: (body) => Effect.provideService(body, Frames, inner),
|
|
73
|
+
publish: (changes) => Effect.provideContext(deliver(changes, kind === "transaction"), context),
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
const open = openAs("transaction");
|
|
78
|
+
const within = (native) => (body) => Effect.uninterruptibleMask((restore) => Effect.flatMap(open, (frame) => Effect.onExit(native(restore(frame.provide(Effect.tap(body, () => frame.beforeCommit)))), (exit) => frame.settle(Exit.isSuccess(exit) ? "committed" : "rolledBack"))));
|
|
79
|
+
const batch = (body) => Effect.uninterruptibleMask((restore) => Effect.flatMap(openAs("batch"), (frame) => Effect.onExit(restore(frame.provide(body)), () => frame.settle("committed"))));
|
|
80
|
+
return { BeforeCommit, batch, CommittedSink, Observer: CurrentObserver, open, record, Sink: CurrentSink, within };
|
|
81
|
+
}
|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AAC"}
|
package/dist/frame.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
export type Outcome = "committed" | "rolledBack";
|
|
3
|
+
export interface Frame {
|
|
4
|
+
readonly provide: <TValue, TError, TRequirements>(body: Effect.Effect<TValue, TError, TRequirements>) => Effect.Effect<TValue, TError, TRequirements>;
|
|
5
|
+
readonly settle: (outcome: Outcome) => Effect.Effect<void>;
|
|
6
|
+
}
|
|
7
|
+
export interface CommitFrame<E = never> extends Frame {
|
|
8
|
+
readonly beforeCommit: Effect.Effect<void, E>;
|
|
9
|
+
}
|
|
10
|
+
export interface Buffer<A> {
|
|
11
|
+
readonly changes: Map<unknown, A>;
|
|
12
|
+
readonly kind: "transaction" | "batch";
|
|
13
|
+
open: boolean;
|
|
14
|
+
readonly parent: Buffer<A> | undefined;
|
|
15
|
+
}
|
|
16
|
+
export declare function makeBuffer<A>(kind: Buffer<A>["kind"], parent: Buffer<A> | undefined): Buffer<A>;
|
|
17
|
+
export declare function settled(name: string): Effect.Effect<never>;
|
|
18
|
+
export declare function add<A>(name: string, buffer: Buffer<A>, entries: Iterable<readonly [unknown, A]>): Effect.Effect<void>;
|
|
19
|
+
export declare function keyed<A>(changes: Iterable<A>, keyOf: (change: A) => unknown): Map<unknown, A>;
|
|
20
|
+
export declare function makeFrame<A, E = never>(options: {
|
|
21
|
+
readonly beforeCommit?: (changes: readonly A[]) => Effect.Effect<void, E>;
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly buffer: Buffer<A>;
|
|
24
|
+
readonly provide: <TValue, TError, TRequirements>(body: Effect.Effect<TValue, TError, TRequirements>) => Effect.Effect<TValue, TError, TRequirements>;
|
|
25
|
+
readonly publish: (changes: readonly A[]) => Effect.Effect<void>;
|
|
26
|
+
readonly discard: (changes: readonly A[]) => Effect.Effect<void>;
|
|
27
|
+
}): CommitFrame<E>;
|
|
28
|
+
//# sourceMappingURL=frame.d.ts.map
|
|
@@ -0,0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|
package/dist/frame.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
export function makeBuffer(kind, parent) {
|
|
3
|
+
return {
|
|
4
|
+
changes: new Map(),
|
|
5
|
+
kind,
|
|
6
|
+
open: true,
|
|
7
|
+
parent,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export function settled(name) {
|
|
11
|
+
return Effect.die(new Error(`${name}: changes arrived after their transaction settled; record them before the transaction body returns`));
|
|
12
|
+
}
|
|
13
|
+
export function add(name, buffer, entries) {
|
|
14
|
+
return buffer.open
|
|
15
|
+
? Effect.sync(() => {
|
|
16
|
+
for (const [key, change] of entries) {
|
|
17
|
+
if (!buffer.changes.has(key)) {
|
|
18
|
+
buffer.changes.set(key, change);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
: settled(name);
|
|
23
|
+
}
|
|
24
|
+
export function keyed(changes, keyOf) {
|
|
25
|
+
const entries = new Map();
|
|
26
|
+
for (const change of changes) {
|
|
27
|
+
const key = keyOf(change);
|
|
28
|
+
if (!entries.has(key)) {
|
|
29
|
+
entries.set(key, change);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return entries;
|
|
33
|
+
}
|
|
34
|
+
function transactionParent(buffer) {
|
|
35
|
+
return buffer !== undefined && (buffer.kind === "transaction" || transactionParent(buffer.parent));
|
|
36
|
+
}
|
|
37
|
+
export function makeFrame(options) {
|
|
38
|
+
const { name, buffer } = options;
|
|
39
|
+
const settle = Effect.fn("Changes.settle")(function* (outcome) {
|
|
40
|
+
if (!buffer.open) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
buffer.open = false;
|
|
44
|
+
if (outcome === "committed" && buffer.parent !== undefined) {
|
|
45
|
+
return yield* add(name, buffer.parent, buffer.changes);
|
|
46
|
+
}
|
|
47
|
+
const changes = [...buffer.changes.values()];
|
|
48
|
+
if (changes.length === 0) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
yield* outcome === "committed" ? options.publish(changes) : options.discard(changes);
|
|
52
|
+
}, Effect.uninterruptible);
|
|
53
|
+
const beforeCommit = Effect.suspend(() => {
|
|
54
|
+
if (!buffer.open) {
|
|
55
|
+
return settled(name);
|
|
56
|
+
}
|
|
57
|
+
if (buffer.kind !== "transaction" || transactionParent(buffer.parent) || buffer.changes.size === 0) {
|
|
58
|
+
return Effect.void;
|
|
59
|
+
}
|
|
60
|
+
return options.beforeCommit?.([...buffer.changes.values()]) ?? Effect.void;
|
|
61
|
+
});
|
|
62
|
+
return { beforeCommit, provide: options.provide, settle };
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=frame.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frame.js","sourceRoot":"","sources":["../src/frame.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAsBhC,MAAM,UAAU,UAAU,CAAI,IAAuB,EAAE,MAA6B;IACnF,OAAO;QACN,OAAO,EAAE,IAAI,GAAG,EAAE;QAClB,IAAI;QACJ,IAAI,EAAE,IAAI;QACV,MAAM;KACN,CAAC;AACH,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,IAAY;IACnC,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC,GAAG,IAAI,oGAAoG,CAAC,CAAC,CAAC;AAC3I,CAAC;AAED,MAAM,UAAU,GAAG,CAAI,IAAY,EAAE,MAAiB,EAAE,OAAwC;IAC/F,OAAO,MAAM,CAAC,IAAI;QACjB,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;YACjB,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;gBACrC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;oBAC9B,MAAM,CAAC,OAAO,CAAC,GAAG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AG,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QAC7C,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1B,OAAO;QACR,CAAC;QACD,KAAK,CAAC,CAAC,OAAO,KAAK,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACtF,CAAC,EAAE,MAAM,CAAC,eAAe,CAAC,CAAC;IAC3B,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE;QACxC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YAClB,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;QACtB,CAAC;QACD,IAAI,MAAM,CAAC,IAAI,KAAK,aAAa,IAAI,iBAAiB,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;YACpG,OAAO,MAAM,CAAC,IAAI,CAAC;QACpB,CAAC;QACD,OAAO,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC;IAC5E,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;AAC3D,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
export type Observation<A> = {
|
|
3
|
+
readonly _tag: "Recorded";
|
|
4
|
+
readonly changes: readonly A[];
|
|
5
|
+
} | {
|
|
6
|
+
readonly _tag: "Published";
|
|
7
|
+
readonly changes: readonly A[];
|
|
8
|
+
} | {
|
|
9
|
+
readonly _tag: "Discarded";
|
|
10
|
+
readonly changes: readonly A[];
|
|
11
|
+
};
|
|
12
|
+
export type Observer<A> = (observation: Observation<A>) => Effect.Effect<void>;
|
|
13
|
+
export declare function unobserved(_observation: unknown): Effect.Effect<void>;
|
|
14
|
+
//# sourceMappingURL=observe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observe.d.ts","sourceRoot":"","sources":["../src/observe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,MAAM,WAAW,CAAC,CAAC,IACtB;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,CAAA;CAAE,GAC7D;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,CAAA;CAAE,GAC9D;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,CAAA;CAAE,CAAC;AAElE,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAE/E,wBAAgB,UAAU,CAAC,YAAY,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAErE"}
|
package/dist/observe.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observe.js","sourceRoot":"","sources":["../src/observe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAShC,MAAM,UAAU,UAAU,CAAC,YAAqB;IAC/C,OAAO,MAAM,CAAC,IAAI,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
export type PublishFailure = "log" | "die";
|
|
3
|
+
export type Publish<A, R = never> = (changes: readonly A[]) => Effect.Effect<void, unknown, R>;
|
|
4
|
+
export declare function publisher<A, R>(name: string, policy: PublishFailure): (sink: Publish<A, R>, changes: readonly A[]) => Effect.Effect<void, never, R>;
|
|
5
|
+
//# sourceMappingURL=publish.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"publish.d.ts","sourceRoot":"","sources":["../src/publish.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,KAAK,CAAC;AAE3C,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;AAE/F,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAcnJ"}
|
package/dist/publish.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
export function publisher(name, policy) {
|
|
3
|
+
return Effect.fn("Changes.publish")(function* (sink, changes) {
|
|
4
|
+
yield* Effect.annotateCurrentSpan({ "changes.channel": name, "changes.count": changes.length });
|
|
5
|
+
const sent = Effect.suspend(() => sink(changes));
|
|
6
|
+
if (policy === "die") {
|
|
7
|
+
return yield* Effect.orDie(sent);
|
|
8
|
+
}
|
|
9
|
+
yield* Effect.catchCause(sent, (cause) => Effect.andThen(Effect.annotateCurrentSpan("changes.published", false), Effect.logError("Changes.publish failed after the changes were committed; the committed result stands", cause)).pipe(Effect.annotateLogs({ changes: changes.length, channel: name })));
|
|
10
|
+
}, Effect.uninterruptible);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=publish.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"publish.js","sourceRoot":"","sources":["../src/publish.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAMhC,MAAM,UAAU,SAAS,CAAO,IAAY,EAAE,MAAsB;IACnE,OAAO,MAAM,CAAC,EAAE,CAAC,iBAAiB,CAAC,CAAC,QAAQ,CAAC,EAAE,IAAmB,EAAE,OAAqB;QACxF,KAAK,CAAC,CAAC,MAAM,CAAC,mBAAmB,CAAC,EAAE,iBAAiB,EAAE,IAAI,EAAE,eAAe,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QAChG,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QACjD,IAAI,MAAM,KAAK,KAAK,EAAE,CAAC;YACtB,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAClC,CAAC;QACD,KAAK,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CACxC,MAAM,CAAC,OAAO,CACb,MAAM,CAAC,mBAAmB,CAAC,mBAAmB,EAAE,KAAK,CAAC,EACtD,MAAM,CAAC,QAAQ,CAAC,sFAAsF,EAAE,KAAK,CAAC,CAC9G,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CACvE,CAAC;IACH,CAAC,EAAE,MAAM,CAAC,eAAe,CAAC,CAAC;AAC5B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/changes",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "Commit-bound change channels for Effect: publish recorded changes only after the transaction commits",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"effect"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://github.com/ShivaeDev/platform/tree/main/packages/changes#readme",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/ShivaeDev/platform/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/ShivaeDev/platform.git",
|
|
15
|
+
"directory": "packages/changes"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"type": "module",
|
|
20
|
+
"imports": {
|
|
21
|
+
"#test/*": "./src/test-support/*",
|
|
22
|
+
"#*.ts": {
|
|
23
|
+
"source": "./src/*.ts",
|
|
24
|
+
"types": "./dist/*.d.ts",
|
|
25
|
+
"default": "./dist/*.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"exports": {
|
|
29
|
+
"./*.ts": {
|
|
30
|
+
"source": "./src/*.ts",
|
|
31
|
+
"types": "./dist/*.d.ts",
|
|
32
|
+
"default": "./dist/*.js"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"src",
|
|
39
|
+
"!src/**/*.test.ts",
|
|
40
|
+
"!src/**/*.spec.ts",
|
|
41
|
+
"!src/**/test-support",
|
|
42
|
+
"CHANGELOG.md",
|
|
43
|
+
"README.md"
|
|
44
|
+
],
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@shivaedev/quality": "0.41.1",
|
|
47
|
+
"@shivaedev/test-story": "0.20.1",
|
|
48
|
+
"@types/node": "24.19.2",
|
|
49
|
+
"effect": "4.0.5",
|
|
50
|
+
"typescript": "7.0.2",
|
|
51
|
+
"vitest": "5.0.5"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"effect": "4.0.5"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=24"
|
|
58
|
+
},
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public",
|
|
61
|
+
"provenance": true
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "node --eval \"import('node:fs').then(({ rmSync }) => rmSync('dist', { force: true, recursive: true }))\" && tsc --project tsconfig.build.json",
|
|
65
|
+
"ready": "pnpm typecheck && pnpm test && pnpm build",
|
|
66
|
+
"test": "vitest run",
|
|
67
|
+
"typecheck": "tsc --noEmit"
|
|
68
|
+
}
|
|
6
69
|
}
|
package/src/channel.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { Context, Effect, Exit } from "effect";
|
|
2
|
+
import { add, type Buffer, type CommitFrame, keyed, makeBuffer, makeFrame, settled } from "./frame.ts";
|
|
3
|
+
import { type Observation, type Observer, unobserved } from "./observe.ts";
|
|
4
|
+
import { type Publish, type PublishFailure, publisher } from "./publish.ts";
|
|
5
|
+
|
|
6
|
+
export type BeforeCommit<A, E = never, R = never> = (changes: readonly A[]) => Effect.Effect<void, E, R>;
|
|
7
|
+
|
|
8
|
+
export interface ChannelOptions<A, R, E = never> {
|
|
9
|
+
readonly beforeCommit?: BeforeCommit<A, E, R>;
|
|
10
|
+
readonly key?: (change: A) => unknown;
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly onPublishFailure?: PublishFailure;
|
|
13
|
+
readonly owner: Effect.Effect<unknown, never, R>;
|
|
14
|
+
readonly publish: Publish<A, R>;
|
|
15
|
+
readonly unowned?: Effect.Effect<void, never, R>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Channel<A, R, THookError = never> {
|
|
19
|
+
readonly BeforeCommit: Context.Reference<BeforeCommit<A, THookError, R>>;
|
|
20
|
+
readonly batch: <TValue, E, TBodyRequirements>(
|
|
21
|
+
body: Effect.Effect<TValue, E, TBodyRequirements>,
|
|
22
|
+
) => Effect.Effect<TValue, E, R | TBodyRequirements>;
|
|
23
|
+
readonly CommittedSink: Context.Reference<Publish<A, R> | undefined>;
|
|
24
|
+
readonly Observer: Context.Reference<Observer<A>>;
|
|
25
|
+
readonly open: Effect.Effect<CommitFrame<THookError>, never, R>;
|
|
26
|
+
readonly record: (changes: Iterable<A>) => Effect.Effect<void, never, R>;
|
|
27
|
+
readonly Sink: Context.Reference<Publish<A, R>>;
|
|
28
|
+
readonly within: <TValue, E, TBodyRequirements, TNativeError, TNativeRequirements>(
|
|
29
|
+
native: (body: Effect.Effect<TValue, E | THookError, TBodyRequirements>) => Effect.Effect<TValue, TNativeError, TNativeRequirements>,
|
|
30
|
+
) => (body: Effect.Effect<TValue, E, TBodyRequirements>) => Effect.Effect<TValue, TNativeError, R | TNativeRequirements>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let channels = 0;
|
|
34
|
+
|
|
35
|
+
export function makeChannel<A, R = never, THookError = never>(options: ChannelOptions<A, R, THookError>): Channel<A, R, THookError> {
|
|
36
|
+
const { name } = options;
|
|
37
|
+
const prefix = `@shivaedev/changes/${name}/${channels}`;
|
|
38
|
+
channels += 1;
|
|
39
|
+
const Frames = Context.Reference<ReadonlyMap<unknown, Buffer<A>>>(`${prefix}/Frames`, { defaultValue: () => new Map() });
|
|
40
|
+
const BeforeCommit = Context.Reference<BeforeCommit<A, THookError, R>>(`${prefix}/BeforeCommit`, {
|
|
41
|
+
defaultValue: () => options.beforeCommit ?? (() => Effect.void),
|
|
42
|
+
});
|
|
43
|
+
const CommittedSink = Context.Reference<Publish<A, R> | undefined>(`${prefix}/CommittedSink`, { defaultValue: () => undefined });
|
|
44
|
+
const CurrentSink = Context.Reference<Publish<A, R>>(`${prefix}/Sink`, { defaultValue: () => options.publish });
|
|
45
|
+
const CurrentObserver = Context.Reference<Observer<A>>(`${prefix}/Observer`, { defaultValue: () => unobserved });
|
|
46
|
+
const keyOf = options.key ?? ((change: A): unknown => change);
|
|
47
|
+
const publish = publisher<A, R>(name, options.onPublishFailure ?? "log");
|
|
48
|
+
const guard = options.unowned ?? Effect.void;
|
|
49
|
+
function unguarded(frame: Buffer<A> | undefined) {
|
|
50
|
+
return frame === undefined || frame.kind === "batch";
|
|
51
|
+
}
|
|
52
|
+
function observe(observation: Observation<A>) {
|
|
53
|
+
return Effect.flatMap(Effect.service(CurrentObserver), (observer) => observer(observation));
|
|
54
|
+
}
|
|
55
|
+
function deliver(changes: readonly A[], committed = false) {
|
|
56
|
+
return Effect.andThen(
|
|
57
|
+
observe({ _tag: "Published", changes }),
|
|
58
|
+
Effect.gen(function* () {
|
|
59
|
+
const override = committed ? yield* CommittedSink : undefined;
|
|
60
|
+
yield* publish(override ?? (yield* CurrentSink), changes);
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const locate = Effect.gen(function* () {
|
|
66
|
+
const owner = yield* options.owner;
|
|
67
|
+
const frames = yield* Frames;
|
|
68
|
+
return { frame: frames.get(owner), frames, owner };
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const record = Effect.fn("Changes.record")(function* (changes: Iterable<A>) {
|
|
72
|
+
const { frame } = yield* locate;
|
|
73
|
+
if (unguarded(frame)) {
|
|
74
|
+
yield* guard;
|
|
75
|
+
}
|
|
76
|
+
const entries = keyed(changes, keyOf);
|
|
77
|
+
if (frame !== undefined) {
|
|
78
|
+
yield* add(name, frame, entries);
|
|
79
|
+
}
|
|
80
|
+
if (entries.size === 0) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const distinct = [...entries.values()];
|
|
84
|
+
yield* observe({ _tag: "Recorded", changes: distinct });
|
|
85
|
+
if (frame === undefined) {
|
|
86
|
+
yield* deliver(distinct);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
function openAs(kind: Buffer<A>["kind"]) {
|
|
91
|
+
return Effect.gen(function* () {
|
|
92
|
+
const { owner, frames, frame: parent } = yield* locate;
|
|
93
|
+
if (parent !== undefined && !parent.open) {
|
|
94
|
+
return yield* settled(name);
|
|
95
|
+
}
|
|
96
|
+
if (kind === "transaction" && unguarded(parent)) {
|
|
97
|
+
yield* guard;
|
|
98
|
+
}
|
|
99
|
+
const buffer = makeBuffer(kind, parent);
|
|
100
|
+
const inner = new Map(frames).set(owner, buffer);
|
|
101
|
+
const context = yield* Effect.context<R>();
|
|
102
|
+
return makeFrame({
|
|
103
|
+
beforeCommit: (changes) =>
|
|
104
|
+
Effect.updateContext<void, THookError, R, never>(
|
|
105
|
+
Effect.flatMap(Effect.service(BeforeCommit), (hook) => hook(changes)),
|
|
106
|
+
(current: Context.Context<never>) => Context.merge(context, current),
|
|
107
|
+
),
|
|
108
|
+
buffer,
|
|
109
|
+
discard: (changes) => Effect.provideContext(observe({ _tag: "Discarded", changes }), context),
|
|
110
|
+
name,
|
|
111
|
+
provide: (body) => Effect.provideService(body, Frames, inner),
|
|
112
|
+
publish: (changes) => Effect.provideContext(deliver(changes, kind === "transaction"), context),
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const open = openAs("transaction");
|
|
118
|
+
|
|
119
|
+
const within: Channel<A, R, THookError>["within"] = (native) => (body) =>
|
|
120
|
+
Effect.uninterruptibleMask((restore) =>
|
|
121
|
+
Effect.flatMap(open, (frame) =>
|
|
122
|
+
Effect.onExit(native(restore(frame.provide(Effect.tap(body, () => frame.beforeCommit)))), (exit) =>
|
|
123
|
+
frame.settle(Exit.isSuccess(exit) ? "committed" : "rolledBack"),
|
|
124
|
+
),
|
|
125
|
+
),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const batch: Channel<A, R, THookError>["batch"] = (body) =>
|
|
129
|
+
Effect.uninterruptibleMask((restore) =>
|
|
130
|
+
Effect.flatMap(openAs("batch"), (frame) => Effect.onExit(restore(frame.provide(body)), () => frame.settle("committed"))),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
return { BeforeCommit, batch, CommittedSink, Observer: CurrentObserver, open, record, Sink: CurrentSink, within };
|
|
134
|
+
}
|
package/src/frame.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
|
|
3
|
+
export type Outcome = "committed" | "rolledBack";
|
|
4
|
+
|
|
5
|
+
export interface Frame {
|
|
6
|
+
readonly provide: <TValue, TError, TRequirements>(
|
|
7
|
+
body: Effect.Effect<TValue, TError, TRequirements>,
|
|
8
|
+
) => Effect.Effect<TValue, TError, TRequirements>;
|
|
9
|
+
readonly settle: (outcome: Outcome) => Effect.Effect<void>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface CommitFrame<E = never> extends Frame {
|
|
13
|
+
readonly beforeCommit: Effect.Effect<void, E>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface Buffer<A> {
|
|
17
|
+
readonly changes: Map<unknown, A>;
|
|
18
|
+
readonly kind: "transaction" | "batch";
|
|
19
|
+
open: boolean;
|
|
20
|
+
readonly parent: Buffer<A> | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function makeBuffer<A>(kind: Buffer<A>["kind"], parent: Buffer<A> | undefined): Buffer<A> {
|
|
24
|
+
return {
|
|
25
|
+
changes: new Map(),
|
|
26
|
+
kind,
|
|
27
|
+
open: true,
|
|
28
|
+
parent,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function settled(name: string): Effect.Effect<never> {
|
|
33
|
+
return Effect.die(new Error(`${name}: changes arrived after their transaction settled; record them before the transaction body returns`));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function add<A>(name: string, buffer: Buffer<A>, entries: Iterable<readonly [unknown, A]>): Effect.Effect<void> {
|
|
37
|
+
return buffer.open
|
|
38
|
+
? Effect.sync(() => {
|
|
39
|
+
for (const [key, change] of entries) {
|
|
40
|
+
if (!buffer.changes.has(key)) {
|
|
41
|
+
buffer.changes.set(key, change);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
: settled(name);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function keyed<A>(changes: Iterable<A>, keyOf: (change: A) => unknown): Map<unknown, A> {
|
|
49
|
+
const entries = new Map<unknown, A>();
|
|
50
|
+
for (const change of changes) {
|
|
51
|
+
const key = keyOf(change);
|
|
52
|
+
if (!entries.has(key)) {
|
|
53
|
+
entries.set(key, change);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return entries;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function transactionParent<A>(buffer: Buffer<A> | undefined): boolean {
|
|
60
|
+
return buffer !== undefined && (buffer.kind === "transaction" || transactionParent(buffer.parent));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function makeFrame<A, E = never>(options: {
|
|
64
|
+
readonly beforeCommit?: (changes: readonly A[]) => Effect.Effect<void, E>;
|
|
65
|
+
readonly name: string;
|
|
66
|
+
readonly buffer: Buffer<A>;
|
|
67
|
+
readonly provide: <TValue, TError, TRequirements>(
|
|
68
|
+
body: Effect.Effect<TValue, TError, TRequirements>,
|
|
69
|
+
) => Effect.Effect<TValue, TError, TRequirements>;
|
|
70
|
+
readonly publish: (changes: readonly A[]) => Effect.Effect<void>;
|
|
71
|
+
readonly discard: (changes: readonly A[]) => Effect.Effect<void>;
|
|
72
|
+
}): CommitFrame<E> {
|
|
73
|
+
const { name, buffer } = options;
|
|
74
|
+
const settle = Effect.fn("Changes.settle")(function* (outcome: Outcome) {
|
|
75
|
+
if (!buffer.open) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
buffer.open = false;
|
|
79
|
+
if (outcome === "committed" && buffer.parent !== undefined) {
|
|
80
|
+
return yield* add(name, buffer.parent, buffer.changes);
|
|
81
|
+
}
|
|
82
|
+
const changes = [...buffer.changes.values()];
|
|
83
|
+
if (changes.length === 0) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
yield* outcome === "committed" ? options.publish(changes) : options.discard(changes);
|
|
87
|
+
}, Effect.uninterruptible);
|
|
88
|
+
const beforeCommit = Effect.suspend(() => {
|
|
89
|
+
if (!buffer.open) {
|
|
90
|
+
return settled(name);
|
|
91
|
+
}
|
|
92
|
+
if (buffer.kind !== "transaction" || transactionParent(buffer.parent) || buffer.changes.size === 0) {
|
|
93
|
+
return Effect.void;
|
|
94
|
+
}
|
|
95
|
+
return options.beforeCommit?.([...buffer.changes.values()]) ?? Effect.void;
|
|
96
|
+
});
|
|
97
|
+
return { beforeCommit, provide: options.provide, settle };
|
|
98
|
+
}
|
package/src/observe.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
|
|
3
|
+
export type Observation<A> =
|
|
4
|
+
| { readonly _tag: "Recorded"; readonly changes: readonly A[] }
|
|
5
|
+
| { readonly _tag: "Published"; readonly changes: readonly A[] }
|
|
6
|
+
| { readonly _tag: "Discarded"; readonly changes: readonly A[] };
|
|
7
|
+
|
|
8
|
+
export type Observer<A> = (observation: Observation<A>) => Effect.Effect<void>;
|
|
9
|
+
|
|
10
|
+
export function unobserved(_observation: unknown): Effect.Effect<void> {
|
|
11
|
+
return Effect.void;
|
|
12
|
+
}
|
package/src/publish.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
|
|
3
|
+
export type PublishFailure = "log" | "die";
|
|
4
|
+
|
|
5
|
+
export type Publish<A, R = never> = (changes: readonly A[]) => Effect.Effect<void, unknown, R>;
|
|
6
|
+
|
|
7
|
+
export function publisher<A, R>(name: string, policy: PublishFailure): (sink: Publish<A, R>, changes: readonly A[]) => Effect.Effect<void, never, R> {
|
|
8
|
+
return Effect.fn("Changes.publish")(function* (sink: Publish<A, R>, changes: readonly A[]) {
|
|
9
|
+
yield* Effect.annotateCurrentSpan({ "changes.channel": name, "changes.count": changes.length });
|
|
10
|
+
const sent = Effect.suspend(() => sink(changes));
|
|
11
|
+
if (policy === "die") {
|
|
12
|
+
return yield* Effect.orDie(sent);
|
|
13
|
+
}
|
|
14
|
+
yield* Effect.catchCause(sent, (cause) =>
|
|
15
|
+
Effect.andThen(
|
|
16
|
+
Effect.annotateCurrentSpan("changes.published", false),
|
|
17
|
+
Effect.logError("Changes.publish failed after the changes were committed; the committed result stands", cause),
|
|
18
|
+
).pipe(Effect.annotateLogs({ changes: changes.length, channel: name })),
|
|
19
|
+
);
|
|
20
|
+
}, Effect.uninterruptible);
|
|
21
|
+
}
|