Skip to content

Add support for vuex actions in defaultPassToStore #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/Observer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Emitter from './Emitter'

const commit = 'commit'
const dispatch = 'dispatch'

export default class {
constructor (connectionUrl, opts = {}) {
this.format = opts.format && opts.format.toLowerCase()
Expand All @@ -22,8 +25,19 @@ export default class {

this.connect(connectionUrl, opts)

if (opts.store) { this.store = opts.store }
if (opts.mutations) { this.mutations = opts.mutations }
if (opts.store) {
this.store = opts.store
if ((opts.storeMethodType && opts.storeMethodType == dispatch) || opts.storeMethodType === commit) {
this.storeMethodType = opts.storeMethodType
} else {
this.storeMethodType = commit
}
}
if (opts.mutations) {
console.warn('vue-native-websocket plugin: mutations will be deprecated, please switch to methods')
this.methods = opts.mutations
}
if (opts.methods) { this.methods = opts.methods }
this.onEvent()
}

Expand Down Expand Up @@ -82,20 +96,21 @@ export default class {

defaultPassToStore (eventName, event) {
if (!eventName.startsWith('SOCKET_')) { return }
let method = 'commit'
let method = this.storeMethodType
let target = eventName.toUpperCase()
let msg = event
if (this.format === 'json' && event.data) {
msg = JSON.parse(event.data)
if (msg.mutation) {
method = commit
target = [msg.namespace || '', msg.mutation].filter((e) => !!e).join('/')
} else if (msg.action) {
method = 'dispatch'
method = dispatch
target = [msg.namespace || '', msg.action].filter((e) => !!e).join('/')
}
}
if (this.mutations) {
target = this.mutations[target] || target
if (this.methods) {
target = this.methods[target] || target
}
this.store[method](target, msg)
}
Expand Down
32 changes: 32 additions & 0 deletions test/unit/specs/Observer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ describe('Observer.js', () => {
}, 100)
})

// TODO: DRY
it('passes an action to the provided vuex store', (done) => {
let expectedMsg = 'hello world'
let mockStore = sinon.mock({
commit: () => {},
dispatch: (event) => {
expect(event.data == expectedMsg)
}
})
mockStore.expects('dispatch').withArgs('SOCKET_ONOPEN')
mockStore.expects('dispatch').withArgs('SOCKET_ONMESSAGE')

mockServer = new Server(wsUrl)
mockServer.on('connection', ws => {
ws.send(expectedMsg)
})

Vue.use(VueNativeSock, wsUrl)
let vm = new Vue()
observer = new Observer(wsUrl, {
store: mockStore.object,
storeMethodType: 'dispatch',
websocket: new WebSocket(wsUrl)
})

setTimeout(() => {
mockStore.verify()
mockServer.stop(done)
}, 100)
})


// TODO: DRY
it('passes a namespaced json commit to the provided vuex store', (done) => {
let expectedMsg = { namespace: 'users', mutation: 'setName', value: 'steve' }
Expand Down