Skip to content

Commit 9542445

Browse files
committed
feat: Custom handling for Feathers events
Closes #221 This adds the `handleEvents` properties, which let you fine-tune the event handling process per event. ```js handleEvents: { created(item, { model, models }) { return true // truthy adds the record }, patched(item, { model, models }) { return true // truthy adds the record }, updated(item, { model, models }) { return true // truthy adds the record }, removed(item, { model, models }) { return true // truthy removes the record from the store }, } ``` Thanks to @barata for the great idea!
1 parent 461b80d commit 9542445

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/service-module/make-service-plugin.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ eslint
33
@typescript-eslint/explicit-function-return-type: 0,
44
@typescript-eslint/no-explicit-any: 0
55
*/
6-
import { FeathersVuexOptions, MakeServicePluginOptions } from './types'
6+
import {
7+
FeathersVuexOptions,
8+
MakeServicePluginOptions,
9+
HandleEvents
10+
} from './types'
711
import makeServiceModule from './make-service-module'
812
import { globalModels, prepareAddModel } from './global-models'
913
import { makeNamespace, getServicePath, assignIfNotPresent } from '../utils'
@@ -13,13 +17,15 @@ const defaults = {
1317
namespace: '', // The namespace for the Vuex module. Will generally be derived from the service.path, service.name, when available. Otherwise, it must be provided here, explicitly.
1418
nameStyle: 'short', // Determines the source of the module name. 'short', 'path', or 'explicit'
1519
servicePath: '',
20+
handleEvents: {} as HandleEvents,
1621
state: {}, // for custom state
1722
getters: {}, // for custom getters
1823
mutations: {}, // for custom mutations
1924
actions: {}, // for custom actions
2025
instanceDefaults: () => ({}), // Default instanceDefaults returns an empty object
2126
setupInstance: instance => instance // Default setupInstance returns the instance
2227
}
28+
const events = ['created', 'patched', 'updated', 'removed']
2329

2430
/**
2531
* prepare only wraps the makeServicePlugin to provide the globalOptions.
@@ -36,6 +42,12 @@ export default function prepareMakeServicePlugin(
3642
* (3) Setup real-time events
3743
*/
3844
return function makeServicePlugin(config: MakeServicePluginOptions) {
45+
// Setup the event handlers. By default they just return the value of `options.enableEvents`
46+
defaults.handleEvents = events.reduce((obj, eventName) => {
47+
obj[eventName] = () => config.enableEvents || true
48+
return obj
49+
}, {} as HandleEvents)
50+
3951
const options = Object.assign({}, defaults, globalOptions, config)
4052
const {
4153
Model,
@@ -93,30 +105,31 @@ export default function prepareMakeServicePlugin(
93105

94106
// (3^) Setup real-time events
95107
if (options.enableEvents) {
108+
const handleEvent = (eventName, item, mutationName) => {
109+
const affectsStore = options.handleEvents[eventName](item, {
110+
model: Model,
111+
models: globalModels
112+
})
113+
affectsStore &&
114+
store.dispatch(`${options.namespace}/${mutationName}`, item)
115+
}
116+
96117
// Listen to socket events when available.
97118
service.on('created', item => {
98-
store.dispatch(`${options.namespace}/addOrUpdate`, item)
99-
if (Model.emit) {
100-
Model.emit('created', item)
101-
}
119+
handleEvent('created', item, 'addOrUpdate')
120+
Model.emit && Model.emit('created', item)
102121
})
103122
service.on('updated', item => {
104-
store.dispatch(`${options.namespace}/addOrUpdate`, item)
105-
if (Model.emit) {
106-
Model.emit('updated', item)
107-
}
123+
handleEvent('updated', item, 'addOrUpdate')
124+
Model.emit && Model.emit('updated', item)
108125
})
109126
service.on('patched', item => {
110-
store.dispatch(`${options.namespace}/addOrUpdate`, item)
111-
if (Model.emit) {
112-
Model.emit('patched', item)
113-
}
127+
handleEvent('patched', item, 'addOrUpdate')
128+
Model.emit && Model.emit('patched', item)
114129
})
115130
service.on('removed', item => {
116-
store.commit(`${options.namespace}/removeItem`, item)
117-
if (Model.emit) {
118-
Model.emit('removed', item)
119-
}
131+
handleEvent('removed', item, 'removeItem')
132+
Model.emit && Model.emit('removed', item)
120133
})
121134
}
122135
}

src/service-module/service-module.state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default function makeDefaultState(servicePath, options) {
1111
'actions',
1212
'getters',
1313
'instanceDefaults',
14+
'handleEvents',
1415
'Model',
1516
'mutations',
1617
'service',

src/service-module/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface FeathersVuexOptions {
88
autoRemove?: boolean
99
debug?: boolean
1010
enableEvents?: boolean
11+
handleEvents?: HandleEvents
1112
idField?: string
1213
tempIdField?: string
1314
keepCopiesInStore?: boolean
@@ -19,6 +20,13 @@ export interface FeathersVuexOptions {
1920
whitelist?: string[]
2021
}
2122

23+
export interface HandleEvents {
24+
created?: Function
25+
patched?: Function
26+
updated?: Function
27+
removed?: Function
28+
}
29+
2230
export interface MakeServicePluginOptions {
2331
Model: any
2432
service: any

0 commit comments

Comments
 (0)