| ... | @@ -287,11 +287,9 @@ const makeEventStore = Effect.gen(function* () { | ... | @@ -287,11 +287,9 @@ const makeEventStore = Effect.gen(function* () { |
| 287 | if (normalizedLimit === 0) { | 287 | if (normalizedLimit === 0) { |
| 288 | return Stream.empty; | 288 | return Stream.empty; |
| 289 | } | 289 | } |
| 290 | const readPage = ( | 290 | return Stream.paginate( |
| 291 | cursor: number, | 291 | { cursor: sequenceExclusive, remaining: normalizedLimit }, |
| 292 | remaining: number, | 292 | ({ cursor, remaining }) => |
| 293 | ): Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError> => | | |
| 294 | Stream.fromEffect( | | |
| 295 | readEventRowsFromSequence({ | 293 | readEventRowsFromSequence({ |
| 296 | sequenceExclusive: cursor, | 294 | sequenceExclusive: cursor, |
| 297 | limit: Math.min(remaining, READ_PAGE_SIZE), | 295 | limit: Math.min(remaining, READ_PAGE_SIZE), |
| ... | @@ -311,24 +309,18 @@ const makeEventStore = Effect.gen(function* () { | ... | @@ -311,24 +309,18 @@ const makeEventStore = Effect.gen(function* () { |
| 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( | 323 | ); |
| 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); | | |
| 332 | }; | 324 | }; |
| 333 | | 325 | |
| 334 | const findEventAfter = SqlSchema.findOneOption({ | 326 | const findEventAfter = SqlSchema.findOneOption({ |
| ... | @@ -363,11 +355,9 @@ const makeEventStore = Effect.gen(function* () { | ... | @@ -363,11 +355,9 @@ const makeEventStore = Effect.gen(function* () { |
| 363 | if (limit === 0 || input.fromSequenceExclusive >= input.toSequenceInclusive) { | 355 | if (limit === 0 || input.fromSequenceExclusive >= input.toSequenceInclusive) { |
| 364 | return Stream.empty; | 356 | return Stream.empty; |
| 365 | } | 357 | } |
| 366 | const readPage = ( | 358 | return Stream.paginate( |
| 367 | cursor: number, | 359 | { cursor: input.fromSequenceExclusive, remaining: limit }, |
| 368 | remaining: number, | 360 | ({ cursor, remaining }) => |
| 369 | ): Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError> => | | |
| 370 | Stream.fromEffect( | | |
| 371 | readAggregateEventRows({ | 361 | readAggregateEventRows({ |
| 372 | ...input, | 362 | ...input, |
| 373 | fromSequenceExclusive: cursor, | 363 | fromSequenceExclusive: cursor, |
| ... | @@ -388,25 +378,21 @@ const makeEventStore = Effect.gen(function* () { | ... | @@ -388,25 +378,21 @@ const makeEventStore = Effect.gen(function* () { |
| 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( | 395 | ); |
| 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); | | |
| 410 | }; | 396 | }; |
| 411 | | 397 | |
| 412 | const getAggregateReplayStats: OrchestrationEventStoreShape["getAggregateReplayStats"] = ( | 398 | const getAggregateReplayStats: OrchestrationEventStoreShape["getAggregateReplayStats"] = ( |