Skip to content

Commit 4d8bf21

Browse files
author
Joshua Morris
committed
Add custom pass to store handler
1 parent 649485e commit 4d8bf21

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
This package is [semantic versioned](http://semver.org/)
44

5+
## 2.0.8
6+
- [feature]: custom pass to store logic. Defaults to original passToStore code if no custom logic provided
7+
58
## 2.0.7
69
- [feature]: manual connect/disconnect
710

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Vue.use(VueNativeSock, 'ws://localhost:9090', { store: store, format: 'json' })
5454
Enable ws reconnect automatically:
5555

5656
``` js
57-
Vue.use(VueNativeSock, 'ws://localhost:9090', {
57+
Vue.use(VueNativeSock, 'ws://localhost:9090', {
5858
reconnection: true, // (Boolean) whether to reconnect automatically (false)
5959
reconnectionAttempts: 5, // (Number) number of reconnection attempts before giving up (Infinity),
6060
reconnectionDelay: 3000, // (Number) how long to initially wait before attempting a new (1000)
@@ -189,6 +189,50 @@ actions: {
189189

190190
Use the `.sendObj({some: data})` method on the `$socket` object to send stringified json messages.
191191

192+
##### Custom socket event handling
193+
194+
Provide you own custom code to handle events received via the `passToStoreHandler` option. The function you provide will be passed the following arguments:
195+
196+
1. event name
197+
2. event
198+
3. original/default handler code function `function (eventName, event)`. This allows you to optionally do some basic preprocessing before handing the event over to the original handler.
199+
200+
The original passToStore code is used if no `passToStoreHandler` is configured.
201+
202+
Here is an example of passing in a custom handler. This has the original passToStore code to give you an example of what you can do:
203+
204+
``` js
205+
Vue.use(VueNativeSock, 'ws://localhost:9090', {
206+
passToStoreHandler: function (eventName, event) {
207+
if (!eventName.startsWith('SOCKET_')) { return }
208+
let method = 'commit'
209+
let target = eventName.toUpperCase()
210+
let msg = event
211+
if (this.format === 'json' && event.data) {
212+
msg = JSON.parse(event.data)
213+
if (msg.mutation) {
214+
target = [msg.namespace || '', msg.mutation].filter((e) => !!e).join('/')
215+
} else if (msg.action) {
216+
method = 'dispatch'
217+
target = [msg.namespace || '', msg.action].filter((e) => !!e).join('/')
218+
}
219+
}
220+
this.store[method](target, msg)
221+
}
222+
})
223+
```
224+
225+
Here is an example of do some preprocessing, then pass the event onto the original handler code:
226+
227+
``` js
228+
Vue.use(VueNativeSock, 'ws://localhost:9090', {
229+
passToStoreHandler: function (eventName, event, next) {
230+
event.data = event.should_have_been_named_data
231+
next(eventName, event)
232+
}
233+
})
234+
```
235+
192236
## Examples
193237

194238
TODO: post your example here!

dist/build.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-native-websocket",
3-
"version": "2.0.7",
3+
"version": "2.0.8",
44
"description": "native websocket implemantation for vuejs and vuex",
55
"main": "dist/build.js",
66
"scripts": {

src/Observer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default class {
1212
this.reconnectTimeoutId = 0
1313
this.reconnectionCount = 0
1414

15+
this.passToStoreHandler = this.opts.passToStoreHandler || false
16+
1517
this.connect(connectionUrl, opts)
1618

1719
if (opts.store) { this.store = opts.store }
@@ -61,6 +63,14 @@ export default class {
6163
}
6264

6365
passToStore (eventName, event) {
66+
if (this.passToStoreHandler) {
67+
this.passToStoreHandler(eventName, event, this.defaultPassToStore)
68+
} else {
69+
this.defaultPassToStore(eventName, event)
70+
}
71+
}
72+
73+
defaultPassToStore (eventName, event) {
6474
if (!eventName.startsWith('SOCKET_')) { return }
6575
let method = 'commit'
6676
let target = eventName.toUpperCase()

0 commit comments

Comments
 (0)