-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement handle_cast and handle_continue (#88)
* Implement handling of continue-style requests * Add handle_cast
- Loading branch information
Showing
4 changed files
with
81 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,12 @@ module Twitch = struct | |
|
||
type _ Gen_server.req += | ||
| Is_connected : bool Gen_server.req | ||
| Status_value : int Gen_server.req | ||
| Profile : | ||
profile_req | ||
-> (user, [ `Twitch_error of error ]) result Gen_server.req | ||
|
||
type Gen_server.cont_req += Update_status : int -> Gen_server.cont_req | ||
type args = { verbose : bool } | ||
|
||
module Server : Gen_server.Impl with type args = args = struct | ||
|
@@ -31,29 +33,46 @@ module Twitch = struct | |
let init _args = Gen_server.Ok { status = 1 } | ||
|
||
let handle_call : | ||
type res. res Gen_server.req -> Pid.t -> state -> res * state = | ||
type res. | ||
res Gen_server.req -> | ||
Pid.t -> | ||
state -> | ||
(res, state) Gen_server.call_result = | ||
fun req _from state -> | ||
match req with | ||
| Is_connected -> (true, state) | ||
| Is_connected -> Gen_server.Reply (true, state) | ||
| Status_value -> Gen_server.Reply (state.status, state) | ||
| Profile _ -> | ||
( Ok { name = "Jonathan Archer"; email = "[email protected]" }, | ||
state ) | ||
Gen_server.Reply_continue | ||
( Ok { name = "Jonathan Archer"; email = "[email protected]" }, | ||
state, | ||
Update_status 2 ) | ||
|
||
let handle_info _msg _state = () | ||
|
||
let handle_continue cont_req _state = | ||
match cont_req with Update_status n -> { status = n } | ||
|
||
let handle_cast _cast_req _state = failwith "unimplemented" | ||
end | ||
|
||
let start_link ?(verbose = false) () = | ||
Gen_server.start_link (module Server) { verbose } | ||
|
||
let is_connected pid = Gen_server.call pid Is_connected | ||
let profile pid ~id = Gen_server.call pid (Profile { id }) | ||
let status pid = Gen_server.call pid Status_value | ||
end | ||
|
||
let main () = | ||
let (Ok _) = Logger.start () in | ||
let (Ok pid) = Twitch.start_link () in | ||
if Twitch.is_connected pid then Logger.info (fun f -> f "connected to twitch"); | ||
let status = Twitch.status pid in | ||
Logger.info (fun f -> f "Status is %d" status); | ||
let (Ok user) = Twitch.profile pid ~id:1 in | ||
Logger.info (fun f -> f "Welcome, %s!" user.name) | ||
Logger.info (fun f -> f "Welcome, %s!" user.name); | ||
let status = Twitch.status pid in | ||
Logger.info (fun f -> f "Status is %d" status) | ||
|
||
let () = Riot.run @@ main |