|
| 1 | +/* eslint-disable*/ |
| 2 | +import Emitter from './Emitter' |
| 3 | + |
| 4 | +export default class { |
| 5 | + constructor(connectionUrl, opts = {}) { |
| 6 | + this.format = opts.format && opts.format.toLowerCase() |
| 7 | + |
| 8 | + if (connectionUrl.startsWith('//')) { |
| 9 | + const scheme = window.location.protocol === 'https:' ? 'wss' : 'ws' |
| 10 | + connectionUrl = `${scheme}:${connectionUrl}` |
| 11 | + } |
| 12 | + |
| 13 | + this.connectionUrl = connectionUrl |
| 14 | + this.opts = opts |
| 15 | + |
| 16 | + this.reconnection = this.opts.reconnection || false |
| 17 | + this.reconnectionAttempts = this.opts.reconnectionAttempts || Infinity |
| 18 | + this.reconnectionDelay = this.opts.reconnectionDelay || 1000 |
| 19 | + this.reconnectTimeoutId = 0 |
| 20 | + this.reconnectionCount = 0 |
| 21 | + |
| 22 | + this.passToStoreHandler = this.opts.passToStoreHandler || false |
| 23 | + |
| 24 | + this.connect(connectionUrl, opts) |
| 25 | + |
| 26 | + if (opts.store) { |
| 27 | + this.store = opts.store |
| 28 | + } |
| 29 | + if (opts.mutations) { |
| 30 | + this.mutations = opts.mutations |
| 31 | + } |
| 32 | + this.onEvent() |
| 33 | + } |
| 34 | + |
| 35 | + connect(connectionUrl, opts = {}) { |
| 36 | + const protocol = opts.protocol || '' |
| 37 | + this.WebSocket = |
| 38 | + opts.WebSocket || (protocol === '' ? new WebSocket(connectionUrl) : new WebSocket(connectionUrl, protocol)) |
| 39 | + if (this.format === 'json') { |
| 40 | + if (!('sendObj' in this.WebSocket)) { |
| 41 | + this.WebSocket.sendObj = obj => this.WebSocket.send(JSON.stringify(obj)) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return this.WebSocket |
| 46 | + } |
| 47 | + |
| 48 | + reconnect() { |
| 49 | + if (this.reconnectionCount <= this.reconnectionAttempts) { |
| 50 | + this.reconnectionCount++ |
| 51 | + clearTimeout(this.reconnectTimeoutId) |
| 52 | + |
| 53 | + this.reconnectTimeoutId = setTimeout(() => { |
| 54 | + if (this.store) { |
| 55 | + this.passToStore('SOCKET_RECONNECT', this.reconnectionCount) |
| 56 | + } |
| 57 | + |
| 58 | + this.connect(this.connectionUrl, this.opts) |
| 59 | + this.onEvent() |
| 60 | + }, this.reconnectionDelay) |
| 61 | + } else if (this.store) { |
| 62 | + this.passToStore('SOCKET_RECONNECT_ERROR', true) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + onEvent() { |
| 67 | + ;['onmessage', 'onclose', 'onerror', 'onopen'].forEach(eventType => { |
| 68 | + this.WebSocket[eventType] = event => { |
| 69 | + Emitter.emit(eventType, event) |
| 70 | + |
| 71 | + if (this.store) { |
| 72 | + this.passToStore(`SOCKET_${eventType}`, event) |
| 73 | + } |
| 74 | + |
| 75 | + if (this.reconnection && eventType === 'onopen') { |
| 76 | + this.opts.$setInstance(event.currentTarget) |
| 77 | + this.reconnectionCount = 0 |
| 78 | + } |
| 79 | + |
| 80 | + if (this.reconnection && eventType === 'onclose') { |
| 81 | + this.reconnect() |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + passToStore(eventName, event) { |
| 88 | + if (this.passToStoreHandler) { |
| 89 | + this.passToStoreHandler(eventName, event, this.defaultPassToStore.bind(this)) |
| 90 | + } else { |
| 91 | + this.defaultPassToStore(eventName, event) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + defaultPassToStore(eventName, event) { |
| 96 | + if (!eventName.startsWith('SOCKET_')) { |
| 97 | + return |
| 98 | + } |
| 99 | + let method = 'commit' |
| 100 | + let target = eventName.toUpperCase() |
| 101 | + let msg = event |
| 102 | + if (this.format === 'json' && event.data) { |
| 103 | + msg = JSON.parse(event.data) |
| 104 | + if (msg.mutation) { |
| 105 | + target = [msg.namespace || '', msg.mutation].filter(e => !!e).join('/') |
| 106 | + } else if (msg.action) { |
| 107 | + method = 'dispatch' |
| 108 | + target = [msg.namespace || '', msg.action].filter(e => !!e).join('/') |
| 109 | + } |
| 110 | + } |
| 111 | + if (this.mutations) { |
| 112 | + target = this.mutations[target] || target |
| 113 | + } |
| 114 | + this.store[method](target, msg) |
| 115 | + } |
| 116 | +} |
0 commit comments