package babel

  1. Overview
  2. Docs

Source file caller.ml

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
open Core
open Async_kernel
open Async_rpc_kernel
module Fn = Babel_fn

module Strategy = struct
  type 'a t =
    { dispatch : Rpc.Connection.t -> 'a
    ; rpc : Generic_rpc.t
    }
  [@@deriving fields ~getters]

  let map { dispatch; rpc } ~f =
    let dispatch conn = f (dispatch conn) in
    { dispatch; rpc }
  ;;

  let description t = Generic_rpc.description t.rpc
  let shape t = description t, Generic_rpc.shape t.rpc
  let in_menu t menu = Versioned_rpc.Menu.mem menu (description t)
end

open Strategy

type 'a t = 'a Strategy.t Nonempty_list.t

let map t ~f = Nonempty_list.map t ~f:(fun s -> Strategy.map ~f s)
let shapes t = Nonempty_list.map t ~f:Strategy.shape
let supported_rpcs t = Nonempty_list.map t ~f:Strategy.rpc
let descriptions t = Nonempty_list.map t ~f:Strategy.description

let print_shapes t =
  print_s [%sexp (shapes t : (Rpc.Description.t * Shape.t) Nonempty_list.t)]
;;

let find_strategy t menu =
  match Nonempty_list.find t ~f:(fun strategy -> Strategy.in_menu strategy menu) with
  | Some strategy -> Ok strategy
  | None ->
    let callee_rpcs = Versioned_rpc.Menu.supported_rpcs menu in
    let caller_rpcs = descriptions t in
    Or_error.error_s
      [%message
        "Could not find any supported rpc in callee"
          (callee_rpcs : Rpc.Description.t list)
          (caller_rpcs : Rpc.Description.t Nonempty_list.t)]
;;

let to_dispatch_fun t menu = find_strategy t menu |> Or_error.map ~f:Strategy.dispatch
let description t menu = find_strategy t menu |> Or_error.map ~f:Strategy.description
let of_list_decreasing_preference = Nonempty_list.concat
let map_query t = Tilde_f.Let_syntax.(map t >>= Fn.map_input)
let map_response t = Tilde_f.Let_syntax.(map t >>= Fn.map)

let can_dispatch t connection_with_menu =
  let menu = Versioned_rpc.Connection_with_menu.menu connection_with_menu in
  find_strategy t menu |> Or_error.is_ok
;;

let dispatch_multi t connection_with_menu query ~on_error =
  let menu = Versioned_rpc.Connection_with_menu.menu connection_with_menu in
  match to_dispatch_fun t menu with
  | Ok f -> f (Versioned_rpc.Connection_with_menu.connection connection_with_menu) query
  | Error _ as error -> on_error error
;;

let dispatch_multi_or_error_deferred t connection_with_menu query =
  dispatch_multi t connection_with_menu query ~on_error:Deferred.return
;;

let dispatch_multi_or_error t connection_with_menu query =
  dispatch_multi t connection_with_menu query ~on_error:Fn.id
;;

let dispatch_multi_exn t connection_with_menu query =
  dispatch_multi t connection_with_menu query ~on_error:ok_exn
;;

let adder t ~rpc ~f = of_list_decreasing_preference [ f rpc; t ]

