Skip to content

Commit ca2d385

Browse files
committed
Move event handling to its own module
1 parent a8cba54 commit ca2d385

File tree

2 files changed

+85
-86
lines changed

2 files changed

+85
-86
lines changed

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

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ eslint
66
import { FeathersVuexOptions, MakeServicePluginOptions } from './types'
77
import makeServiceModule from './make-service-module'
88
import { globalModels, prepareAddModel } from './global-models'
9-
import {
10-
makeNamespace,
11-
getServicePath,
12-
assignIfNotPresent,
13-
getId
14-
} from '../utils'
9+
import { enableServiceEvents } from './service-module.events'
10+
import { makeNamespace, getServicePath, assignIfNotPresent } from '../utils'
1511
import _get from 'lodash/get'
16-
import _debounce from 'lodash/debounce'
1712

1813
interface ServiceOptionsDefaults {
1914
servicePath: string
@@ -124,87 +119,9 @@ export default function prepareMakeServicePlugin(
124119
}
125120
addModel(Model)
126121

127-
const debounceMap = {
128-
addOrUpdateById: {},
129-
removeItemById: {},
130-
addOrUpdate(item) {
131-
const id = getId(item, options.idField)
132-
this.addOrUpdateById[id] = item
133-
if (this.removeItemById.hasOwnProperty(id)) {
134-
delete this.removeItemById[id]
135-
}
136-
this.debouncedAddOrUpdate()
137-
},
138-
removeItem(item) {
139-
const id = getId(item, options.idField)
140-
this.removeItemById[id] = item
141-
if (this.addOrUpdateById.hasOwnProperty(id)) {
142-
delete this.addOrUpdateById[id]
143-
}
144-
this.debouncedRremoveItem()
145-
},
146-
debouncedAddOrUpdate: _debounce(async function () {
147-
const values = Object.values(this.addOrUpdateById)
148-
if (values.length === 0) return
149-
await store.dispatch(`${options.namespace}/addOrUpdateList`, {
150-
data: values,
151-
disableRemove: true
152-
})
153-
this.addOrUpdateById = {}
154-
}, options.debounceEventsTime || 20),
155-
debouncedRremoveItem: _debounce(function () {
156-
const values = Object.values(this.removeItemById)
157-
if (values.length === 0) return
158-
store.commit(`${options.namespace}/removeItems`, values)
159-
this.removeItemById = {}
160-
}, options.debounceEventsTime || 20)
161-
}
162-
163122
// (3^) Setup real-time events
164123
if (options.enableEvents) {
165-
const handleEvent = (eventName, item, mutationName) => {
166-
const handler = options.handleEvents[eventName]
167-
const confirmOrArray = handler(item, {
168-
model: Model,
169-
models: globalModels
170-
})
171-
const [affectsStore, modified = item] = Array.isArray(confirmOrArray)
172-
? confirmOrArray
173-
: [confirmOrArray]
174-
if (affectsStore) {
175-
if (!options.debounceEventsTime) {
176-
eventName === 'removed'
177-
? store.commit(`${options.namespace}/removeItem`, modified)
178-
: store.dispatch(
179-
`${options.namespace}/${mutationName}`,
180-
modified
181-
)
182-
} else {
183-
const id = getId(item, options.idField)
184-
eventName === 'removed'
185-
? debounceMap.removeItem(item)
186-
: debounceMap.addOrUpdate(item)
187-
}
188-
}
189-
}
190-
191-
// Listen to socket events when available.
192-
service.on('created', item => {
193-
handleEvent('created', item, 'addOrUpdate')
194-
Model.emit && Model.emit('created', item)
195-
})
196-
service.on('updated', item => {
197-
handleEvent('updated', item, 'addOrUpdate')
198-
Model.emit && Model.emit('updated', item)
199-
})
200-
service.on('patched', item => {
201-
handleEvent('patched', item, 'addOrUpdate')
202-
Model.emit && Model.emit('patched', item)
203-
})
204-
service.on('removed', item => {
205-
handleEvent('removed', item, 'removeItem')
206-
Model.emit && Model.emit('removed', item)
207-
})
124+
enableServiceEvents({ service, Model, store, options })
208125
}
209126
}
210127
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { getId } from '../utils'
2+
import _debounce from 'lodash/debounce'
3+
import { globalModels } from './global-models'
4+
5+
export function enableServiceEvents({ service, Model, store, options }): void {
6+
const debounceMap = {
7+
addOrUpdateById: {},
8+
removeItemById: {},
9+
addOrUpdate(item): void {
10+
const id = getId(item, options.idField)
11+
this.addOrUpdateById[id] = item
12+
if (this.removeItemById.hasOwnProperty(id)) {
13+
delete this.removeItemById[id]
14+
}
15+
this.debouncedAddOrUpdate()
16+
},
17+
removeItem(item): void {
18+
const id = getId(item, options.idField)
19+
this.removeItemById[id] = item
20+
if (this.addOrUpdateById.hasOwnProperty(id)) {
21+
delete this.addOrUpdateById[id]
22+
}
23+
this.debouncedRemoveItem()
24+
},
25+
debouncedAddOrUpdate: _debounce(async function () {
26+
const values = Object.values(this.addOrUpdateById)
27+
if (values.length === 0) return
28+
await store.dispatch(`${options.namespace}/addOrUpdateList`, {
29+
data: values,
30+
disableRemove: true
31+
})
32+
this.addOrUpdateById = {}
33+
}, options.debounceEventsTime || 20),
34+
debouncedRemoveItem: _debounce(function () {
35+
const values = Object.values(this.removeItemById)
36+
if (values.length === 0) return
37+
store.commit(`${options.namespace}/removeItems`, values)
38+
this.removeItemById = {}
39+
}, options.debounceEventsTime || 20)
40+
}
41+
42+
const handleEvent = (eventName, item, mutationName): void => {
43+
const handler = options.handleEvents[eventName]
44+
const confirmOrArray = handler(item, {
45+
model: Model,
46+
models: globalModels
47+
})
48+
const [affectsStore, modified = item] = Array.isArray(confirmOrArray)
49+
? confirmOrArray
50+
: [confirmOrArray]
51+
if (affectsStore) {
52+
if (!options.debounceEventsTime) {
53+
eventName === 'removed'
54+
? store.commit(`${options.namespace}/removeItem`, modified)
55+
: store.dispatch(`${options.namespace}/${mutationName}`, modified)
56+
} else {
57+
const id = getId(item, options.idField)
58+
eventName === 'removed'
59+
? debounceMap.removeItem(item)
60+
: debounceMap.addOrUpdate(item)
61+
}
62+
}
63+
}
64+
65+
// Listen to socket events when available.
66+
service.on('created', item => {
67+
handleEvent('created', item, 'addOrUpdate')
68+
Model.emit && Model.emit('created', item)
69+
})
70+
service.on('updated', item => {
71+
handleEvent('updated', item, 'addOrUpdate')
72+
Model.emit && Model.emit('updated', item)
73+
})
74+
service.on('patched', item => {
75+
handleEvent('patched', item, 'addOrUpdate')
76+
Model.emit && Model.emit('patched', item)
77+
})
78+
service.on('removed', item => {
79+
handleEvent('removed', item, 'removeItem')
80+
Model.emit && Model.emit('removed', item)
81+
})
82+
}

0 commit comments

Comments
 (0)