Skip to content

Commit 42086c3

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

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/Observer.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import Emitter from './Emitter'
22

3+
const commit = 'commit'
4+
const dispatch = 'dispatch'
5+
36
export default class {
47
constructor (connectionUrl, opts = {}) {
58
this.format = opts.format && opts.format.toLowerCase()
@@ -22,8 +25,18 @@ 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+
if ((opts.storeMethodType && opts.storeMethodType == dispatch) || opts.storeMethodType === commit) {
30+
this.storeMethodType = opts.storeMethodType
31+
} else {
32+
this.storeMethodType = commit
33+
}
34+
}
35+
if (opts.mutations) {
36+
console.warn('vue-native-websocket plugin: mutations will be deprecated, please switch to methods')
37+
this.methods = opts.mutations
38+
}
39+
if (opts.methods) { this.methods = opts.methods }
2740
this.onEvent()
2841
}
2942

@@ -82,21 +95,23 @@ export default class {
8295

8396
defaultPassToStore (eventName, event) {
8497
if (!eventName.startsWith('SOCKET_')) { return }
85-
let method = 'commit'
98+
let method = this.storeMethodType
8699
let target = eventName.toUpperCase()
87100
let msg = event
88101
if (this.format === 'json' && event.data) {
89102
msg = JSON.parse(event.data)
90103
if (msg.mutation) {
104+
method = commit
91105
target = [msg.namespace || '', msg.mutation].filter((e) => !!e).join('/')
92106
} else if (msg.action) {
93-
method = 'dispatch'
107+
method = dispatch
94108
target = [msg.namespace || '', msg.action].filter((e) => !!e).join('/')
95109
}
96110
}
97-
if (this.mutations) {
98-
target = this.mutations[target] || target
111+
if (this.methods) {
112+
target = this.methods[target] || target
99113
}
100114
this.store[method](target, msg)
101115
}
102116
}
117+
}

0 commit comments

Comments
 (0)