|
| 1 | +import io from 'socket.io-client'; |
| 2 | +import camelCase from 'camelcase'; |
| 3 | +import SocketEmitter from './emitter'; |
| 4 | + |
| 5 | +const SYSTEM_EVENTS = [ |
| 6 | + 'connect', |
| 7 | + 'error', |
| 8 | + 'disconnect', |
| 9 | + 'reconnect', |
| 10 | + 'reconnect_attempt', |
| 11 | + 'reconnecting', |
| 12 | + 'reconnect_error', |
| 13 | + 'reconnect_failed', |
| 14 | + 'connect_error', |
| 15 | + 'connect_timeout', |
| 16 | + 'connecting', |
| 17 | + 'ping', |
| 18 | + 'pong', |
| 19 | +]; |
| 20 | + |
| 21 | +export default class SocketioConnector { |
| 22 | + constructor (store = null) { |
| 23 | + this.socket = null; |
| 24 | + this.store = store; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Initialize a new Socket connection. |
| 29 | + * |
| 30 | + * @param {io} socket |
| 31 | + */ |
| 32 | + connect (socket) { |
| 33 | + if (!socket instanceof io) { |
| 34 | + throw new Error('First argument passed to connect() should be an instance of socket.io-client.'); |
| 35 | + } |
| 36 | + |
| 37 | + this.socket = socket; |
| 38 | + this.registerEventListeners(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Return the socket instance we are working with. |
| 43 | + * |
| 44 | + * @return {io|null} |
| 45 | + */ |
| 46 | + instance () { |
| 47 | + return this.socket; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Register the event listeners for this socket including user-defined ones in the store as |
| 52 | + * well as global system events from Socekt.io. |
| 53 | + */ |
| 54 | + registerEventListeners () { |
| 55 | + this.socket['onevent'] = (packet) => { |
| 56 | + const [event, ...args] = packet.data; |
| 57 | + SocketEmitter.emit(event, ...args); |
| 58 | + this.passToStore(event, args); |
| 59 | + }; |
| 60 | + |
| 61 | + SYSTEM_EVENTS.forEach((event) => { |
| 62 | + this.socket.on(event, (payload) => { |
| 63 | + SocketEmitter.emit(event, payload); |
| 64 | + this.passToStore(event, payload); |
| 65 | + }) |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Pass event calls off to the Vuex store if there is a corresponding function. |
| 71 | + * |
| 72 | + * @param {String|Number|Symbol} event |
| 73 | + * @param {Array} payload |
| 74 | + */ |
| 75 | + passToStore (event, payload) { |
| 76 | + if (!this.store) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const mutation = `SOCKET_${event.toUpperCase()}`; |
| 81 | + const action = `socket_${camelCase(event)}`; |
| 82 | + |
| 83 | + Object.keys(this.store._mutations).filter((namespaced) => { |
| 84 | + return namespaced.split('/').pop() === mutation; |
| 85 | + }).forEach((namespaced) => { |
| 86 | + this.store.commit(namespaced, this.unwrap(payload)); |
| 87 | + }); |
| 88 | + |
| 89 | + Object.keys(this.store._actions).filter((namespaced) => { |
| 90 | + return namespaced.split('/').pop() === action; |
| 91 | + }).forEach((namespaced) => { |
| 92 | + this.store.dispatch(namespaced, this.unwrap(payload)); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param {Array} args |
| 98 | + * @return {Array<Object>|Object} |
| 99 | + */ |
| 100 | + unwrap (args) { |
| 101 | + return (args && args.length <= 1) ? args[0] : args; |
| 102 | + } |
| 103 | +} |
0 commit comments