Skip to content

Commit 900f623

Browse files
committed
feat: debounceEvents
1 parent 7beed94 commit 900f623

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

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

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

1218
interface ServiceOptionsDefaults {
1319
servicePath: string
@@ -118,6 +124,42 @@ export default function prepareMakeServicePlugin(
118124
}
119125
addModel(Model)
120126

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+
121163
// (3^) Setup real-time events
122164
if (options.enableEvents) {
123165
const handleEvent = (eventName, item, mutationName) => {
@@ -130,9 +172,19 @@ export default function prepareMakeServicePlugin(
130172
? confirmOrArray
131173
: [confirmOrArray]
132174
if (affectsStore) {
133-
eventName === 'removed'
134-
? store.commit(`${options.namespace}/removeItem`, modified)
135-
: store.dispatch(`${options.namespace}/${mutationName}`, modified)
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+
}
136188
}
137189
}
138190

0 commit comments

Comments
 (0)