mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-11-04 04:57:51 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			45 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
import { createConsumer } from '@rails/actioncable';
 | 
						|
 | 
						|
const PRESENCE_INTERVAL = 60000;
 | 
						|
 | 
						|
class BaseActionCableConnector {
 | 
						|
  constructor(app, pubsubToken) {
 | 
						|
    this.consumer = createConsumer();
 | 
						|
    this.subscription = this.consumer.subscriptions.create(
 | 
						|
      {
 | 
						|
        channel: 'RoomChannel',
 | 
						|
        pubsub_token: pubsubToken,
 | 
						|
        account_id: app.$store.getters.getCurrentAccountId,
 | 
						|
        user_id: app.$store.getters.getCurrentUserID,
 | 
						|
      },
 | 
						|
      {
 | 
						|
        updatePresence() {
 | 
						|
          this.perform('update_presence');
 | 
						|
        },
 | 
						|
        received: this.onReceived,
 | 
						|
      }
 | 
						|
    );
 | 
						|
    this.app = app;
 | 
						|
    this.events = {};
 | 
						|
    this.isAValidEvent = () => true;
 | 
						|
 | 
						|
    setInterval(() => {
 | 
						|
      this.subscription.updatePresence();
 | 
						|
    }, PRESENCE_INTERVAL);
 | 
						|
  }
 | 
						|
 | 
						|
  disconnect() {
 | 
						|
    this.consumer.disconnect();
 | 
						|
  }
 | 
						|
 | 
						|
  onReceived = ({ event, data } = {}) => {
 | 
						|
    if (this.isAValidEvent(data)) {
 | 
						|
      if (this.events[event] && typeof this.events[event] === 'function') {
 | 
						|
        this.events[event](data);
 | 
						|
      }
 | 
						|
    }
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
export default BaseActionCableConnector;
 |