Skip to content

Commit

Permalink
after getting an EOSE we should stop checking since/until.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Sep 6, 2024
1 parent cd145f4 commit f57d93a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
33 changes: 25 additions & 8 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,37 @@ func (eff Filters) Match(event *Event) bool {
return false
}

func (eff Filters) MatchIgnoringTimestampConstraints(event *Event) bool {
for _, filter := range eff {
if filter.MatchesIgnoringTimestampConstraints(event) {
return true
}
}
return false
}

func (ef Filter) String() string {
j, _ := easyjson.Marshal(ef)
return string(j)
}

func (ef Filter) Matches(event *Event) bool {
if !ef.MatchesIgnoringTimestampConstraints(event) {
return false
}

if ef.Since != nil && event.CreatedAt < *ef.Since {
return false
}

if ef.Until != nil && event.CreatedAt > *ef.Until {
return false
}

return true
}

func (ef Filter) MatchesIgnoringTimestampConstraints(event *Event) bool {
if event == nil {
return false
}
Expand All @@ -67,14 +92,6 @@ func (ef Filter) Matches(event *Event) bool {
}
}

if ef.Since != nil && event.CreatedAt < *ef.Since {
return false
}

if ef.Until != nil && event.CreatedAt > *ef.Until {
return false
}

return true
}

Expand Down
1 change: 1 addition & 0 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ func (r *Relay) PrepareSubscription(ctx context.Context, filters Filters, opts .
EndOfStoredEvents: make(chan struct{}, 1),
ClosedReason: make(chan string, 1),
Filters: filters,
match: filters.Match,
}

for _, opt := range opts {
Expand Down
2 changes: 2 additions & 0 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Subscription struct {
// Context will be .Done() when the subscription ends
Context context.Context

match func(*Event) bool // this will be either Filters.Match or Filters.MatchIgnoringTimestampConstraints
live atomic.Bool
eosed atomic.Bool
closed atomic.Bool
Expand Down Expand Up @@ -104,6 +105,7 @@ func (sub *Subscription) dispatchEvent(evt *Event) {

func (sub *Subscription) dispatchEose() {
if sub.eosed.CompareAndSwap(false, true) {
sub.match = sub.Filters.MatchIgnoringTimestampConstraints
go func() {
sub.storedwg.Wait()
sub.EndOfStoredEvents <- struct{}{}
Expand Down

0 comments on commit f57d93a

Please sign in to comment.