@@ -2,41 +2,63 @@ import { getId } from '../utils'
2
2
import _debounce from 'lodash/debounce'
3
3
import { globalModels } from './global-models'
4
4
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 = {
7
21
addOrUpdateById : { } ,
8
22
removeItemById : { } ,
9
- queueAddOrUpdate ( item ) : void {
23
+ enqueueAddOrUpdate ( item ) : void {
10
24
const id = getId ( item , options . idField )
11
25
this . addOrUpdateById [ id ] = item
12
26
if ( this . removeItemById . hasOwnProperty ( id ) ) {
13
27
delete this . removeItemById [ id ]
14
28
}
15
29
this . flushAddOrUpdateQueue ( )
16
30
} ,
17
- queueRemoval ( item ) : void {
31
+ enqueueRemoval ( item ) : void {
18
32
const id = getId ( item , options . idField )
19
33
this . removeItemById [ id ] = item
20
34
if ( this . addOrUpdateById . hasOwnProperty ( id ) ) {
21
35
delete this . addOrUpdateById [ id ]
22
36
}
23
37
this . flushRemoveItemQueue ( )
24
38
} ,
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
+ )
40
62
}
41
63
42
64
const handleEvent = ( eventName , item , mutationName ) : void => {
@@ -55,8 +77,8 @@ export function enableServiceEvents({ service, Model, store, options }): void {
55
77
: store . dispatch ( `${ options . namespace } /${ mutationName } ` , modified )
56
78
} else {
57
79
eventName === 'removed'
58
- ? debounceMap . queueRemoval ( item )
59
- : debounceMap . queueAddOrUpdate ( item )
80
+ ? debouncedQueue . enqueueRemoval ( item )
81
+ : debouncedQueue . enqueueAddOrUpdate ( item )
60
82
}
61
83
}
62
84
}
@@ -78,4 +100,6 @@ export function enableServiceEvents({ service, Model, store, options }): void {
78
100
handleEvent ( 'removed' , item , 'removeItem' )
79
101
Model . emit && Model . emit ( 'removed' , item )
80
102
} )
103
+
104
+ return debouncedQueue
81
105
}
0 commit comments