@shivaedev/contract 0.41.0 → 0.43.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.
Files changed (169) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +28 -178
  3. package/dist/AutomaticQueryOptions.d.ts +0 -5
  4. package/dist/AutomaticQueryOptions.d.ts.map +1 -1
  5. package/dist/HostVersion.d.ts +1 -19
  6. package/dist/HostVersion.d.ts.map +1 -1
  7. package/dist/adapters/activityState.d.ts.map +1 -1
  8. package/dist/adapters/activityState.js +0 -1
  9. package/dist/adapters/activityState.js.map +1 -1
  10. package/dist/adapters/liveHintStream.d.ts +0 -1
  11. package/dist/adapters/liveHintStream.d.ts.map +1 -1
  12. package/dist/adapters/openLiveHints.d.ts +0 -1
  13. package/dist/adapters/openLiveHints.d.ts.map +1 -1
  14. package/dist/adapters/retainedQueries.d.ts +7 -0
  15. package/dist/adapters/retainedQueries.d.ts.map +1 -0
  16. package/dist/adapters/retainedQueries.js +45 -0
  17. package/dist/adapters/retainedQueries.js.map +1 -0
  18. package/dist/adapters/standaloneLiveServer.d.ts +1 -7
  19. package/dist/adapters/standaloneLiveServer.d.ts.map +1 -1
  20. package/dist/adapters/standaloneLiveServer.js.map +1 -1
  21. package/dist/automaticLive.d.ts +2 -4
  22. package/dist/automaticLive.d.ts.map +1 -1
  23. package/dist/automaticLive.js +5 -14
  24. package/dist/automaticLive.js.map +1 -1
  25. package/dist/automaticQuery.d.ts +1 -1
  26. package/dist/automaticQuery.d.ts.map +1 -1
  27. package/dist/automaticQuery.js +20 -26
  28. package/dist/automaticQuery.js.map +1 -1
  29. package/dist/bind.d.ts +1 -2
  30. package/dist/bind.d.ts.map +1 -1
  31. package/dist/bind.js +8 -12
  32. package/dist/bind.js.map +1 -1
  33. package/dist/bindOperations.d.ts +6 -0
  34. package/dist/bindOperations.d.ts.map +1 -0
  35. package/dist/bindOperations.js +10 -0
  36. package/dist/bindOperations.js.map +1 -0
  37. package/dist/changesSuccess.d.ts +2 -3
  38. package/dist/changesSuccess.d.ts.map +1 -1
  39. package/dist/changesSuccess.js +1 -3
  40. package/dist/changesSuccess.js.map +1 -1
  41. package/dist/clientActivities.d.ts +0 -2
  42. package/dist/clientActivities.d.ts.map +1 -1
  43. package/dist/clientActivities.js +0 -4
  44. package/dist/clientActivities.js.map +1 -1
  45. package/dist/clientActivity.d.ts +0 -1
  46. package/dist/clientActivity.d.ts.map +1 -1
  47. package/dist/clientActivity.js +1 -1
  48. package/dist/clientActivity.js.map +1 -1
  49. package/dist/clientOperations.d.ts +1 -1
  50. package/dist/clientOperations.d.ts.map +1 -1
  51. package/dist/clientOperations.js +53 -52
  52. package/dist/clientOperations.js.map +1 -1
  53. package/dist/compareQueries.d.ts +2 -19
  54. package/dist/compareQueries.d.ts.map +1 -1
  55. package/dist/compareQueries.js +6 -33
  56. package/dist/compareQueries.js.map +1 -1
  57. package/dist/live-server/register.d.ts +0 -1
  58. package/dist/live-server/register.d.ts.map +1 -1
  59. package/dist/live-server/service.d.ts +0 -2
  60. package/dist/live-server/service.d.ts.map +1 -1
  61. package/dist/live-server/stream.d.ts +0 -1
  62. package/dist/live-server/stream.d.ts.map +1 -1
  63. package/dist/live-server/subscribe.d.ts +2 -2
  64. package/dist/live-server/subscribe.d.ts.map +1 -1
  65. package/dist/live-server/subscribe.js.map +1 -1
  66. package/dist/live.d.ts +5 -12
  67. package/dist/live.d.ts.map +1 -1
  68. package/dist/live.js +15 -85
  69. package/dist/live.js.map +1 -1
  70. package/dist/liveAttempt.d.ts +3 -6
  71. package/dist/liveAttempt.d.ts.map +1 -1
  72. package/dist/liveAttempt.js +11 -30
  73. package/dist/liveAttempt.js.map +1 -1
  74. package/dist/liveHintQueue.d.ts +0 -1
  75. package/dist/liveHintQueue.d.ts.map +1 -1
  76. package/dist/liveHintQueue.js +9 -12
  77. package/dist/liveHintQueue.js.map +1 -1
  78. package/dist/makeLiveServer.d.ts +1 -5
  79. package/dist/makeLiveServer.d.ts.map +1 -1
  80. package/dist/operation.d.ts +2 -4
  81. package/dist/operation.d.ts.map +1 -1
  82. package/dist/operation.js +0 -1
  83. package/dist/operation.js.map +1 -1
  84. package/dist/operationDeclaration.d.ts +1 -2
  85. package/dist/operationDeclaration.d.ts.map +1 -1
  86. package/dist/operationDeclaration.js.map +1 -1
  87. package/dist/publicConfig.d.ts +6 -21
  88. package/dist/publicConfig.d.ts.map +1 -1
  89. package/dist/queryDelivery.d.ts +4 -5
  90. package/dist/queryDelivery.d.ts.map +1 -1
  91. package/dist/queryDelivery.js +4 -15
  92. package/dist/queryDelivery.js.map +1 -1
  93. package/dist/queryProgress.d.ts.map +1 -1
  94. package/dist/queryProgress.js +0 -3
  95. package/dist/queryProgress.js.map +1 -1
  96. package/dist/queryRetention.d.ts +8 -0
  97. package/dist/queryRetention.d.ts.map +1 -0
  98. package/dist/queryRetention.js +3 -0
  99. package/dist/queryRetention.js.map +1 -0
  100. package/dist/querySignals.d.ts +1 -0
  101. package/dist/querySignals.d.ts.map +1 -1
  102. package/dist/querySignals.js.map +1 -1
  103. package/dist/querySubscription.d.ts +1 -1
  104. package/dist/querySubscription.d.ts.map +1 -1
  105. package/dist/queryUpdate.d.ts +15 -4
  106. package/dist/queryUpdate.d.ts.map +1 -1
  107. package/dist/queryUpdate.js +7 -1
  108. package/dist/queryUpdate.js.map +1 -1
  109. package/dist/retainQuery.d.ts +4 -0
  110. package/dist/retainQuery.d.ts.map +1 -0
  111. package/dist/retainQuery.js +35 -0
  112. package/dist/retainQuery.js.map +1 -0
  113. package/package.json +8 -8
  114. package/src/AutomaticQueryOptions.ts +0 -5
  115. package/src/adapters/activityState.ts +0 -1
  116. package/src/adapters/retainedQueries.ts +47 -0
  117. package/src/adapters/standaloneLiveServer.ts +1 -5
  118. package/src/automaticLive.ts +4 -16
  119. package/src/automaticQuery.ts +28 -42
  120. package/src/bind.ts +16 -22
  121. package/src/bindOperations.ts +20 -0
  122. package/src/changesSuccess.ts +3 -5
  123. package/src/clientActivities.ts +1 -5
  124. package/src/clientActivity.ts +1 -2
  125. package/src/clientOperations.ts +77 -85
  126. package/src/compareQueries.ts +12 -38
  127. package/src/live-server/subscribe.ts +5 -9
  128. package/src/live.ts +19 -97
  129. package/src/liveAttempt.ts +17 -41
  130. package/src/liveHintQueue.ts +9 -12
  131. package/src/operation.ts +3 -17
  132. package/src/operationDeclaration.ts +0 -2
  133. package/src/queryDelivery.ts +6 -26
  134. package/src/queryProgress.ts +0 -3
  135. package/src/queryRetention.ts +9 -0
  136. package/src/querySignals.ts +1 -0
  137. package/src/querySubscription.ts +1 -1
  138. package/src/queryUpdate.ts +10 -4
  139. package/src/retainQuery.ts +36 -0
  140. package/dist/liveInvalidation.d.ts +0 -12
  141. package/dist/liveInvalidation.d.ts.map +0 -1
  142. package/dist/liveInvalidation.js +0 -14
  143. package/dist/liveInvalidation.js.map +0 -1
  144. package/dist/livePending.d.ts +0 -7
  145. package/dist/livePending.d.ts.map +0 -1
  146. package/dist/livePending.js +0 -27
  147. package/dist/livePending.js.map +0 -1
  148. package/dist/queryCoalesceWindow.d.ts +0 -4
  149. package/dist/queryCoalesceWindow.d.ts.map +0 -1
  150. package/dist/queryCoalesceWindow.js +0 -11
  151. package/dist/queryCoalesceWindow.js.map +0 -1
  152. package/dist/queryFailure.d.ts +0 -4
  153. package/dist/queryFailure.d.ts.map +0 -1
  154. package/dist/queryFailure.js +0 -12
  155. package/dist/queryFailure.js.map +0 -1
  156. package/dist/queryHints.d.ts +0 -9
  157. package/dist/queryHints.d.ts.map +0 -1
  158. package/dist/queryHints.js +0 -34
  159. package/dist/queryHints.js.map +0 -1
  160. package/dist/queryReactivity.d.ts +0 -5
  161. package/dist/queryReactivity.d.ts.map +0 -1
  162. package/dist/queryReactivity.js +0 -109
  163. package/dist/queryReactivity.js.map +0 -1
  164. package/src/liveInvalidation.ts +0 -21
  165. package/src/livePending.ts +0 -28
  166. package/src/queryCoalesceWindow.ts +0 -13
  167. package/src/queryFailure.ts +0 -13
  168. package/src/queryHints.ts +0 -39
  169. package/src/queryReactivity.ts +0 -127
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/contract",
3
- "version": "0.41.0",
3
+ "version": "0.43.0",
4
4
  "description": "Typed query and command contracts over native Effect RPC and reactivity",
