diff options
author | Clombrong <cromblong@egregore.fun> | 2025-08-12 12:13:34 +0200 |
---|---|---|
committer | Clombrong <cromblong@egregore.fun> | 2025-08-14 14:52:29 +0200 |
commit | 00b2a40656dad43b1d48212e4b53f145b24ea3e8 (patch) | |
tree | 6c64c3030647a18a695ae623c241c3616e979b03 | |
parent | 1b4de3ec44510b0e2a9f5a6c90a1cf1a3c2b889f (diff) |
feat(session): merge connect and create into a single function
-rw-r--r-- | lib/session.ml | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/lib/session.ml b/lib/session.ml index 7534433..b874c23 100644 --- a/lib/session.ml +++ b/lib/session.ml @@ -7,39 +7,39 @@ type state = | Connecting | Connected of Portal.t * Stream.features -(** [connect domain config] is a promise containing the portal connected to the XMPP - server located at [domain], and all its supported features. +(** [create domain config] is a promise containing a signal representing the portal + connected to the XMPP server located at [domain], and all its supported features. - Basically, it conforms to {{: - https://datatracker.ietf.org/doc/html/rfc6120#section-4.3 }}, and gets the provided - Portal in a "ready" state. *) -let connect (domain : string) (config : Stream.config) : (Portal.t * Stream.features) Lwt.t = - let open Portal in - let open Stream in - let* portal = connect domain - in - let needs_restart = function - | Feature.Mechanisms _ | STARTTLS -> true - | _ -> false - in - let+ features = - let rec handle_features (features : Stream.features) : Stream.features Lwt.t = - match features with - | feature :: rest -> - let* () = negotiate feature portal config - in if needs_restart (Feature.unwrap feature) - then start domain portal >>= handle_features - else handle_features rest - | features -> Lwt.return features - in start domain portal >>= handle_features - in (portal, features) + When calling the function, the portal will try to connect to the provided domain. + Basically, it conforms to {{: + https://datatracker.ietf.org/doc/html/rfc6120#section-4.3 }}, and gets the Portal in + a "ready" state. *) let create (domain : string) (config : Stream.config) : (state signal * (unit -> unit)) Lwt.t = let state, update = S.create Disconnected in + let connect () : unit Lwt.t = + let open Portal in + let open Stream in + let* portal = connect domain + in + let needs_restart = function + | Feature.Mechanisms _ | STARTTLS -> true + | _ -> false + in + let+ features = + let rec handle_features (features : Stream.features) : Stream.features Lwt.t = + match features with + | feature :: rest -> + let* () = negotiate feature portal config + in if needs_restart (Feature.unwrap feature) + then start domain portal >>= handle_features + else handle_features rest + | features -> Lwt.return features + in start domain portal >>= handle_features + in update (Connected (portal, features)) + in let+ () = S.map_s - (function - | Connecting -> let+ portal, features = connect domain config - in update (Connected (portal, features)) - | _ -> Lwt.return_unit) + (function | Connecting -> connect () + | _ -> Lwt.return_unit) state >|= S.keep; in state, (fun () -> update Connecting) |