Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dmmulroy committed Apr 23, 2024
1 parent 5cd9b16 commit fe6c9b3
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/glitch/eventsub/client.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,24 @@ pub fn start(client: Client) {
pub fn subscribe(
client: Client,
subscription_request: CreateEventSubscriptionRequest,
) -> Result(Subject(Event), TwitchError) {
subscription_event_mailbox,
) -> Result(Nil, TwitchError) {
let state = actor.call(client, GetState(_), 1000)

use _ <- result.try(eventsub.create_eventsub_subscription(
state.api_client,
subscription_request,
))

let mailbox = process.new_subject()
actor.send(
client,
Subscribe(
subscription_request.subscription_type,
subscription_event_mailbox,
),
)

actor.send(client, Subscribe(subscription_request.subscription_type, mailbox))
Ok(mailbox)
Ok(Nil)
}

pub fn websocket_message_mailbox(client: Client) -> Subject(WebSocketMessage) {
Expand All @@ -145,7 +151,6 @@ pub fn api_client(client: Client) -> api_client.Client {
}

fn handle_message(message: Message, state: ClientState) {
io.debug(message)
case message {
GetState(state_mailbox) -> {
process.send(state_mailbox, state)
Expand All @@ -169,18 +174,27 @@ fn handle_websocket_message(state: ClientState, message: WebSocketMessage) {
case message {
websocket_message.Close -> {
// TODO SHUTDOWN
process.send(state.websocket_message_mailbox, message)
actor.continue(state)
}
websocket_message.NotificationMessage(metadata, payload) -> {
state.subscriptions
|> dict.get(metadata.subscription_type)
|> result.map(process.send(_, payload.event))
|> result.unwrap_both
|> ignore
// process.send(state.websocket_message_mailbox, message)

io.println("")
io.println("eventsub client - notification message")
io.debug(payload)
io.println("")

let assert Ok(subject) =
dict.get(state.subscriptions, metadata.subscription_type)

process.send(subject, payload.event)

actor.continue(state)
}
websocket_message.WelcomeMessage(_metadata, payload) -> {
process.send(state.websocket_message_mailbox, message)

let session_id = payload.session.id
actor.continue(State(..state, session_id: Some(session_id)))
}
Expand Down

0 comments on commit fe6c9b3

Please sign in to comment.