@@ -117,13 +117,13 @@ test('args: wait', async () => {
117117 expect ( x10 ) . toEqual ( [ 10 , [ 8 , 9 , 10 ] ] )
118118} )
119119
120- test ( 'args: shouldSplitBatch ' , async ( ) => {
120+ test ( 'args: getBatchSize ' , async ( ) => {
121121 const fn = vi . fn ( )
122122 const { schedule } = createBatchScheduler ( {
123123 id : uid ( ) ,
124124 // biome-ignore lint/style/noCommaOperator:
125125 fn : async ( args : number [ ] ) => ( fn ( ) , args ) ,
126- shouldSplitBatch : ( args ) => args . length > 3 ,
126+ getBatchSize : ( ) => 3 ,
127127 } )
128128
129129 const p = [ ]
@@ -163,6 +163,47 @@ test('args: shouldSplitBatch', async () => {
163163 expect ( fn ) . toBeCalledTimes ( 6 )
164164} )
165165
166+ test ( 'args: waitAsRateLimit' , async ( ) => {
167+ const date = Date . now ( )
168+
169+ const fn = vi . fn ( )
170+ const { schedule } = createBatchScheduler ( {
171+ id : uid ( ) ,
172+ getBatchSize : ( ) => 3 ,
173+ wait : 500 ,
174+ waitAsRateLimit : true ,
175+ // biome-ignore lint/style/noCommaOperator:
176+ fn : async ( args : number [ ] ) => ( fn ( ) , args ) ,
177+ } )
178+
179+ const p = [ ]
180+
181+ p . push ( schedule ( 1 ) )
182+ p . push ( schedule ( 2 ) )
183+ p . push ( schedule ( 3 ) )
184+ p . push ( schedule ( 4 ) )
185+ p . push ( schedule ( 5 ) )
186+ p . push ( schedule ( 6 ) )
187+ p . push ( schedule ( 7 ) )
188+ p . push ( schedule ( 8 ) )
189+ p . push ( schedule ( 9 ) )
190+
191+ const [ x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 , x9 ] = await Promise . all ( p )
192+
193+ expect ( x1 ) . toEqual ( [ 1 , [ 1 , 2 , 3 ] ] )
194+ expect ( x2 ) . toEqual ( [ 2 , [ 1 , 2 , 3 ] ] )
195+ expect ( x3 ) . toEqual ( [ 3 , [ 1 , 2 , 3 ] ] )
196+ expect ( x4 ) . toEqual ( [ 4 , [ 4 , 5 , 6 ] ] )
197+ expect ( x5 ) . toEqual ( [ 5 , [ 4 , 5 , 6 ] ] )
198+ expect ( x6 ) . toEqual ( [ 6 , [ 4 , 5 , 6 ] ] )
199+ expect ( x7 ) . toEqual ( [ 7 , [ 7 , 8 , 9 ] ] )
200+ expect ( x8 ) . toEqual ( [ 8 , [ 7 , 8 , 9 ] ] )
201+ expect ( x9 ) . toEqual ( [ 9 , [ 7 , 8 , 9 ] ] )
202+
203+ const elapsed = Date . now ( ) - date
204+ expect ( elapsed ) . toBeGreaterThanOrEqual ( 1500 )
205+ } )
206+
166207describe ( 'behavior' , ( ) => {
167208 test ( 'complex args' , async ( ) => {
168209 const { schedule } = createBatchScheduler ( {
@@ -188,51 +229,6 @@ describe('behavior', () => {
188229 ] )
189230 } )
190231
191- test ( 'complex split batch' , async ( ) => {
192- const fn = vi . fn ( )
193- const { schedule } = createBatchScheduler ( {
194- id : uid ( ) ,
195- wait : 16 ,
196- // biome-ignore lint/style/noCommaOperator:
197- fn : async ( args : string [ ] ) => ( fn ( ) , args ) ,
198- shouldSplitBatch : ( args ) =>
199- args . reduce ( ( acc , x ) => acc + x . length , 0 ) > 20 ,
200- } )
201-
202- const p = [ ]
203- p . push ( schedule ( 'hello' ) )
204- p . push ( schedule ( 'world' ) )
205- p . push ( schedule ( 'this is me' ) )
206- p . push ( schedule ( 'life should be' ) )
207- p . push ( schedule ( 'fun for everyone' ) )
208- await wait ( 1 )
209- p . push ( schedule ( 'hello world' ) )
210- p . push ( schedule ( 'come and see' ) )
211- p . push ( schedule ( 'come' ) )
212- p . push ( schedule ( 'and' ) )
213- await wait ( 16 )
214- p . push ( schedule ( 'see' ) )
215- p . push ( schedule ( 'smile' ) )
216- p . push ( schedule ( 'just be yourself' ) )
217- p . push ( schedule ( 'be happy' ) )
218-
219- const [ x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 , x9 , x10 , x11 ] = await Promise . all ( p )
220-
221- expect ( x1 ) . toEqual ( [ 'hello' , [ 'hello' , 'world' , 'this is me' ] ] )
222- expect ( x2 ) . toEqual ( [ 'world' , [ 'hello' , 'world' , 'this is me' ] ] )
223- expect ( x3 ) . toEqual ( [ 'this is me' , [ 'hello' , 'world' , 'this is me' ] ] )
224- expect ( x4 ) . toEqual ( [ 'life should be' , [ 'life should be' ] ] )
225- expect ( x5 ) . toEqual ( [ 'fun for everyone' , [ 'fun for everyone' ] ] )
226- expect ( x6 ) . toEqual ( [ 'hello world' , [ 'hello world' ] ] )
227- expect ( x7 ) . toEqual ( [ 'come and see' , [ 'come and see' , 'come' , 'and' ] ] )
228- expect ( x8 ) . toEqual ( [ 'come' , [ 'come and see' , 'come' , 'and' ] ] )
229- expect ( x9 ) . toEqual ( [ 'and' , [ 'come and see' , 'come' , 'and' ] ] )
230- expect ( x10 ) . toEqual ( [ 'see' , [ 'see' , 'smile' ] ] )
231- expect ( x11 ) . toEqual ( [ 'smile' , [ 'see' , 'smile' ] ] )
232-
233- expect ( fn ) . toBeCalledTimes ( 8 )
234- } )
235-
236232 test ( 'throws error' , async ( ) => {
237233 const { schedule } = createBatchScheduler ( {
238234 id : uid ( ) ,
0 commit comments