@gjermundgaraba/effect-actions 0.32.0 → 0.33.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/dist/Stored.d.mts CHANGED
@@ -1,7 +1,21 @@
1
1
  import { Events } from "./contract/hub.mjs";
2
2
  import { Idempotency } from "./contract/idempotency.mjs";
3
- import { Layer } from "effect";
3
+ import { Context, Layer } from "effect";
4
4
  import { SqlClient } from "effect/sql";
5
+ /**
6
+ * What `layerEvents` calls in the fiber that made each change its change signal counts, each
7
+ * append to a log, published or emitted, and each `notify`, once the change is written: a host
8
+ * serving the database's events from outside `Events`, as the Cloudflare package's socket host
9
+ * does, learns of a change as it is made, by work that outlived the call that started it too, not
10
+ * only when a handler ends. `layerEvents` reads it as it builds; by default it does nothing, and
11
+ * the Cloudflare package's `ActionCluster.layer` provides one to each entity's build.
12
+ *
13
+ * **Gotchas**
14
+ *
15
+ * - It is called inside the transaction that wrote the change, before that commits: a listener reading the database on the same connection sees the change uncommitted, and one a transaction rolls back has called it all the same.
16
+ * - It runs synchronously in the writer's fiber, the write waiting on it: hand any work to another fiber, and throw nothing, as a throw is the write's defect.
17
+ */
18
+ export declare const ChangeListener: Context.Reference<() => void>;
5
19
  /**
6
20
  * An `Events` kept in tables through the `SqlClient` it is built with, created as it builds:
7
21
  * each log source's occurrences in `effect_actions_event_log`, by event name and sequence number,
@@ -12,7 +26,7 @@ import { SqlClient } from "effect/sql";
12
26
  * occurrence past it within the log's bounds, `maxAge` (10 minutes by default) and `maxEvents`.
13
27
  * Its change signal, which `changeSignal` reads, moves at each `notify` and each append to a
14
28
  * log, published or emitted, and at no read or restart: a host reads it to learn whether a poll
15
- * could answer anything new.
29
+ * could answer anything new, and `ChangeListener`, read as it builds, is called at each move.
16
30
  *
17
31
  * **Gotchas**
18
32
  *
@@ -28,8 +42,9 @@ export declare const layerEvents: Layer.Layer<Events, never, SqlClient.SqlClient
28
42
  * The SQL query reading `layerEvents`' change signal from its database, one row whose `signal`
29
43
  * column is the sum of `effect_actions_event_revision`'s counts and of
30
44
  * `effect_actions_event_position`'s `last` sequence numbers: it moves at each `notify` and each
31
- * append to a log, and at no read or restart, so a host that runs it after each handler, as the
32
- * Cloudflare package's socket host does, learns whether a parked poll could answer anything new.
45
+ * append to a log, and at no read or restart, so a host that runs it as `ChangeListener` is
46
+ * called and after each handler, as the Cloudflare package's socket host does, learns whether a
47
+ * parked poll could answer anything new.
33
48
  *
34
49
  * **Gotchas**
35
50
  *
package/dist/Stored.mjs CHANGED
@@ -1,11 +1,25 @@
1
1
  import { changeSignalQuery, createLogTables, sqlRevisions, sqlStorage } from "./contract/event-tables.mjs";
2
2
  import { Events, makeEvents } from "./contract/hub.mjs";
3
3
  import { Idempotency, Kept, keptFor } from "./contract/idempotency.mjs";
4
- import { Duration, Effect, Layer, Option, Schema } from "effect";
4
+ import { Context, Duration, Effect, Layer, Option, Schema } from "effect";
5
5
  import { Persistence } from "effect/persistence";
6
6
  import { SqlClient } from "effect/sql";
7
7
  // src/contract/Stored.ts
8
8
  /**
9
+ * What `layerEvents` calls in the fiber that made each change its change signal counts, each
10
+ * append to a log, published or emitted, and each `notify`, once the change is written: a host
11
+ * serving the database's events from outside `Events`, as the Cloudflare package's socket host
12
+ * does, learns of a change as it is made, by work that outlived the call that started it too, not
13
+ * only when a handler ends. `layerEvents` reads it as it builds; by default it does nothing, and
14
+ * the Cloudflare package's `ActionCluster.layer` provides one to each entity's build.
15
+ *
16
+ * **Gotchas**
17
+ *
18
+ * - It is called inside the transaction that wrote the change, before that commits: a listener reading the database on the same connection sees the change uncommitted, and one a transaction rolls back has called it all the same.
19
+ * - It runs synchronously in the writer's fiber, the write waiting on it: hand any work to another fiber, and throw nothing, as a throw is the write's defect.
20
+ */
21
+ const ChangeListener = Context.Reference("effect-actions/Stored/ChangeListener", { defaultValue: () => () => void 0 });
22
+ /**
9
23
  * An `Events` kept in tables through the `SqlClient` it is built with, created as it builds:
10
24
  * each log source's occurrences in `effect_actions_event_log`, by event name and sequence number,
11
25
  * with its key and the occurrence as JSON, and its position in `effect_actions_event_position`;
@@ -15,7 +29,7 @@ import { SqlClient } from "effect/sql";
15
29
  * occurrence past it within the log's bounds, `maxAge` (10 minutes by default) and `maxEvents`.
16
30
  * Its change signal, which `changeSignal` reads, moves at each `notify` and each append to a
17
31
  * log, published or emitted, and at no read or restart: a host reads it to learn whether a poll
18
- * could answer anything new.
32
+ * could answer anything new, and `ChangeListener`, read as it builds, is called at each move.
19
33
  *
20
34
  * **Gotchas**
21
35
  *
@@ -29,18 +43,27 @@ import { SqlClient } from "effect/sql";
29
43
  const layerEvents = Layer.effect(Events)(Effect.gen(function* () {
30
44
  const sql = (yield* SqlClient.SqlClient).withoutTransforms();
31
45
  yield* createLogTables(sql);
32
- const revisions = yield* sqlRevisions(sql);
46
+ const counted = yield* sqlRevisions(sql);
47
+ const changed = yield* ChangeListener;
33
48
  const named = /* @__PURE__ */ new Map();
