Skip to content

Commit

Permalink
bindings: handle more events
Browse files Browse the repository at this point in the history
  • Loading branch information
voodoos committed Oct 23, 2024
1 parent d807912 commit a0d2a16
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
14 changes: 14 additions & 0 deletions vendor/brr_lwd_ui/bindings/yjs/awareness.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ external to_jv : t -> Jv.t = "%identity"

let class_awarness = Jv.get (get_awareness_protocol ()) "Awareness"
let make doc = Jv.new' class_awarness [| Doc.Doc.to_jv doc |]

let set_local_state_field t ~field value =
Jv.call t "setLocalStateField" [| Jv.of_string field; value |] |> ignore

let set_user_info t ~name ?color () =
let info =
Jv.obj
@@
match color with
| Some color ->
[| ("name", Jv.of_string name); ("color", Jv.of_string color) |]
| None -> [| ("name", Jv.of_string name) |]
in
set_local_state_field t ~field:"user" info
1 change: 1 addition & 0 deletions vendor/brr_lwd_ui/bindings/yjs/awareness.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ type t

val to_jv : t -> Jv.t
val make : Doc.Doc.t -> t
val set_user_info : t -> name:string -> ?color:string -> unit -> unit
53 changes: 53 additions & 0 deletions vendor/brr_lwd_ui/bindings/yjs/webrtc_provider.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,56 @@ let make ~room_name ?signaling ?awareness yjs_doc =
in
Jv.new' web_rtc_provider
[| Jv.of_string room_name; Doc.Doc.to_jv yjs_doc; Jv.obj options |]

type status = { connected : bool }
type synced = { synced : bool }

type peers = {
added : string list;
removed : string list;
webrtc_peers : string list;
bc_peers : string list;
}

type 'a event =
| Status : status event
| Synced : synced event
| Peers : peers event

let on (type a) t (event : a event) ~(f : a -> unit) =
match event with
| Status ->
ignore
@@ Jv.call t "on"
[|
Jv.of_string "status";
Jv.callback ~arity:1 (fun status ->
f { connected = Jv.get status "connected" |> Jv.to_bool });
|]
| Synced ->
ignore
@@ Jv.call t "on"
[|
Jv.of_string "synced";
Jv.callback ~arity:1 (fun synced ->
f { synced = Jv.get synced "synced" |> Jv.to_bool });
|]
| Peers ->
ignore
@@ Jv.call t "on"
[|
Jv.of_string "peers";
Jv.callback ~arity:1 (fun peers ->
let get_array key =
Jv.get peers key |> Jv.to_list Jv.to_string
in
let peers =
{
added = get_array "added";
removed = get_array "removed";
webrtc_peers = get_array "webrtcPeers";
bc_peers = get_array "bcPeers";
}
in
f peers);
|]
17 changes: 17 additions & 0 deletions vendor/brr_lwd_ui/bindings/yjs/webrtc_provider.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,20 @@ val make :
?awareness:Awareness.t ->
Doc.Doc.t ->
t

type status = { connected : bool }
type synced = { synced : bool }

type peers = {
added : string list;
removed : string list;
webrtc_peers : string list;
bc_peers : string list;
}

type 'a event =
| Status : status event
| Synced : synced event
| Peers : peers event

val on : 'a. t -> 'a event -> f:('a -> unit) -> unit

0 comments on commit a0d2a16

Please sign in to comment.