Skip to content

Commit 068181a

Browse files
committed
feat: add maxWait for debounce
- add throttle functionality for intense applications - eg. a chat, where much messages will be created - not relevant for most applications
1 parent d37967a commit 068181a

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ eslint
66
import { FeathersVuexOptions, MakeServicePluginOptions } from './types'
77
import makeServiceModule from './make-service-module'
88
import { globalModels, prepareAddModel } from './global-models'
9-
import { enableServiceEvents } from './service-module.events'
9+
import enableServiceEvents from './service-module.events'
1010
import { makeNamespace, getServicePath, assignIfNotPresent } from '../utils'
1111
import _get from 'lodash/get'
1212

@@ -19,6 +19,7 @@ interface ServiceOptionsDefaults {
1919
actions: {}
2020
instanceDefaults: () => {}
2121
setupInstance: (instance: {}) => {}
22+
debounceEventsMaxWait: number
2223
}
2324

2425
const defaults: ServiceOptionsDefaults = {
@@ -29,7 +30,8 @@ const defaults: ServiceOptionsDefaults = {
2930
mutations: {}, // for custom mutations
3031
actions: {}, // for custom actions
3132
instanceDefaults: () => ({}), // Default instanceDefaults returns an empty object
32-
setupInstance: instance => instance // Default setupInstance returns the instance
33+
setupInstance: instance => instance, // Default setupInstance returns the instance
34+
debounceEventsMaxWait: 1000
3335
}
3436
const events = ['created', 'patched', 'updated', 'removed']
3537

src/service-module/service-module.events.ts

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,63 @@ import { getId } from '../utils'
22
import _debounce from 'lodash/debounce'
33
import { globalModels } from './global-models'
44

5-
export function enableServiceEvents({ service, Model, store, options }): void {
6-
const debounceMap = {
5+
export interface ServiceEventsDebouncedQueue {
6+
addOrUpdateById: {}
7+
removeItemById: {}
8+
enqueueAddOrUpdate(item: any): void
9+
enqueueRemoval(item: any): void
10+
flushAddOrUpdateQueue(): void
11+
flushRemoveItemQueue(): void
12+
}
13+
14+
export default function enableServiceEvents({
15+
service,
16+
Model,
17+
store,
18+
options
19+
}): ServiceEventsDebouncedQueue {
20+
const debouncedQueue: ServiceEventsDebouncedQueue = {
721
addOrUpdateById: {},
822
removeItemById: {},
9-
queueAddOrUpdate(item): void {
23+
enqueueAddOrUpdate(item): void {
1024
const id = getId(item, options.idField)
1125
this.addOrUpdateById[id] = item
1226
if (this.removeItemById.hasOwnProperty(id)) {
1327
delete this.removeItemById[id]
1428
}
1529
this.flushAddOrUpdateQueue()
1630
},
17-
queueRemoval(item): void {
31+
enqueueRemoval(item): void {
1832
const id = getId(item, options.idField)
1933
this.removeItemById[id] = item
2034
if (this.addOrUpdateById.hasOwnProperty(id)) {
2135
delete this.addOrUpdateById[id]
2236
}
2337
this.flushRemoveItemQueue()
2438
},
25-
flushAddOrUpdateQueue: _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-
flushRemoveItemQueue: _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)
39+
flushAddOrUpdateQueue: _debounce(
40+
async function () {
41+
const values = Object.values(this.addOrUpdateById)
42+
if (values.length === 0) return
43+
await store.dispatch(`${options.namespace}/addOrUpdateList`, {
44+
data: values,
45+
disableRemove: true
46+
})
47+
this.addOrUpdateById = {}
48+
},
49+
options.debounceEventsTime || 20,
50+
{ maxWait: options.debounceEventsMaxWait }
51+
),
52+
flushRemoveItemQueue: _debounce(
53+
function () {
54+
const values = Object.values(this.removeItemById)
55+
if (values.length === 0) return
56+
store.commit(`${options.namespace}/removeItems`, values)
57+
this.removeItemById = {}
58+
},
59+
options.debounceEventsTime || 20,
60+
{ maxWait: options.debounceEventsMaxWait }
61+
)
4062
}
4163

4264
const handleEvent = (eventName, item, mutationName): void => {
@@ -55,8 +77,8 @@ export function enableServiceEvents({ service, Model, store, options }): void {
5577
: store.dispatch(`${options.namespace}/${mutationName}`, modified)
5678
} else {
5779
eventName === 'removed'
58-
? debounceMap.queueRemoval(item)
59-
: debounceMap.queueAddOrUpdate(item)
80+
? debouncedQueue.enqueueRemoval(item)
81+
: debouncedQueue.enqueueAddOrUpdate(item)
6082
}
6183
}
6284
}
@@ -78,4 +100,6 @@ export function enableServiceEvents({ service, Model, store, options }): void {
78100
handleEvent('removed', item, 'removeItem')
79101
Model.emit && Model.emit('removed', item)
80102
})
103+
104+
return debouncedQueue
81105
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export interface ServiceState<M extends Model = Model> {
7979
}
8080
modelName?: string
8181
debounceEventsTime: number
82+
debounceEventsMaxWait: number
8283
}
8384

8485
export interface PaginationState {

src/service-module/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface FeathersVuexOptions {
2121
tempIdField?: string
2222
keepCopiesInStore?: boolean
2323
debounceEventsTime?: number
24+
debounceEventsMaxWait?: number
2425
nameStyle?: string
2526
paramsForServer?: string[]
2627
preferUpdate?: boolean
@@ -52,6 +53,7 @@ export interface MakeServicePluginOptions {
5253
skipRequestIfExists?: boolean
5354
nameStyle?: string
5455
debounceEventsTime?: number
56+
debounceEventsMaxWait?: number
5557

5658
servicePath?: string
5759
namespace?: string

0 commit comments

Comments
 (0)