-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add ActionCable to rescript (#565) * unsubscribe from action cable when component didunmount * format rescript
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 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
39 changes: 39 additions & 0 deletions
39
client/app/bundles/comments/rescript/bindings/ActionCable.res
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
type webSocket | ||
type optionalWebSocket = option<webSocket> | ||
type subscriptions | ||
type consumer = { | ||
disconnect: unit => unit, | ||
subscriptions: subscriptions, | ||
} | ||
type actionCable = {createConsumer: unit => consumer} | ||
type createConsumer | ||
type subscription<'sendData> = { | ||
consumer: consumer, | ||
send: 'sendData => unit, | ||
} | ||
type subscriprtionCallbacks<'updateData> = { | ||
connected: unit => unit, | ||
received: 'updateData => unit, | ||
disconnected: unit => unit, | ||
} | ||
|
||
@val @scope("window") @return(nullable) | ||
external webSocket: option<webSocket> = "WebSocket" | ||
@val external actionCable: actionCable = "ActionCable" | ||
@send | ||
external createSubscription: ( | ||
subscriptions, | ||
string, | ||
subscriprtionCallbacks<'updateData>, | ||
) => subscription<'sendData'> = "create" | ||
@send external sendData: (subscription<'sendData>, 'sendData) => unit = "send" | ||
@send external unsubscribeSubscription: subscription<'sendData'> => unit = "unsubscribe" | ||
@send external diconnectConsumer: consumer => unit = "disconnect" | ||
|
||
let subscribeConsumer = ( | ||
channnelName: string, | ||
subscriprtionCallbacks: subscriprtionCallbacks<'updateData>, | ||
) => { | ||
let consumer = actionCable.createConsumer() | ||
createSubscription(consumer.subscriptions, channnelName, subscriprtionCallbacks) | ||
} |