@shivaedev/contract 0.34.0 → 0.36.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 (75) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +8 -4
  3. package/dist/HostVersion.d.ts +31 -0
  4. package/dist/HostVersion.d.ts.map +1 -0
  5. package/dist/HostVersion.js +7 -0
  6. package/dist/HostVersion.js.map +1 -0
  7. package/dist/LiveServer.d.ts +3 -8
  8. package/dist/LiveServer.d.ts.map +1 -1
  9. package/dist/LiveServer.js +8 -11
  10. package/dist/LiveServer.js.map +1 -1
  11. package/dist/adapters/activityState.d.ts +3 -2
  12. package/dist/adapters/activityState.d.ts.map +1 -1
  13. package/dist/adapters/activityState.js +4 -1
  14. package/dist/adapters/activityState.js.map +1 -1
  15. package/dist/adapters/clientConnection.d.ts +8 -0
  16. package/dist/adapters/clientConnection.d.ts.map +1 -0
  17. package/dist/adapters/clientConnection.js +22 -0
  18. package/dist/adapters/clientConnection.js.map +1 -0
  19. package/dist/automaticLive.d.ts.map +1 -1
  20. package/dist/automaticLive.js +3 -1
  21. package/dist/automaticLive.js.map +1 -1
  22. package/dist/clientActivity.d.ts +6 -0
  23. package/dist/clientActivity.d.ts.map +1 -1
  24. package/dist/clientActivity.js +1 -1
  25. package/dist/clientActivity.js.map +1 -1
  26. package/dist/internal/automaticQuery.d.ts +2 -2
  27. package/dist/internal/automaticQuery.d.ts.map +1 -1
  28. package/dist/internal/automaticQuery.js +12 -10
  29. package/dist/internal/automaticQuery.js.map +1 -1
  30. package/dist/internal/clientActivity.d.ts +7 -1
  31. package/dist/internal/clientActivity.d.ts.map +1 -1
  32. package/dist/internal/clientActivity.js +13 -3
  33. package/dist/internal/clientActivity.js.map +1 -1
  34. package/dist/internal/live-server/resync.d.ts +4 -0
  35. package/dist/internal/live-server/resync.d.ts.map +1 -0
  36. package/dist/internal/live-server/resync.js +4 -0
  37. package/dist/internal/live-server/resync.js.map +1 -0
  38. package/dist/internal/live-server/stream.d.ts +23 -0
  39. package/dist/internal/live-server/stream.d.ts.map +1 -0
  40. package/dist/internal/live-server/stream.js +5 -0
  41. package/dist/internal/live-server/stream.js.map +1 -0
  42. package/dist/internal/live-server/subscribe.d.ts +7 -0
  43. package/dist/internal/live-server/subscribe.d.ts.map +1 -0
  44. package/dist/internal/live-server/subscribe.js +8 -0
  45. package/dist/internal/live-server/subscribe.js.map +1 -0
  46. package/dist/internal/liveRetry.d.ts +2 -2
  47. package/dist/internal/liveRetry.d.ts.map +1 -1
  48. package/dist/internal/liveRetry.js +6 -3
  49. package/dist/internal/liveRetry.js.map +1 -1
  50. package/dist/internal/rpcClient.d.ts +2 -0
  51. package/dist/internal/rpcClient.d.ts.map +1 -1
  52. package/dist/internal/rpcClient.js +1 -1
  53. package/dist/internal/rpcClient.js.map +1 -1
  54. package/dist/internal/subjectMiddleware.d.ts +1 -2
  55. package/dist/internal/subjectMiddleware.d.ts.map +1 -1
  56. package/dist/live.d.ts +1 -0
  57. package/dist/live.d.ts.map +1 -1
  58. package/dist/live.js +14 -3
  59. package/dist/live.js.map +1 -1
  60. package/package.json +15 -15
  61. package/src/HostVersion.ts +7 -0
  62. package/src/LiveServer.ts +8 -19
  63. package/src/adapters/activityState.ts +10 -3
  64. package/src/adapters/clientConnection.ts +37 -0
  65. package/src/automaticLive.ts +3 -1
  66. package/src/clientActivity.ts +9 -2
  67. package/src/internal/automaticQuery.ts +13 -11
  68. package/src/internal/clientActivity.ts +13 -3
  69. package/src/internal/live-server/resync.ts +6 -0
  70. package/src/internal/live-server/stream.ts +7 -0
  71. package/src/internal/live-server/subscribe.ts +16 -0
  72. package/src/internal/liveRetry.ts +9 -2
  73. package/src/internal/rpcClient.ts +1 -1
  74. package/src/internal/subjectMiddleware.ts +1 -1
  75. package/src/live.ts +15 -2