5
5
  "keywords": [
6
6
  "effect"
@@ -51,13 +51,13 @@
51
51
  "@effect/platform-node": "4.1.0",
52
52
  "@effect/vitest": "4.1.0",
53
53
  "@shivaedev/errors": "0.10.0",
54
- "@shivaedev/identity": "0.29.0",
55
- "@shivaedev/quality": "0.67.0",
56
- "@shivaedev/server": "0.29.0",
57
- "@shivaedev/sql": "0.44.0",
58
- "@shivaedev/test-kits": "0.9.0",
59
- "@shivaedev/test-sql": "0.24.2",
60
- "@shivaedev/test-story": "0.43.0",
54
+ "@shivaedev/identity": "0.30.0",
55
+ "@shivaedev/quality": "0.69.3",
56
+ "@shivaedev/server": "0.31.0",
57
+ "@shivaedev/sql": "0.45.0",
58
+ "@shivaedev/test-kits": "0.10.0",
59
+ "@shivaedev/test-sql": "0.24.3",
60
+ "@shivaedev/test-story": "0.45.0",
61
61
  "@types/node": "24.19.2",
62
62
  "effect": "4.1.0",
63
63
  "esbuild": "0.28.2",
@@ -1,4 +1,3 @@
1
- import type { Duration } from "effect";
2
1
  import type * as AsyncResult from "effect/reactivity/AsyncResult";
3
2
  import type * as Atom from "effect/reactivity/Atom";
4
3
  import type { LiveBound, LiveConnection } from "#automaticLive.ts";
@@ -6,14 +5,10 @@ import type { LiveStatus } from "#live.ts";
6
5
  import type { QuerySignals } from "#querySignals.ts";
7
6
 
8
7
  export interface AutomaticQueryOptions<A, E> {
9
- readonly atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>;
10
8
  readonly clientStatuses: Atom.Writable<ReadonlyMap<object, LiveStatus<unknown>>> | undefined;
11
- readonly coalesceWindow: Atom.Writable<Duration.Input>;
12
9
  readonly equivalent: (left: A, right: A) => boolean;
13
10
  readonly paused: Atom.Writable<boolean>;
14
- readonly pushed: boolean;
15
11
  readonly ready: Atom.Writable<boolean>;
16
- readonly resultVersion: Atom.Atom<number>;
17
12
  readonly signals: Atom.Writable<QuerySignals>;
18
13
  readonly source: Atom.Atom<AsyncResult.AsyncResult<A, E>>;
19
14
  readonly statuses: Atom.Writable<ReadonlyMap<object, LiveStatus<unknown>>>;
@@ -10,7 +10,6 @@ export function activityState() {
10
10
  connectionStatus: "live",
11
11
  initialQueries: 0,
12
12
  liveHints: 0,
13
- liveInvalidations: 0,
14
13
  refreshingAtoms: 0,
15
14
  rpcCalls: 0,
16
15
  }),
@@ -0,0 +1,47 @@
1
+ import { Duration, Effect, Option } from "effect";
2
+ import * as AsyncResult from "effect/reactivity/AsyncResult";
3
+ import * as Atom from "effect/reactivity/Atom";
4
+ import { queryRetention } from "#queryRetention.ts";
5
+
6
+ export const retainedQueries = Atom.keepAlive(
7
+ Atom.make((get) => {
8
+ const idle = new Map<Atom.Atom<unknown>, { readonly release: () => void; readonly expire: () => void }>();
9
+ get.addFinalizer(() => idle.clear());
10
+ return {
11
+ keep: <A>(query: Atom.Atom<unknown>, snapshot: Atom.Writable<Option.Option<A>>, runtime: Atom.AtomRuntime<unknown, unknown>) => {
12
+ const { capacity = 100, timeToLive = "5 minutes" } = get.registry.get(queryRetention);
13
+ const duration = Duration.fromInputUnsafe(timeToLive);
14
+ if (capacity <= 0 || Duration.toMillis(duration) <= 0) {
15
+ get.registry.set(snapshot, Option.none());
16
+ return;
17
+ }
18
+ const registry = get.registry;
19
+ const unmount = registry.mount(snapshot);
20
+ function release() {
21
+ idle.delete(query);
22
+ stop();
23
+ unmount();
24
+ }
25
+ function expire() {
26
+ registry.set(snapshot, Option.none());
27
+ release();
28
+ }
29
+ idle.set(query, { expire, release });
30
+ const timer = Atom.make((context) => {
31
+ const services = context(runtime);
32
+ if (AsyncResult.isSuccess(services)) {
33
+ context.addFinalizer(Effect.runCallbackWith(services.value)(Effect.sleep(duration).pipe(Effect.andThen(Effect.sync(expire)))));
34
+ }
35
+ }).pipe(Atom.setIdleTTL(0));
36
+ const stop = registry.mount(timer);
37
+ while (idle.size > capacity) {
38
+ const oldest = idle.keys().next().value;
39
+ if (oldest !== undefined) {
40
+ idle.get(oldest)?.expire();
41
+ }
42
+ }
43
+ },
44
+ take: (atom: Atom.Atom<unknown>) => idle.get(atom)?.release(),
45
+ };
46
+ }),
47
+ );
@@ -4,15 +4,11 @@ import { liveServerLayer } from "#live-server/layer.ts";
4
4
  import { LiveServer } from "#live-server/service.ts";
5
5
  import { subscribe as subscribeQueries } from "#live-server/subscribe.ts";
6
6
  import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
7
- import type { QueryUpdate, SubscriptionHint } from "#queryUpdate.ts";
8
- import type { QueryReadDenied } from "#readPolicyErrors.ts";
9
7
 
10
8
  export const standaloneLiveServer = Effect.gen(function* () {
11
9
  const context = yield* Effect.scoped(Layer.build(liveServerLayer));
12
10
  const server = Context.get(context, LiveServer);
13
- function subscribe<E, R>(subscription: QuerySubscription<E, R>): Stream.Stream<SubscriptionHint, E | QueryReadDenied, R>;
14
- function subscribe<E, R>(subscription: QuerySubscription<E, R>, read: QueryRerun<E, R>): Stream.Stream<QueryUpdate, E | QueryReadDenied, R>;
15
- function subscribe<E, R>(subscription: QuerySubscription<E, R>, read?: QueryRerun<E, R>) {
11
+ function subscribe<E, R>(subscription: QuerySubscription<E, R>, read: QueryRerun<E, R>) {
16
12
  return Stream.unwrap(Effect.provideContext(subscribeQueries(subscription, read), context));
17
13
  }
18
14
  return { resync: server.resync(), stream: (keys: readonly Key[]) => Stream.unwrap(server.stream(keys)), subscribe };
@@ -1,4 +1,4 @@
1
- import { type Duration, Effect, type Schema, Stream } from "effect";
1
+ import { Effect, type Schema, Stream } from "effect";
2
2
  import type { Headers } from "effect/http";
3
3
  import type * as AsyncResult from "effect/reactivity/AsyncResult";
4
4
  import * as Atom from "effect/reactivity/Atom";
@@ -11,7 +11,6 @@ import { automaticQuery } from "#automaticQuery.ts";
11
11
  import { clientActivities } from "#clientActivities.ts";
12
12
  import { type LiveStatus, live } from "#live.ts";
13
13
  import { queryDelivery } from "#queryDelivery.ts";
14
- import { localQueryKeys, queryHint } from "#queryHints.ts";
15
14
  import { queryProgress } from "#queryProgress.ts";
16
15
  import { querySignals, shareQuerySignals } from "#querySignals.ts";
17
16
  import type { QueryUpdate } from "#queryUpdate.ts";
@@ -24,7 +23,6 @@ declare const window: ResumeWindow | undefined;
24
23
  export const LiveConnection = Symbol.for("@shivaedev/contract/LiveConnection");
25
24
  export interface LiveBound {
26
25
  readonly [LiveConnection]: {
27
- readonly coalesceWindow: Atom.Writable<Duration.Input>;
28
26
  readonly connection: Atom.Atom<unknown>;
29
27
  readonly identity?: Atom.Writable<string | undefined>;
30
28
  readonly paused: Atom.Writable<boolean>;
@@ -58,7 +56,6 @@ export function automaticLive(
58
56
  throw new Error("A session-scoped query belongs to its binding registry");
59
57
  }
60
58
  }
61
- const coalesceWindow = Atom.keepAlive(Atom.make<Duration.Input>("20 millis"));
62
59
  const paused = pausedByRuntime(service.runtime);
63
60
  const statuses = Atom.make<ReadonlyMap<object, LiveStatus<unknown>>>(new Map());
64
61
  const status = Atom.map(statuses, (current) => aggregateLiveStatus([...current.values()]));
@@ -75,7 +72,6 @@ export function automaticLive(
75
72
  keys: readonly Key[],
76
73
  equivalent: (left: A, right: A) => boolean,
77
74
  decode: (value: unknown) => A,
78
- compared: boolean,
79
75
  reactive: (source: Atom.Atom<AsyncResult.AsyncResult<A, E>>) => Atom.Atom<AsyncResult.AsyncResult<A, E>>,
80
76
  headers?: Headers.Input,
81
77
  ) {
@@ -84,30 +80,22 @@ export function automaticLive(
84
80
  }
85
81
  const ready = Atom.make(false);
86
82
  const signals = querySignals(atom);
87
- const delivery = queryDelivery(atom, decode, compared, paused, signals);
88
- const source = compared ? delivery.atom : reactive(delivery.atom.pipe(Atom.setIdleTTL(0)));
83
+ const delivery = queryDelivery(decode, paused, signals);
84
+ const source = delivery.atom;
89
85
  const subscription = live(service.runtime, {
90
- initialResync: false,
91
86
  keyContext: owner?.context,
92
- reopenOnResume: compared,
93
87
  resume: signal,
94
- resyncKeys: compared ? [] : localQueryKeys(keys),
95
88
  retryKeys: keys,
96
89
  stream: Stream.unwrap(service.use((client) => Effect.succeed(client(`${name}.$changes`, { queries: [reference] }, { headers })))).pipe(
97
90
  delivery.stream,
98
- Stream.map((hint) => queryHint(hint, [{ keys }])),
99
91
  ),
100
92
  });
101
93
  const gated = automaticQuery({
102
94
  assertOwner,
103
- atom,
104
95
  clientStatuses: clientActivities.get(service)?.statuses,
105
- coalesceWindow,
106
96
  equivalent,
107
97
  paused,
108
- pushed: compared,
109
98
  ready,
110
- resultVersion: delivery.version,
111
99
  signals,
112
100
  source,
113
101
  statuses,
@@ -121,5 +109,5 @@ export function automaticLive(
121
109
  }
122
110
  return gated;
123
111
  }
124
- return { query, subscription: { coalesceWindow, connection, identity: socketIdentity, paused, status } };
112
+ return { query, subscription: { connection, identity: socketIdentity, paused, status } };
125
113
  }
@@ -3,8 +3,6 @@ import * as AsyncResult from "effect/reactivity/AsyncResult";
3
3
  import * as Atom from "effect/reactivity/Atom";
4
4
  import type { AutomaticQueryOptions } from "#AutomaticQueryOptions.ts";
5
5
  import type { LiveStatus } from "#live.ts";
6
- import { queryCoalesceWindow } from "#queryCoalesceWindow.ts";
7
- import { queryFailure } from "#queryFailure.ts";
8
6
  import { recordCompletion, recordSubscription } from "#queryProgress.ts";
9
7
  import { queryResult } from "#queryResult.ts";
10
8
 
@@ -12,15 +10,11 @@ interface GateOptions<A, E> extends AutomaticQueryOptions<A, E> {
12
10
  readonly assertOwner: (get: Atom.AtomContext) => void;
13
11
  }
14
12
  export function automaticQuery<A, E>({
15
- atom,
16
13
  assertOwner,
17
- coalesceWindow,
18
14
  clientStatuses,
19
15
  equivalent,
20
16
  paused,
21
- pushed,
22
17
  ready,
23
- resultVersion,
24
18
  signals,
25
19
  source,
26
20
  statuses,
@@ -31,7 +25,6 @@ export function automaticQuery<A, E>({
31
25
  (get) => {
32
26
  assertOwner(get);
33
27
  get.mount(signals);
34
- get.mount(resultVersion);
35
28
  get.mount(subscriptionVersion);
36
29
  const openingVersion = get.once(subscriptionVersion);
37
30
  get.refresh(subscription.connection);
@@ -42,8 +35,6 @@ export function automaticQuery<A, E>({
42
35
  get.addFinalizer(() => stopSource?.());
43
36
  get.set(ready, false);
44
37
  get.set(signals, { ...get.once(signals), pending: true, subscription: "opening" });
45
- get.once(queryCoalesceWindow(gated));
46
- get.subscribe(queryCoalesceWindow(gated), (value) => get.set(queryCoalesceWindow(source), value), { immediate: true });
47
38
  const token = {};
48
39
  let active = true;
49
40
  function observe(value: LiveStatus<unknown>) {
@@ -73,17 +64,10 @@ export function automaticQuery<A, E>({
73
64
  }
74
65
  });
75
66
  get.addFinalizer(registry.subscribe(subscription.status, observe, { immediate: true }));
76
- get.addFinalizer(registry.subscribe(coalesceWindow, (value) => registry.set(subscription.coalesceWindow, value), { immediate: true }));
77
- let started = false;
78
- let deniedVersion = 0;
79
67
  function start() {
80
68
  if (stopSource !== undefined) {
81
69
  return;
82
70
  }
83
- if (!pushed && get.once(resultVersion) === deniedVersion) {
84
- get.refresh(source);
85
- }
86
- started = true;
87
71
  const publish = queryResult<A, E>(equivalent, (value) => {
88
72
  if (!denied) {
89
73
  get.setSelf(value);
@@ -96,37 +80,41 @@ export function automaticQuery<A, E>({
96
80
  get.subscribe(ready, (value) => {
97
81
  if (value) {
98
82
  start();
83
+ } else {
84
+ const previous = get.self<AsyncResult.AsyncResult<A, E>>();
85
+ if (Option.isSome(previous)) {
86
+ const waiting = AsyncResult.waiting(previous.value);
87
+ get.setSelf(waiting);
88
+ get.set(signals, recordCompletion(get.once(signals), waiting));
89
+ }
99
90
  }
100
91
  });
101
- const failed = queryFailure(
102
- () => started && !pushed,
103
- (cause) => {
104
- if (Option.exists(Cause.findErrorOption(cause), Predicate.isTagged("QueryReadDenied"))) {
105
- denied = true;
106
- deniedVersion = get.once(resultVersion);
107
- registry.set(ready, false);
108
- stopSource?.();
109
- stopSource = undefined;
110
- }
111
- const result = AsyncResult.failure(cause);
112
- get.setSelf(result);
113
- get.set(signals, recordCompletion(get.once(signals), result));
114
- },
115
- );
116
- get.subscribe(subscription.status, failed);
92
+ get.subscribe(subscription.status, ({ connection: state, failure }) => {
93
+ if (state !== "failed" || Option.isNone(failure)) {
94
+ return;
95
+ }
96
+ const cause = failure.value;
97
+ if (Option.exists(Cause.findErrorOption(cause), Predicate.isTagged("QueryReadDenied"))) {
98
+ denied = true;
99
+ registry.set(ready, false);
100
+ stopSource?.();
101
+ stopSource = undefined;
102
+ }
103
+ const result = AsyncResult.failure(cause);
104
+ get.setSelf(result);
105
+ get.set(signals, recordCompletion(get.once(signals), result));
106
+ });
117
107
  get.addFinalizer(registry.subscribe(paused, (value) => registry.set(subscription.paused, value), { immediate: true }));
118
108
  function resume() {
119
109
  if (get.once(subscription.status).connection !== "failed") {
120
110
  get.refresh(subscription.connection);
121
111
  }
122
112
  }
123
- if (pushed) {
124
- get.subscribe(paused, (value) => {
125
- if (!value) {
126
- resume();
127
- }
128
- });
129
- }
113
+ get.subscribe(paused, (value) => {
114
+ if (!value) {
115
+ resume();
116
+ }
117
+ });
130
118
  get.mount(subscription.connection);
131
119
  if (get.once(ready)) {
132
120
  start();
@@ -135,9 +123,7 @@ export function automaticQuery<A, E>({
135
123
  return AsyncResult.initial();
136
124
  },
137
125
  (refresh) => {
138
- if (!pushed) {
139
- refresh(atom);
140
- }
126
+ refresh(ready);
141
127
  refresh(subscription.connection);
142
128
  },
143
129
  ).pipe(Atom.setIdleTTL(0));
package/src/bind.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Duration, Effect, Layer, Stream } from "effect";
1
+ import type { Effect, Layer, Stream } from "effect";
2
2
  import type { Headers } from "effect/http";
3
3
  import type * as AsyncResult from "effect/reactivity/AsyncResult";
4
4
  import type * as Atom from "effect/reactivity/Atom";
@@ -8,7 +8,8 @@ import type * as Reactivity from "effect/reactivity/Reactivity";
8
8
  import type { Rpc, RpcGroup } from "effect/rpc";
9
9
  import type { RpcClientError } from "effect/rpc/RpcClientError";
10
10
  import { bindingOwner } from "#adapters/bindingOwner.ts";
11
- import { automaticLive, type LiveBound, LiveConnection } from "#automaticLive.ts";
11
+ import { automaticLive, type LiveBound } from "#automaticLive.ts";
12
+ import { bindOperations } from "#bindOperations.ts";
12
13
  import { clientOperations } from "#clientOperations.ts";
13
14
  import type { ClientOptions, MadeClient } from "#clientOptions.ts";
14
15
  import type { Contract, Declared } from "#contract.ts";
@@ -27,7 +28,6 @@ export type RunFailure<R extends Rpc.Any> = Failure<R>;
27
28
  export interface QueryOptions {
28
29
  readonly headers?: Headers.Input;
29
30
  readonly serializationKey?: string;
30
- readonly timeToLive?: Duration.Input;
31
31
  }
32
32
 
33
33
  export interface BoundQuery<R extends Rpc.Any, TSelf, TSubscription extends Rpc.Any = never> {
@@ -95,26 +95,10 @@ export function bind(
95
95
  service: AtomRpc.AtomRpcClient<unknown, string, Rpc.Any>,
96
96
  options?: BindOptions,
97
97
  ): unknown {
98
- return bindApi(contract, service, options);
99
- }
100
-
101
- function bindApi(
102
- contract: { readonly declaration: Declared<string, readonly QueryShape[], readonly CommandShape[], readonly StreamShape[], "session" | undefined> },
103
- service: AtomRpc.AtomRpcClient<unknown, string, Rpc.Any>,
104
- options?: BindOptions,
105
- ) {
106
- const { name, queries, commands, streams, keyScope } = contract.declaration;
98
+ const { name, keyScope } = contract.declaration;
107
99
  const owner = bindingOwner(keyScope, options?.registry);
108
100
  const automatic = automaticLive(service, name, owner, options?.resume, options?.live, options?.resumeWindow);
109
- const operations = clientOperations(service, automatic, owner);
110
- return Object.assign(
111
- Object.fromEntries([
112
- ...queries.map((query) => [query.name, operations.query(`${name}.${query.name}`, query)] as const),
113
- ...commands.map((command) => [command.name, operations.command(`${name}.${command.name}`, command)] as const),
114
- ...streams.map((stream) => [stream.name, operations.stream(`${name}.${stream.name}`)] as const),
115
- ]),
116
- { [LiveConnection]: automatic.subscription },
117
- );
101
+ return bindOperations(contract.declaration, clientOperations(service, automatic, owner), automatic.subscription);
118
102
  }
119
103
 
120
104
  export function makeClient<
@@ -141,6 +125,16 @@ export function makeClient(
141
125
  },
142
126
  ): Layer.Layer<never> {
143
127
  const connection = rpcClient(contract.declaration.name, options.group ?? contract, options);
144
- const api = bindApi(contract, connection.Client, options);
128
+ const bindingOptions: BindOptions = options;
129
+ const owner = bindingOwner(contract.declaration.keyScope, bindingOptions.registry);
130
+ const automatic = automaticLive(
131
+ connection.Client,
132
+ contract.declaration.name,
133
+ owner,
134
+ bindingOptions.resume,
135
+ bindingOptions.live,
136
+ bindingOptions.resumeWindow,
137
+ );
138
+ const api = bindOperations(contract.declaration, clientOperations(connection.Client, automatic, owner), automatic.subscription);
145
139
  return Object.assign(connection, { api });
146
140
  }
@@ -0,0 +1,20 @@
1
+ import { type LiveBound, LiveConnection } from "#automaticLive.ts";
2
+ import type { clientOperations } from "#clientOperations.ts";
3
+ import type { Declared } from "#contract.ts";
4
+ import type { CommandShape, QueryShape, StreamShape } from "#operation.ts";
5
+
6
+ export function bindOperations(
7
+ declaration: Declared<string, readonly QueryShape[], readonly CommandShape[], readonly StreamShape[], "session" | undefined>,
8
+ operations: ReturnType<typeof clientOperations>,
9
+ subscription: LiveBound[typeof LiveConnection],
10
+ ) {
11
+ const { name, queries, commands, streams } = declaration;
12
+ return Object.assign(
13
+ Object.fromEntries([
14
+ ...queries.map((query) => [query.name, operations.query(`${name}.${query.name}`, query)] as const),
15
+ ...commands.map((command) => [command.name, operations.command(`${name}.${command.name}`, command)] as const),
16
+ ...streams.map((stream) => [stream.name, operations.stream(`${name}.${stream.name}`)] as const),
17
+ ]),
18
+ { [LiveConnection]: subscription },
19
+ );
20
+ }
@@ -1,5 +1,4 @@
1
1
  import { Schema } from "effect";
2
- import { LiveHint } from "#live.ts";
3
2
  import type { QueryShape } from "#operation.ts";
4
3
  import { QueryReadDenied } from "#readPolicyErrors.ts";
5
4
 
@@ -13,18 +12,17 @@ type ResultSchema<TQuery extends QueryShape> = TQuery extends QueryShape
13
12
  queryIndex: Schema.Number;
14
13
  value: TQuery["success"];
15
14
  }>
16
- | ("rerun-and-compare" extends TQuery["live"] ? typeof QueryUnchanged : never)
15
+ | typeof QueryUnchanged
17
16
  : never;
18
17
 
19
18
  export function changesSuccess<const TQueries extends readonly QueryShape[]>(
20
19
  queries: TQueries,
21
- ): Schema.Union<readonly [typeof LiveHint, typeof QueryReadDenied, ...ResultSchema<TQueries[number]>[]]>;
20
+ ): Schema.Union<readonly [typeof QueryReadDenied, ...ResultSchema<TQueries[number]>[]]>;
22
21
  export function changesSuccess(queries: readonly QueryShape[]) {
23
22
  return Schema.Union([
24
- LiveHint,
25
23
  QueryReadDenied,
26
24
  ...queries.flatMap((query) => [
27
- ...(query.live === "rerun-and-compare" ? [QueryUnchanged] : []),
25
+ QueryUnchanged,
28
26
  Schema.Struct({
29
27
  _tag: Schema.Literal("Result"),
30
28
  query: Schema.Literal(query.name),
@@ -3,7 +3,6 @@ import * as AsyncResult from "effect/reactivity/AsyncResult";
3
3
  import type * as Atom from "effect/reactivity/Atom";
4
4
  import { activityState } from "#adapters/activityState.ts";
5
5
  import { clientConnection } from "#adapters/clientConnection.ts";
6
- import { queryCoalesceWindow } from "#queryCoalesceWindow.ts";
7
6
  import { wrapAtom } from "./wrapAtom.ts";
8
7
 
9
8
  type ActivityTracker = ReturnType<typeof makeActivity>;
@@ -12,7 +11,7 @@ export const clientActivities = new WeakMap<object, ActivityTracker>();
12
11
  export function makeActivity(factory: Atom.RuntimeFactory) {
13
12
  const { activity, change, hold, setConnection } = activityState();
14
13
  const connection = clientConnection(factory, setConnection, activity);
15
- function begin(key: "rpcCalls" | "liveInvalidations" | "commands") {
14
+ function begin(key: "rpcCalls" | "commands") {
16
15
  change(key, 1);
17
16
  return () => change(key, -1);
18
17
  }
@@ -43,8 +42,6 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
43
42
  function watch<A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) {
44
43
  const watched = wrapAtom(atom, (get) => {
45
44
  get.mount(connection.status);
46
- get.once(queryCoalesceWindow(watched));
47
- get.subscribe(queryCoalesceWindow(watched), (duration) => get.set(queryCoalesceWindow(atom), duration), { immediate: true });
48
45
  const refreshing = counter("refreshingAtoms");
49
46
  const initial = counter("initialQueries");
50
47
  let received = false;
@@ -90,7 +87,6 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
90
87
  ),
91
88
  true,
92
89
  ),
93
- queued: () => begin("liveInvalidations"),
94
90
  statuses: connection.statuses,
95
91
  track,
96
92
  watch,
@@ -8,7 +8,6 @@ export interface ClientActivityState {
8
8
  readonly connectionStatus: ConnectionStatus;
9
9
  readonly initialQueries: number;
10
10
  readonly liveHints: number;
11
- readonly liveInvalidations: number;
12
11
  readonly refreshingAtoms: number;
13
12
  readonly rpcCalls: number;
14
13
  }
@@ -29,7 +28,7 @@ export function clientActivity(client: ActiveClient): ClientActivity {
29
28
  }
30
29
 
31
30
  export function isClientIdle(state: ClientActivityState): boolean {
32
- return state.rpcCalls === 0 && state.liveInvalidations === 0 && state.refreshingAtoms === 0;
31
+ return state.rpcCalls === 0 && state.refreshingAtoms === 0;
33
32
  }
34
33
 
35
34
  export function isClientSettled(state: ClientActivityState): boolean {