@@ -19,6 +19,7 @@ import { makeTodos } from '../fixtures/todos'
19
19
import Vuex from 'vuex'
20
20
import { performance } from 'perf_hooks'
21
21
import enableServiceEvents from '../../src/service-module/service-module.events'
22
+ import { Service } from '@feathersjs/feathers'
22
23
23
24
interface Options {
24
25
idField : string
@@ -1191,15 +1192,17 @@ describe('Service Module', function () {
1191
1192
} )
1192
1193
1193
1194
describe ( 'Updates the Store on Events' , function ( ) {
1194
- const {
1195
- feathersSocketioClient,
1196
- BaseModel,
1197
- Thing,
1198
- ThingDebounced,
1199
- TodoDebounced,
1195
+ let feathersSocketioClient ,
1200
1196
debouncedQueue ,
1201
- store
1202
- } = makeSocketIoContext ( )
1197
+ store ,
1198
+ debouncedService : Service < any >
1199
+ beforeEach ( ( ) => {
1200
+ const context = makeSocketIoContext ( )
1201
+ feathersSocketioClient = context . feathersSocketioClient
1202
+ debouncedQueue = context . debouncedQueue
1203
+ store = context . store
1204
+ debouncedService = feathersSocketioClient . service ( 'things-debounced' )
1205
+ } )
1203
1206
1204
1207
it ( 'created' , function ( done ) {
1205
1208
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -1233,15 +1236,12 @@ describe('Service Module', function () {
1233
1236
store . state [ 'things-debounced' ] . keyedById [ 0 ] . test ,
1234
1237
'the item received from the socket event was added to the store'
1235
1238
)
1236
- feathersSocketioClient
1237
- . service ( 'things-debounced' )
1238
- . off ( 'created' , listener )
1239
+ debouncedService . off ( 'created' , listener )
1239
1240
done ( )
1240
1241
} , debounceEventsTime * 2 )
1241
1242
}
1242
- feathersSocketioClient . service ( 'things-debounced' ) . on ( 'created' , listener )
1243
-
1244
- feathersSocketioClient . service ( 'things-debounced' ) . create ( { test : true } )
1243
+ debouncedService . on ( 'created' , listener )
1244
+ debouncedService . create ( { test : true } )
1245
1245
} )
1246
1246
1247
1247
it ( 'patched' , function ( done ) {
@@ -1277,17 +1277,12 @@ describe('Service Module', function () {
1277
1277
'the item received from the socket event was updated in the store'
1278
1278
)
1279
1279
} , debounceEventsTime * 2 )
1280
- feathersSocketioClient
1281
- . service ( 'things-debounced' )
1282
- . off ( 'patched' , listener )
1280
+ debouncedService . off ( 'patched' , listener )
1283
1281
done ( )
1284
1282
}
1285
1283
1286
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1287
- feathersSocketioClient . service ( 'things-debounced' ) . on ( 'patched' , listener )
1288
- feathersSocketioClient
1289
- . service ( 'things-debounced' )
1290
- . patch ( 1 , { test : true } )
1284
+ debouncedService . on ( 'patched' , listener )
1285
+ debouncedService . patch ( 1 , { test : true } )
1291
1286
} )
1292
1287
1293
1288
it ( 'updated' , function ( done ) {
@@ -1324,17 +1319,11 @@ describe('Service Module', function () {
1324
1319
)
1325
1320
done ( )
1326
1321
} , debounceEventsTime * 2 )
1327
- feathersSocketioClient
1328
- . service ( 'things-debounced' )
1329
- . off ( 'updated' , listener )
1322
+ debouncedService . off ( 'updated' , listener )
1330
1323
}
1331
1324
1332
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1333
- feathersSocketioClient . service ( 'things-debounced' ) . on ( 'updated' , listener )
1334
-
1335
- feathersSocketioClient
1336
- . service ( 'things-debounced' )
1337
- . update ( 1 , { test : true } )
1325
+ debouncedService . on ( 'updated' , listener )
1326
+ debouncedService . update ( 1 , { test : true } )
1338
1327
} )
1339
1328
1340
1329
it ( 'removed' , function ( done ) {
@@ -1372,21 +1361,19 @@ describe('Service Module', function () {
1372
1361
)
1373
1362
done ( )
1374
1363
} , debounceEventsTime * 2 )
1375
-
1376
- feathersSocketioClient
1377
- . service ( 'things-debounced' )
1378
- . off ( 'removed' , listener )
1364
+ debouncedService . off ( 'removed' , listener )
1379
1365
}
1380
1366
1381
- feathersSocketioClient . service ( 'things-debounced' ) . on ( 'removed' , listener )
1382
-
1383
- feathersSocketioClient . service ( 'things-debounced' ) . remove ( 1 )
1367
+ debouncedService . on ( 'removed' , listener )
1368
+ debouncedService . remove ( 1 )
1384
1369
} )
1385
1370
1386
1371
it ( 'debounce works with plenty items' , function ( done ) {
1387
1372
store . commit ( 'things-debounced/clearAll' )
1388
1373
1389
- const { debounceEventsTime } = store . state [ 'things-debounced' ]
1374
+ const { debounceEventsTime, debounceEventsMaxWait } = store . state [
1375
+ 'things-debounced'
1376
+ ]
1390
1377
1391
1378
const itemsCount = 100
1392
1379
let i = 0
@@ -1396,17 +1383,20 @@ describe('Service Module', function () {
1396
1383
'no items at start'
1397
1384
)
1398
1385
1386
+ const now = performance . now ( )
1387
+
1399
1388
const setTimeoutCreate = ( ) => {
1400
1389
setTimeout ( ( ) => {
1401
- feathersSocketioClient
1402
- . service ( 'things-debounced' )
1403
- . create ( { test : true , i } )
1390
+ debouncedService . create ( { test : true , i } )
1404
1391
i ++
1405
- assert (
1406
- Object . keys ( store . state [ 'things-debounced' ] . keyedById ) . length === 0 ,
1407
- `no items at i: ${ i } `
1408
- )
1409
1392
if ( i < itemsCount ) {
1393
+ if ( performance . now ( ) - now < debounceEventsMaxWait ) {
1394
+ assert (
1395
+ Object . keys ( store . state [ 'things-debounced' ] . keyedById )
1396
+ . length === 0 ,
1397
+ `no items at i: ${ i } `
1398
+ )
1399
+ }
1410
1400
setTimeoutCreate ( )
1411
1401
} else {
1412
1402
setTimeout ( ( ) => {
@@ -1443,9 +1433,7 @@ describe('Service Module', function () {
1443
1433
1444
1434
const setTimeoutCreate = ( ) => {
1445
1435
setTimeout ( ( ) => {
1446
- feathersSocketioClient
1447
- . service ( 'things-debounced' )
1448
- . create ( { test : true , i } )
1436
+ debouncedService . create ( { test : true , i } )
1449
1437
i ++
1450
1438
const timePassed = Math . floor (
1451
1439
performance . now ( ) - startedAt - debounceEventsTime
0 commit comments