@@ -2,14 +2,16 @@ import { Effect } from "effect";
2
2
  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
+ import { clientConnection } from "#adapters/clientConnection.ts";
5
6
  import { queryCoalesceWindow } from "#queryCoalesceWindow.ts";
6
7
  import { wrapAtom } from "./wrapAtom.ts";
7
8
 
8
9
  type ActivityTracker = ReturnType<typeof makeActivity>;
9
10
  export const clientActivities = new WeakMap<object, ActivityTracker>();
10
11
 
11
- export function makeActivity() {
12
- const { activity, change } = activityState();
12
+ export function makeActivity(factory: Atom.RuntimeFactory) {
13
+ const { activity, change, setConnection } = activityState();
14
+ const connection = clientConnection(factory, setConnection);
13
15
  function begin(key: "rpcCalls" | "liveInvalidations") {
14
16
  change(key, 1);
15
17
  return () => change(key, -1);
@@ -23,6 +25,7 @@ export function makeActivity() {
23
25
  }
24
26
  function watch<A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) {
25
27
  const watched = wrapAtom(atom, (get) => {
28
+ get.mount(connection.status);
26
29
  get.once(queryCoalesceWindow(watched));
27
30
  get.subscribe(queryCoalesceWindow(watched), (duration) => get.set(queryCoalesceWindow(atom), duration), { immediate: true });
28
31
  let busy = false;
@@ -59,5 +62,12 @@ export function makeActivity() {
59
62
  });
60
63
  return watched;
61
64
  }
62
- return { activity, hint: () => change("liveHints", 1), queued: () => begin("liveInvalidations"), track, watch };
65
+ return {
66
+ activity: Object.assign(activity, { connectionStatus: connection.status }),
67
+ hint: () => change("liveHints", 1),
68
+ queued: () => begin("liveInvalidations"),
69
+ statuses: connection.statuses,
70
+ track,
71
+ watch,
72
+ };
63
73
  }
@@ -0,0 +1,6 @@
1
+ import type { Effect } from "effect";
2
+ import type { makeLiveServer } from "#makeLiveServer.ts";
3
+
4
+ export function resync(server: Effect.Success<typeof makeLiveServer>) {
5
+ return () => server.resync;
6
+ }
@@ -0,0 +1,7 @@
1
+ import { Effect } from "effect";
2
+ import type { Key } from "@shivaedev/changes/keys.ts";
3
+ import type { makeLiveServer } from "#makeLiveServer.ts";
4
+
5
+ export function stream(server: Effect.Success<typeof makeLiveServer>) {
6
+ return (keys: readonly Key[]) => Effect.succeed(server.stream(keys));
7
+ }
@@ -0,0 +1,16 @@
1
+ import { Effect, type Stream } from "effect";
2
+ import type { makeLiveServer } from "#makeLiveServer.ts";
3
+ import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
4
+ import type { QueryUpdate, SubscriptionHint } from "#queryUpdate.ts";
5
+ import type { QueryReadDenied } from "#readPolicyErrors.ts";
6
+
7
+ export function subscribe(server: Effect.Success<typeof makeLiveServer>) {
8
+ function subscribeToQueries<E, R, TRead extends QueryRerun<unknown, unknown> | undefined = undefined>(
9
+ subscription: QuerySubscription<E, R>,
10
+ read?: TRead & (QueryRerun<E, R> | undefined),
11
+ ): Effect.Effect<Stream.Stream<TRead extends undefined ? SubscriptionHint : QueryUpdate, E | QueryReadDenied, R>>;
12
+ function subscribeToQueries<E, R>(subscription: QuerySubscription<E, R>, read?: QueryRerun<E, R>) {
13
+ return Effect.succeed(read === undefined ? server.subscribe(subscription) : server.subscribe(subscription, read));
14
+ }
15
+ return subscribeToQueries;
16
+ }
@@ -1,9 +1,10 @@
1
- import { Cause, type Duration, Effect, Option, Predicate, Schedule } from "effect";
1
+ import { Cause, type Duration, Effect, Option, Predicate, Queue, Schedule } from "effect";
2
2
 
3
3
  export function liveRetry<E, R>(
4
4
  attempt: Effect.Effect<void, E, R>,
5
5
  ended: (failure: Option.Option<Cause.Cause<E>>, retry: boolean) => void,
6
6
  delay: Duration.Input = "1 second",
7
+ resume?: Queue.Queue<void>,
7
8
  ) {
8
9
  const next = Effect.matchCause(attempt, {
9
10
  onFailure: (failure) => {
@@ -17,8 +18,14 @@ export function liveRetry<E, R>(
17
18
  return true;
18
19
  },
19
20
  });
20
- return Effect.repeat(next, {
21
+ const run = Effect.repeat(next, {
21
22
  schedule: Schedule.jittered(Schedule.min([Schedule.exponential(delay, 2), Schedule.spaced("30 seconds")])),
22
23
  while: (retry) => retry,
23
24
  }).pipe(Effect.asVoid);
25
+ return resume === undefined
26
+ ? run
27
+ : Effect.suspend(() => Effect.raceFirst(Effect.as(run, false), Effect.as(Queue.take(resume), true))).pipe(
28
+ Effect.repeat({ while: (restarted) => restarted }),
29
+ Effect.asVoid,
30
+ );
24
31
  }
@@ -14,7 +14,7 @@ export interface RpcSource {
14
14
  }
15
15
 
16
16
  export function rpcClient(name: string, source: RpcSource, options: ClientOptions) {
17
- const activity = makeActivity();
17
+ const activity = makeActivity(options.runtime ?? Atom.runtime);
18
18
  const group = RpcGroup.make(...source.requests.values()).annotateMerge(source.annotations);
19
19
  function sharedProtocol(identity?: string) {
20
20
  return socketProtocol({
@@ -3,7 +3,7 @@ import type { SubjectDeclaration, SubjectProvides } from "#subject.ts";
3
3
 
4
4
  export const SUBJECT_TYPE_ID = "@shivaedev/contract/SubjectDeclaration";
5
5
 
6
- interface SubjectMiddleware<TSubject extends SubjectDeclaration> extends RpcMiddleware.AnyId {
6
+ export interface SubjectMiddleware<TSubject extends SubjectDeclaration> extends RpcMiddleware.AnyId {
7
7
  readonly [RpcMiddleware.TypeId]: {
8
8
  readonly provides: SubjectProvides<TSubject>;
9
9
  readonly requires: never;
package/src/live.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Cause, Duration, Effect, Option, Schema, type Stream } from "effect";
1
+ import { Cause, Duration, Effect, Option, Queue, Schema, type Stream } from "effect";
2
2
  import * as Atom from "effect/reactivity/Atom";
3
3
  import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
4
4
  import * as Reactivity from "effect/reactivity/Reactivity";
@@ -27,6 +27,7 @@ export interface LiveOptions<E, R> {
27
27
  readonly coalesceWindow?: Duration.Input;
28
28
  readonly initialResync?: boolean;
29
29
  readonly keyContext?: KeyContext | undefined;
30
+ readonly reopenOnResume?: boolean;
30
31
  readonly resume?: Atom.Atom<unknown>;
31
32
  readonly resyncKeys: readonly Key[] | ((registry: AtomRegistry.AtomRegistry) => readonly Key[]);
32
33
  readonly retryDelay?: Duration.Input;
@@ -44,6 +45,7 @@ export function live<R, E, TRuntimeError>(runtime: Atom.AtomRuntime<R, TRuntimeE
44
45
  .atom((get) =>
45
46
  Effect.gen(function* () {
46
47
  get.mount(status);
48
+ const resumed = yield* Queue.unbounded<void>();
47
49
  const reactivity = yield* Reactivity.Reactivity;
48
50
  const retryKeys = options.retryKeys ?? (typeof options.resyncKeys === "function" ? options.resyncKeys(get.registry) : options.resyncKeys);
49
51
  yield* retryOnChange(status, clientReadKeys(retryKeys, options.keyContext), () => get.refreshSelf());
@@ -101,10 +103,20 @@ export function live<R, E, TRuntimeError>(runtime: Atom.AtomRuntime<R, TRuntimeE
101
103
  resync();
102
104
  }
103
105
  });
106
+ function resume() {
107
+ const current = get.once(status).connection;
108
+ if (current === "failed") {
109
+ return;
110
+ }
111
+ resync();
112
+ if (options.reopenOnResume === true || current === "reconnecting") {
113
+ Queue.offerUnsafe(resumed, undefined);
114
+ }
115
+ }
104
116
  if (options.resume !== undefined) {
105
117
  // A subscription alone does not initialize a derived signal's dependencies.
106
118
  get.once(options.resume);
107
- get.subscribe(options.resume, resync);
119
+ get.subscribe(options.resume, resume);
108
120
  }
109
121
  const attempt = liveAttempt(options.stream, {
110
122
  changed,
@@ -129,6 +141,7 @@ export function live<R, E, TRuntimeError>(runtime: Atom.AtomRuntime<R, TRuntimeE
129
141
  update({ connection: retry ? "reconnecting" : "failed", failure, needsResync: true });
130
142
  },
131
143
  options.retryDelay,
144
+ resumed,
132
145
  );
133
146
  }),
134
147
  )