-
Notifications
You must be signed in to change notification settings - Fork 0
/
cable.js
70 lines (58 loc) · 2.11 KB
/
cable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { createConsumer } from "@rails/actioncable";
import config from "./config";
import state from "./state";
import { camelize } from "./stringCase";
let consumer = null;
const getEndpoint = () => {
const [protocol, host] = config.apiHost.split("://");
const wsProtocol = protocol === "https" ? "wss" : "ws";
return `${wsProtocol}://${host}/cable/${state.getToken()}`;
};
const getConsumer = () => {
if (!consumer) {
consumer = createConsumer(getEndpoint());
}
return consumer;
};
/**
* There are a few functions on the client that will establish a WebSocket
* connection and call a callback function when data is received. For these
* functions, in order to avoid leaking memory, it's important to ensure that
* when you're done with the subscription (for instance when the component
* containing it is unmounted) that you call `unsubscribe` on the subscription
* object. An example with React of using these functions is below:
*
* import React, { useEffect } from "react";
* import { onNotificationReceived } from "@culturehq/client";
*
* const MyComponent = () => {
* const [lastNotification, setLastNotification] = useState(null);
*
* useEffect(
* () => {
* const subscription = onNotificationReceived(setLastNotification);
* return () => subscription.unsubscribe();
* },
* [setLastNotification]
* );
*
* return <span>{lastNotification}<span>;
* };
*/
const subscribe = channel => callback => (
getConsumer().subscriptions.create(channel, {
received: data => callback(camelize(data))
})
);
export const disconnect = () => {
if (consumer) {
consumer.disconnect();
consumer = null;
}
};
export const onEventStarting = subscribe("EventStartingChannel");
export const onLeaderboardUpdated = subscribe("LeaderboardChannel");
export const onLiveEventCommented = subscribe("LiveEventChannel");
export const onNotificationReceived = subscribe("NotificationChannel");
export const onRecognitionCreated = subscribe("RecognitionChannel");
export const onUserActivityCreated = subscribe("UserActivityChannel");