File size: 4,057 Bytes
31e8457
e391660
 
 
 
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e391660
 
 
 
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * Connects typed RPC clients to atoms.
 *
 * The service created here exposes a flattened RPC client plus atom-based query
 * and mutation helpers. Query atoms call RPCs and track asynchronous or
 * streaming results, while mutations run RPC calls that can invalidate
 * reactivity keys after success. Query atoms can also use request headers,
 * time-to-live settings, and serialization keys for hydration.
 *
 * @since 4.0.0
 */
import * as Context from "../../Context.js";
import * as Duration from "../../Duration.js";
import * as Effect from "../../Effect.js";
import * as Layer from "../../Layer.js";
import * as Schema from "../../Schema.js";
import * as Stream from "../../Stream.js";
import * as Headers from "../http/Headers.js";
import * as RpcClient from "../rpc/RpcClient.js";
import { RpcClientError } from "../rpc/RpcClientError.js";
import * as RpcSchema from "../rpc/RpcSchema.js";
import * as AsyncResult from "./AsyncResult.js";
import * as Atom from "./Atom.js";
import * as Reactivity from "./Reactivity.js";
/**
 * Creates a `Context.Service` class for an RPC client backed by an atom runtime.
 *
 * **Details**
 *
 * The options provide the RPC group, protocol layer, tracing options, request id generation, optional custom client
 * effect, and runtime factory used by the query and mutation helpers.
 *
 * @category constructors
 * @since 4.0.0
 */
export const Service = () => (id, options) => {
  const self = Context.Service()(id);
  const layer = Layer.effect(self, options.makeEffect ?? RpcClient.make(options.group, {
    ...options,
    flatten: true
  }));
  const runtimeFactory = options.runtime ?? Atom.runtime;
  self.runtime = runtimeFactory(typeof options.protocol === "function" ? get => Layer.provide(layer, Layer.orDie(options.protocol(get))) : Layer.provide(layer, Layer.orDie(options.protocol)));
  self.mutation = Atom.family(tag => {
    const rpc = options.group.requests.get(tag);
    return self.runtime.fn()(Effect.fnUntraced(function* ({
      headers,
      payload,
      reactivityKeys
    }) {
      const client = yield* self;
      const effect = client(tag, payload, {
        headers
      });
      return yield* reactivityKeys ? Reactivity.mutation(effect, reactivityKeys) : effect;
    })).pipe(Atom.serializable({
      key: `AtomRpc:mutation:${tag}`,
      schema: AsyncResult.Schema({
        success: rpc.successSchema,
        error: makeErrorSchema(rpc)
      })
    }));
  });
  const queryFamily = Atom.family(key => {
    const {
      headers,
      payload,
      reactivityKeys,
      tag,
      timeToLive
    } = key;
    const rpc = options.group.requests.get(tag);
    const isStream = RpcSchema.isStreamSchema(rpc.successSchema);
    let atom = isStream ? self.runtime.pull(Stream.unwrap(self.use(client => Effect.succeed(client(tag, payload, {
      headers
    }))))) : self.runtime.atom(self.use(client => client(tag, payload, {
      headers
    })));
    if (!isStream && key.serializationKey) {
      atom = Atom.serializable(atom, {
        key: `AtomRpc:${key.tag}:${key.serializationKey}`,
        schema: AsyncResult.Schema({
          success: rpc.successSchema,
          error: makeErrorSchema(rpc)
        })
      });
    }
    if (timeToLive) {
      atom = Duration.isFinite(timeToLive) ? Atom.setIdleTTL(atom, timeToLive) : Atom.keepAlive(atom);
    }
    return reactivityKeys ? self.runtime.factory.withReactivity(reactivityKeys)(atom) : atom;
  });
  self.query = (tag, payload, options) => {
    const key = {
      tag,
      payload,
      headers: options?.headers ? Headers.fromInput(options.headers) : undefined,
      reactivityKeys: options?.reactivityKeys,
      timeToLive: options?.timeToLive ? Duration.fromInputUnsafe(options.timeToLive) : undefined,
      serializationKey: options?.serializationKey
    };
    return queryFamily(key);
  };
  return self;
};
const makeErrorSchema = rpc => Schema.Union([rpc.errorSchema, ...Array.from(rpc.middlewares, middleware => middleware.error), RpcClientError]);
//# sourceMappingURL=AtomRpc.js.map