2019-10-29 07:20:54 +00:00
|
|
|
import { createConsumer } from '@rails/actioncable';
|
|
|
|
|
|
|
|
class BaseActionCableConnector {
|
|
|
|
constructor(app, pubsubToken) {
|
2020-04-03 07:34:58 +00:00
|
|
|
this.consumer = createConsumer();
|
|
|
|
this.consumer.subscriptions.create(
|
2019-10-29 07:20:54 +00:00
|
|
|
{
|
|
|
|
channel: 'RoomChannel',
|
|
|
|
pubsub_token: pubsubToken,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
received: this.onReceived,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
this.app = app;
|
|
|
|
this.events = {};
|
2020-05-26 17:08:48 +00:00
|
|
|
this.isAValidEvent = () => true;
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
disconnect() {
|
|
|
|
this.consumer.disconnect();
|
|
|
|
}
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
onReceived = ({ event, data } = {}) => {
|
2020-05-26 17:08:48 +00:00
|
|
|
if (this.isAValidEvent(data)) {
|
|
|
|
if (this.events[event] && typeof this.events[event] === 'function') {
|
|
|
|
this.events[event](data);
|
|
|
|
}
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BaseActionCableConnector;
|