| @@ -287,11 +287,9 @@ const makeEventStore = Effect.gen(function* () { · 286 unmodified lines |
| 287 | 287 | if (normalizedLimit === 0) { |
| 288 | 288 | return Stream.empty; |
| 289 | 289 | } |
| 290 | | − const readPage = ( |
| 291 | | − cursor: number, |
| 292 | | − remaining: number, |
| 293 | | − ): Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError> => |
| 294 | | − Stream.fromEffect( |
| 290 | + return Stream.paginate( |
| 291 | + { cursor: sequenceExclusive, remaining: normalizedLimit }, |
| 292 | + ({ cursor, remaining }) => |
| 295 | 293 | readEventRowsFromSequence({ |
| 296 | 294 | sequenceExclusive: cursor, |
| 297 | 295 | limit: Math.min(remaining, READ_PAGE_SIZE), |
| @@ -311,24 +309,18 @@ const makeEventStore = Effect.gen(function* () { · 13 unmodified lines |
| 311 | 309 | ), |
| 312 | 310 | ), |
| 313 | 311 | ), |
| 312 | + Effect.map((events) => { |
| 313 | + const last = events.at(-1); |
| 314 | + const nextRemaining = remaining - events.length; |
| 315 | + return [ |
| 316 | + events, |
| 317 | + last === undefined || nextRemaining <= 0 |
| 318 | + ? Option.none() |
| 319 | + : Option.some({ cursor: last.sequence, remaining: nextRemaining }), |
| 320 | + ] as const; |
| 321 | + }), |
| 314 | 322 | ), |
| 315 | | − ).pipe( |
| 316 | | − Stream.flatMap((events) => { |
| 317 | | − if (events.length === 0) { |
| 318 | | − return Stream.empty; |
| 319 | | − } |
| 320 | | − const nextRemaining = remaining - events.length; |
| 321 | | − if (nextRemaining <= 0) { |
| 322 | | − return Stream.fromIterable(events); |
| 323 | | − } |
| 324 | | − return Stream.concat( |
| 325 | | − Stream.fromIterable(events), |
| 326 | | − readPage(events[events.length - 1]!.sequence, nextRemaining), |
| 327 | | − ); |
| 328 | | − }), |
| 329 | | − ); |
| 330 | | − |
| 331 | | − return readPage(sequenceExclusive, normalizedLimit); |
| 323 | + ); |
| 332 | 324 | }; |
| 333 | 325 | |
| 334 | 326 | const findEventAfter = SqlSchema.findOneOption({ |
| @@ -363,11 +355,9 @@ const makeEventStore = Effect.gen(function* () { · 28 unmodified lines |
| 363 | 355 | if (limit === 0 || input.fromSequenceExclusive >= input.toSequenceInclusive) { |
| 364 | 356 | return Stream.empty; |
| 365 | 357 | } |
| 366 | | − const readPage = ( |
| 367 | | − cursor: number, |
| 368 | | − remaining: number, |
| 369 | | − ): Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError> => |
| 370 | | − Stream.fromEffect( |
| 358 | + return Stream.paginate( |
| 359 | + { cursor: input.fromSequenceExclusive, remaining: limit }, |
| 360 | + ({ cursor, remaining }) => |
| 371 | 361 | readAggregateEventRows({ |
| 372 | 362 | ...input, |
| 373 | 363 | fromSequenceExclusive: cursor, |
| @@ -388,25 +378,21 @@ const makeEventStore = Effect.gen(function* () { · 14 unmodified lines |
| 388 | 378 | ), |
| 389 | 379 | ), |
| 390 | 380 | ), |
| 381 | + Effect.map((events) => { |
| 382 | + const last = events.at(-1); |
| 383 | + const nextRemaining = remaining - events.length; |
| 384 | + return [ |
| 385 | + events, |
| 386 | + last === undefined || |
| 387 | + events.length < READ_PAGE_SIZE || |
| 388 | + nextRemaining === 0 || |
| 389 | + last.sequence >= input.toSequenceInclusive |
| 390 | + ? Option.none() |
| 391 | + : Option.some({ cursor: last.sequence, remaining: nextRemaining }), |
| 392 | + ] as const; |
| 393 | + }), |
| 391 | 394 | ), |
| 392 | | − ).pipe( |
| 393 | | − Stream.flatMap((events) => { |
| 394 | | − const last = events.at(-1); |
| 395 | | − if (last === undefined) { |
| 396 | | − return Stream.empty; |
| 397 | | − } |
| 398 | | − const nextRemaining = remaining - events.length; |
| 399 | | − if ( |
| 400 | | − events.length < READ_PAGE_SIZE || |
| 401 | | − nextRemaining === 0 || |
| 402 | | − last.sequence >= input.toSequenceInclusive |
| 403 | | − ) { |
| 404 | | − return Stream.fromIterable(events); |
| 405 | | − } |
| 406 | | − return Stream.concat(Stream.fromIterable(events), readPage(last.sequence, nextRemaining)); |
| 407 | | − }), |
| 408 | | − ); |
| 409 | | − return readPage(input.fromSequenceExclusive, limit); |
| 395 | + ); |
| 410 | 396 | }; |
| 411 | 397 | |
| 412 | 398 | const getAggregateReplayStats: OrchestrationEventStoreShape["getAggregateReplayStats"] = ( |