Skip to content

Commit a7325c7

Browse files
committed
Add support for vuex actions
1 parent 4298712 commit a7325c7

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

src/Observer.js

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Emitter from './Emitter'
22

3+
const commit = 'commit'
4+
const dispatch = 'dispatch'
5+
36
export default class {
4-
constructor (connectionUrl, opts = {}) {
7+
constructor(connectionUrl, opts = {}) {
58
this.format = opts.format && opts.format.toLowerCase()
69

710
if (connectionUrl.startsWith('//')) {
@@ -22,80 +25,105 @@ export default class {
2225

2326
this.connect(connectionUrl, opts)
2427

25-
if (opts.store) { this.store = opts.store }
26-
if (opts.mutations) { this.mutations = opts.mutations }
28+
if (opts.store) {
29+
this.store = opts.store
30+
if ((opts.storeMethodType && opts.storeMethodType == dispatch) || opts.storeMethodType === commit) {
31+
this.storeMethodType = opts.storeMethodType
32+
} else {
33+
this.storeMethodType = commit
34+
}
35+
}
36+
if (opts.methods) {
37+
this.methods = opts.methods
38+
}
39+
if (opts.mutations) {
40+
console.warn('vue-native-websocket plugin: mutations will be deprecated, please switch to methods')
41+
this.methods = opts.mutations
42+
}
2743
this.onEvent()
2844
}
2945

30-
connect (connectionUrl, opts = {}) {
46+
connect(connectionUrl, opts = {}) {
3147
let protocol = opts.protocol || ''
32-
this.WebSocket = opts.WebSocket || (protocol === '' ? new WebSocket(connectionUrl) : new WebSocket(connectionUrl, protocol))
48+
this.WebSocket =
49+
opts.WebSocket || (protocol === '' ? new WebSocket(connectionUrl) : new WebSocket(connectionUrl, protocol))
3350
if (this.format === 'json') {
3451
if (!('sendObj' in this.WebSocket)) {
35-
this.WebSocket.sendObj = (obj) => this.WebSocket.send(JSON.stringify(obj))
52+
this.WebSocket.sendObj = obj => this.WebSocket.send(JSON.stringify(obj))
3653
}
3754
}
3855

3956
return this.WebSocket
4057
}
4158

42-
reconnect () {
59+
reconnect() {
4360
if (this.reconnectionCount <= this.reconnectionAttempts) {
4461
this.reconnectionCount++
4562
clearTimeout(this.reconnectTimeoutId)
4663

4764
this.reconnectTimeoutId = setTimeout(() => {
48-
if (this.store) { this.passToStore('SOCKET_RECONNECT', this.reconnectionCount) }
65+
if (this.store) {
66+
this.passToStore('SOCKET_RECONNECT', this.reconnectionCount)
67+
}
4968

5069
this.connect(this.connectionUrl, this.opts)
5170
this.onEvent()
5271
}, this.reconnectionDelay)
5372
} else {
54-
if (this.store) { this.passToStore('SOCKET_RECONNECT_ERROR', true) }
73+
if (this.store) {
74+
this.passToStore('SOCKET_RECONNECT_ERROR', true)
75+
}
5576
}
5677
}
5778

58-
onEvent () {
59-
['onmessage', 'onclose', 'onerror', 'onopen'].forEach((eventType) => {
60-
this.WebSocket[eventType] = (event) => {
79+
onEvent() {
80+
;['onmessage', 'onclose', 'onerror', 'onopen'].forEach(eventType => {
81+
this.WebSocket[eventType] = event => {
6182
Emitter.emit(eventType, event)
6283

63-
if (this.store) { this.passToStore('SOCKET_' + eventType, event) }
84+
if (this.store) {
85+
this.passToStore('SOCKET_' + eventType, event)
86+
}
6487

6588
if (this.reconnection && eventType === 'onopen') {
6689
this.opts.$setInstance(event.currentTarget)
6790
this.reconnectionCount = 0
6891
}
6992

70-
if (this.reconnection && eventType === 'onclose') { this.reconnect() }
93+
if (this.reconnection && eventType === 'onclose') {
94+
this.reconnect()
95+
}
7196
}
7297
})
7398
}
7499

75-
passToStore (eventName, event) {
100+
passToStore(eventName, event) {
76101
if (this.passToStoreHandler) {
77102
this.passToStoreHandler(eventName, event, this.defaultPassToStore.bind(this))
78103
} else {
79104
this.defaultPassToStore(eventName, event)
80105
}
81106
}
82107

83-
defaultPassToStore (eventName, event) {
84-
if (!eventName.startsWith('SOCKET_')) { return }
85-
let method = 'commit'
108+
defaultPassToStore(eventName, event) {
109+
if (!eventName.startsWith('SOCKET_')) {
110+
return
111+
}
112+
let method = this.storeMethodType
86113
let target = eventName.toUpperCase()
87114
let msg = event
88115
if (this.format === 'json' && event.data) {
89116
msg = JSON.parse(event.data)
90117
if (msg.mutation) {
91-
target = [msg.namespace || '', msg.mutation].filter((e) => !!e).join('/')
118+
method = commit
119+
target = [msg.namespace || '', msg.mutation].filter(e => !!e).join('/')
92120
} else if (msg.action) {
93-
method = 'dispatch'
94-
target = [msg.namespace || '', msg.action].filter((e) => !!e).join('/')
121+
method = dispatch
122+
target = [msg.namespace || '', msg.action].filter(e => !!e).join('/')
95123
}
96124
}
97-
if (this.mutations) {
98-
target = this.mutations[target] || target
125+
if (this.methods) {
126+
target = this.methods[target] || target
99127
}
100128
this.store[method](target, msg)
101129
}

0 commit comments

Comments
 (0)