module Rpc = struct
  let dispatch_multi = dispatch_multi_or_error_deferred

  let singleton rpc =
    Nonempty_list.singleton { dispatch = Rpc.Rpc.dispatch rpc; rpc = Rpc rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query

  let map_response t =
    Tilde_f.Let_syntax.(
      map_response t >>= Deferred.map >>= Tilde_f.of_local_k Or_error.map)
  ;;
end

module Rpc' = struct
  open Async_rpc_kernel

  let singleton rpc =
    Nonempty_list.singleton { dispatch = Rpc.Rpc.dispatch' rpc; rpc = Rpc rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query

  let map_response t =
    Tilde_f.Let_syntax.(map_response t >>= Deferred.map >>= Tilde_f.of_local_k Result.map)
  ;;
end

module Rpc_exn = struct
  open Async_rpc_kernel

  let dispatch_multi = dispatch_multi_exn

  let singleton rpc =
    Nonempty_list.singleton { dispatch = Rpc.Rpc.dispatch_exn rpc; rpc = Rpc rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query
  let map_response t = Tilde_f.Let_syntax.(map_response t >>= Deferred.map)
end

module Pipe_rpc = struct
  open Async_rpc_kernel

  let dispatch_multi = dispatch_multi_or_error_deferred

  let singleton rpc =
    Nonempty_list.singleton { dispatch = Rpc.Pipe_rpc.dispatch rpc; rpc = Pipe rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query

  let map_error t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tilde_f.of_local_k Result.map_error)
  ;;

  let filter_map_response t =
    let open Tilde_f.Let_syntax in
    map_response t
    >>= Deferred.map
    >>= Tilde_f.of_local_k Or_error.map
    >>= Tilde_f.of_local_k Result.map
    >>= Tuple2.map_fst
    >>= Pipe.filter_map ?max_queue_length:None
  ;;

  let map_response t =
    let open Tilde_f.Let_syntax in
    map_response t
    >>= Deferred.map
    >>= Tilde_f.of_local_k Or_error.map
    >>= Tilde_f.of_local_k Result.map
    >>= Tuple2.map_fst
    >>= Pipe_extended.map_batched
  ;;
end

module Pipe_rpc_exn = struct
  open Async_rpc_kernel

  let dispatch_multi = dispatch_multi_exn

  let singleton rpc =
    Nonempty_list.singleton { dispatch = Rpc.Pipe_rpc.dispatch_exn rpc; rpc = Pipe rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query

  let filter_map_response t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tuple2.map_fst
      >>= Pipe.filter_map ?max_queue_length:None)
  ;;

  let map_response t =
    Tilde_f.Let_syntax.(
      map_response t >>= Deferred.map >>= Tuple2.map_fst >>= Pipe_extended.map_batched)
  ;;
end

module Pipe_rpc_iter = struct
  open Async_rpc_kernel

  module Id = struct
    type t =
      | T :
          { rpc : _ Rpc.Pipe_rpc.t
          ; connection : Rpc.Connection.t
          ; id : Rpc.Pipe_rpc.Id.t
          }
          -> t
  end

  module Pipe_message = struct
    include Rpc.Pipe_rpc.Pipe_message

    let map (t : _ t) ~f =
      match t with
      | Update a -> Update (f a)
      | Closed _ as t -> t
    ;;
  end

  module Generator = struct
    let map t ~f ~f:k = f (t ~f:k)
  end

  let dispatch_multi t connection_with_menu query ~f =
    let menu = Versioned_rpc.Connection_with_menu.menu connection_with_menu in
    match to_dispatch_fun t menu with
    | Ok dispatch ->
      dispatch
        (Versioned_rpc.Connection_with_menu.connection connection_with_menu)
        query
        ~f
    | Error _ as error -> Deferred.return error
  ;;

  let singleton rpc =
    let dispatch connection query ~f =
      (let open Tilde_f.Let_syntax in
       Rpc.Pipe_rpc.dispatch_iter rpc connection query ~f
       |> Deferred.map
       >>= Tilde_f.of_local_k Or_error.map
       >>= Tilde_f.of_local_k Result.map)
        ~f:(fun id -> Id.T { rpc; connection; id })
    in
    Nonempty_list.singleton { dispatch; rpc = Pipe rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query

  let map_error t =
    let open Tilde_f.Let_syntax in
    map_response t
    >>= Generator.map
    >>= Deferred.map
    >>= Tilde_f.of_local_k Or_error.map
    >>= Tilde_f.of_local_k Result.map_error
  ;;

  let map_response t =
    Tilde_f.Let_syntax.(map_response t >>= Tilde_f.map >>= Pipe_message.map)
  ;;

  let abort (Id.T { rpc; connection; id }) = Rpc.Pipe_rpc.abort rpc connection id
end

module State_rpc = struct
  open Async_rpc_kernel

  let dispatch_multi = dispatch_multi_or_error_deferred

  let singleton rpc =
    Nonempty_list.singleton { dispatch = Rpc.State_rpc.dispatch rpc; rpc = State rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query

  let map_state t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tilde_f.of_local_k Result.map
      >>= Tuple3.map_fst)
  ;;

  let filter_map_update t =
    let open Tilde_f.Let_syntax in
    map_response t
    >>= Deferred.map
    >>= Tilde_f.of_local_k Or_error.map
    >>= Tilde_f.of_local_k Result.map
    >>= Tuple3.map_snd
    >>= Pipe.filter_map ?max_queue_length:None
  ;;

  let map_update t =
    let open Tilde_f.Let_syntax in
    map_response t
    >>= Deferred.map
    >>= Tilde_f.of_local_k Or_error.map
    >>= Tilde_f.of_local_k Result.map
    >>= Tuple3.map_snd
    >>= Pipe_extended.map_batched
  ;;

  let map_error t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tilde_f.of_local_k Result.map_error)
  ;;
end

module One_way = struct
  open Async_rpc_kernel

  let dispatch_multi = dispatch_multi_or_error

  let singleton rpc =
    Nonempty_list.singleton { dispatch = Rpc.One_way.dispatch rpc; rpc = One_way rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query
end

module One_way_exn = struct
  open Async_rpc_kernel

  let dispatch_multi = dispatch_multi_exn

  let singleton rpc =
    Nonempty_list.singleton { dispatch = Rpc.One_way.dispatch_exn rpc; rpc = One_way rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query
end

module One_way' = struct
  open Async_rpc_kernel

  let singleton rpc =
    Nonempty_list.singleton { dispatch = Rpc.One_way.dispatch' rpc; rpc = One_way rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query
end

module Streamable_plain_rpc = struct
  let dispatch_multi = dispatch_multi_or_error_deferred

  let singleton rpc =
    Nonempty_list.singleton
      { dispatch = Streamable.Plain_rpc.dispatch' rpc; rpc = Streamable_plain rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query

  let map_response t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tilde_f.of_local_k Or_error.map)
  ;;
end

module Streamable_pipe_rpc = struct
  let dispatch_multi = dispatch_multi_or_error_deferred

  let singleton rpc =
    Nonempty_list.singleton
      { dispatch = Streamable.Pipe_rpc.dispatch' rpc; rpc = Streamable_pipe rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query

  let filter_map_response t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Pipe.filter_map ?max_queue_length:None)
  ;;

  let map_response t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Pipe_extended.map_batched)
  ;;
end

module Streamable_state_rpc = struct
  let dispatch_multi = dispatch_multi_or_error_deferred

  let singleton rpc =
    Nonempty_list.singleton
      { dispatch = Streamable.State_rpc.dispatch' rpc; rpc = Streamable_state rpc }
  ;;

  let add = adder ~f:singleton
  let map_query = map_query

  let map_state t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tuple2.map_fst)
  ;;

  let filter_map_update t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tuple2.map_snd
      >>= Pipe.filter_map ?max_queue_length:None)
  ;;

  let map_update t =
    Tilde_f.Let_syntax.(
      map_response t
      >>= Deferred.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tilde_f.of_local_k Or_error.map
      >>= Tuple2.map_snd
      >>= Pipe_extended.map_batched)
  ;;
end

module Expert = struct
  let return rpc a =
    Nonempty_list.singleton
      { dispatch = (fun (_ : Async_rpc_kernel.Rpc.Connection.t) -> a); rpc }
  ;;
end