49
+ const revisions = {
50
+ ...counted,
51
+ bump: (names) => Effect.tap(counted.bump(names), () => Effect.sync(changed))
52
+ };
34
53
  return yield* makeEvents({
35
54
  log: (event) => {
36
55
  const prior = named.get(event.name);
37
56
  if (prior !== void 0 && prior !== event) throw new Error(`Stored.layerEvents keeps each log source's log by its event's name, and two event types are named "${event.name}" (give each event type a name of its own)`);
38
57
  named.set(event.name, event);
39
- return sqlStorage(sql, event.name, Schema.fromJsonString(Schema.toCodecJson(Schema.Struct({
58
+ const storage = sqlStorage(sql, event.name, Schema.fromJsonString(Schema.toCodecJson(Schema.Struct({
40
59
  data: event.payload,
41
60
  eventId: Schema.optional(Schema.String),
42
61
  timestamp: Schema.optional(Schema.DateTimeUtcFromMillis)
43
62
  }))));
63
+ return {
64
+ ...storage,
65
+ put: (position, entry) => Effect.tap(storage.put(position, entry), () => entry === void 0 ? Effect.void : Effect.sync(changed))
66
+ };
44
67
  },
45
68
  revisions
46
69
  });
@@ -49,8 +72,9 @@ const layerEvents = Layer.effect(Events)(Effect.gen(function* () {
49
72
  * The SQL query reading `layerEvents`' change signal from its database, one row whose `signal`
50
73
  * column is the sum of `effect_actions_event_revision`'s counts and of
51
74
  * `effect_actions_event_position`'s `last` sequence numbers: it moves at each `notify` and each
52
- * append to a log, and at no read or restart, so a host that runs it after each handler, as the
53
- * Cloudflare package's socket host does, learns whether a parked poll could answer anything new.
75
+ * append to a log, and at no read or restart, so a host that runs it as `ChangeListener` is
76
+ * called and after each handler, as the Cloudflare package's socket host does, learns whether a
77
+ * parked poll could answer anything new.
54
78
  *
55
79
  * **Gotchas**
56
80
  *
@@ -75,4 +99,4 @@ const layerIdempotency = Layer.effect(Idempotency)(Effect.gen(function* () {
75
99
  set: (id, kept) => Effect.orDie(store.set(id, kept, Duration.millis(keptFor)))
76
100
  });
77
101
  })).pipe(Layer.provide(Persistence.layerBackingSqlMultiTable));
78
- export { changeSignal, layerEvents, layerIdempotency };
102
+ export { ChangeListener, changeSignal, layerEvents, layerIdempotency };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjermundgaraba/effect-actions",
3
- "version": "0.32.0",
3
+ "version": "0.33.0",
4
4
  "description": "Define Effect action contracts once and project them onto HTTP, MCP, Effect RPC, native Toolkits, and CLIs.",
5
5
  "keywords": [
6
6
  "ai-agents",