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 { makeNamespace , getServicePath , assignIfNotPresent } from '../utils'
9
+ import {
10
+ makeNamespace ,
11
+ getServicePath ,
12
+ assignIfNotPresent ,
13
+ getId
14
+ } from '../utils'
10
15
import _get from 'lodash/get'
16
+ import _debounce from 'lodash/debounce'
11
17
12
18
interface ServiceOptionsDefaults {
13
19
servicePath : string
@@ -118,6 +124,42 @@ export default function prepareMakeServicePlugin(
118
124
}
119
125
addModel ( Model )
120
126
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
+
121
163
// (3^) Setup real-time events
122
164
if ( options . enableEvents ) {
123
165
const handleEvent = ( eventName , item , mutationName ) => {
@@ -130,9 +172,19 @@ export default function prepareMakeServicePlugin(
130
172
? confirmOrArray
131
173
: [ confirmOrArray ]
132
174
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
+ }
136
188
}
137
189
}
138
190
0 commit comments