Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Merge tuples types

$
0
0

Yes, I believe so.

Right now, in my bindings I invoke an external function that returns:

  type queryResult<'documentdata> = (
    option<Firebase.Firestore.querySnapshot<'documentdata>>,
    bool,
    option<Firebase.Firestore.firestoreError>,
  )

If I have two queries, I’d like to combine the queryResult to have something like:

type internalQueryResult<'t> =
  | Data('t)
  | Loading
  | Error(Firebase.Firestore.firestoreError)
  | Unknown

If either query is loading or has an error then return that for combined result.
If both have data, return a tuple of data 'a and data 'b.

let combine = (
  a: ReactFirehooks.Firestore.queryResult<'a>,
  b: ReactFirehooks.Firestore.queryResult<'b>,
) => {
  switch (a, b) {
  | ((_, true, _), _)
  | (_, (_, true, _)) =>
    Loading
  | ((_, _, Some(error)), _) => Error(error)
  | (_, (_, _, Some(error))) => Error(error)
  | (
      (Some(qsA), _, _), 
      (Some(qsB), _ ,_)
    ) => {
      Data(qsA, qsB)
    } 
  | _ => Unknown
  }
}

Some of my components have five queries, so I’d like to flatten that nested tuple result.


Viewing all articles
Browse latest Browse all 2592

Trending Articles