|
6 | 6 | import { FeathersVuexOptions, MakeServicePluginOptions } from './types'
|
7 | 7 | import makeServiceModule from './make-service-module'
|
8 | 8 | 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' |
15 | 11 | import _get from 'lodash/get'
|
16 |
| -import _debounce from 'lodash/debounce' |
17 | 12 |
|
18 | 13 | interface ServiceOptionsDefaults {
|
19 | 14 | servicePath: string
|
@@ -124,87 +119,9 @@ export default function prepareMakeServicePlugin(
|
124 | 119 | }
|
125 | 120 | addModel(Model)
|
126 | 121 |
|
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 |
| - |
163 | 122 | // (3^) Setup real-time events
|
164 | 123 | 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 }) |
208 | 125 | }
|
209 | 126 | }
|
210 | 127 | }
|
|
0 commit comments