Source file local_data_source.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
open! Core
open! Private_skyline_prelude

type 'a t =
  { filtered : 'a iarray Bonsai.t
  ; query_for_suggestions : string Bonsai.t
  ; max_suggestions : int option
  }

let create
  (type collection elt)
  ~length
  ~(to_iarray_map : 'out. collection -> f:(elt -> 'out) @ local -> 'out iarray)
  ~is_iarray
  ~(items : collection Bonsai.t)
  ~to_search_string
  ~query_for_suggestions
  ~max_suggestions
  ~worker_threshold
  (graph @ local)
  : elt t
  =
  let use_worker =
    let%arr items in
    length items >= worker_threshold
  in
  { query_for_suggestions
  ; max_suggestions
  ; filtered =
      (match%sub use_worker with
       | false ->
         Incremental_cached_filter.component
           ~items
           ~query_for_suggestions
           ~to_search_string
           ~to_iarray:to_iarray_map
           graph
       | true ->
         Bonsai.delay graph ~f:(fun (graph @ local) ->
           let items : elt iarray Bonsai.t =
             match is_iarray with
             | Some (T : (collection, elt iarray) Type_equal.t) -> items
             | None ->
               let%arr items in
               to_iarray_map items ~f:Fn.id
           in
           Worker_filter.component
             ~items
             ~max_suggestions
             ~query_for_suggestions
             ~to_search_string
             graph))
  }
;;

let of_iarray
  ~items
  ~to_search_string
  ~query_for_suggestions
  ~max_suggestions
  ~worker_threshold
  (graph @ local)
  : 'a t
  =
  create
    ~length:Iarray.length
    ~to_iarray_map:Iarray.map
    ~is_iarray:(Some T)
    ~items
    ~to_search_string
    ~query_for_suggestions
    ~max_suggestions
    ~worker_threshold
    graph
;;

let of_list
  ~items
  ~to_search_string
  ~query_for_suggestions
  ~max_suggestions
  ~worker_threshold
  (graph @ local)
  : 'a t
  =
  create
    ~length:List.length
    ~to_iarray_map:Iarray.of_list_map
    ~is_iarray:None
    ~items
    ~to_search_string
    ~query_for_suggestions
    ~max_suggestions
    ~worker_threshold
    graph
;;

let merge_with_fallback
  { filtered; query_for_suggestions; max_suggestions }
  ~fallback_items
  (_ : Bonsai.graph @ local)
  =
  let fallback_items =
    match fallback_items with
    | None -> Bonsai.return []
    | Some fallback_items ->
      let%arr fallback_items and query_for_suggestions in
      let l = fallback_items ~query:query_for_suggestions in
      (match max_suggestions with
       | None -> l
       | Some n -> List.take l n)
  in
  let%arr filtered and fallback_items in
  let num_filtered =
    match max_suggestions with
    | None -> Iarray.length filtered
    | Some n ->
      let num_fallback = List.length fallback_items in
      Int.max 0 (Int.min (Iarray.length filtered) (n - num_fallback))
  in
  let mutable acc = fallback_items in
  for i = num_filtered - 1 downto 0 do
    acc <- Iarray.unsafe_get filtered i :: acc
  done;
  acc
;;