fix: improve paging and subscriptions

This commit is contained in:
Jon Lundy
2022-08-10 10:09:58 -06:00
parent 0642879c07
commit 72e7d5f265
11 changed files with 118 additions and 81 deletions

View File

@@ -114,31 +114,9 @@ func (es *eventLog) Read(ctx context.Context, pos, count int64) (event.Events, e
return nil
}
if count == AllEvents {
count = int64(first - last)
}
var start uint64
switch {
case pos >= 0 && count > 0:
start = first + uint64(pos)
case pos < 0 && count > 0:
start = uint64(int64(last) + pos + 1)
case pos >= 0 && count < 0:
start = first + uint64(pos)
if pos > 1 {
start -= 2 // if pos is positive and count negative start before
}
if pos <= 1 {
return nil // if pos is one or zero and negative count nothing to return
}
case pos < 0 && count < 0:
start = uint64(int64(last) + pos)
}
if start >= last {
return nil // if start is after last and positive count nothing to return
start, count := math.PagerBox(first, last, pos, count)
if count == 0 {
return nil
}
events = make([]event.Event, math.Abs(count))