From e6ef78c509e56e2277803f00439e04bb17ffb5a8 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 24 Feb 2024 18:51:56 -0300 Subject: [PATCH] DirectedFilters to accept a list of filters. --- pool.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pool.go b/pool.go index 6e1b955..b78e1b6 100644 --- a/pool.go +++ b/pool.go @@ -24,8 +24,8 @@ type SimplePool struct { cancel context.CancelFunc } -type DirectedFilter struct { - Filter +type DirectedFilters struct { + Filters Relay string } @@ -317,23 +317,25 @@ func (pool *SimplePool) QuerySingle(ctx context.Context, urls []string, filter F func (pool *SimplePool) batchedSubMany( ctx context.Context, - dfs []DirectedFilter, + dfs []DirectedFilters, subFn func(context.Context, []string, Filters, bool) chan IncomingEvent, ) chan IncomingEvent { type batch struct { - filter Filter - relays []string + filters Filters + relays []string } batches := make([]batch, 0, len(dfs)) for _, df := range dfs { - idx := slices.IndexFunc(batches, func(b batch) bool { return FilterEqual(b.filter, df.Filter) }) + idx := slices.IndexFunc(batches, func(b batch) bool { + return slices.EqualFunc(b.filters, df.Filters, FilterEqual) + }) if idx != -1 { batches[idx].relays = append(batches[idx].relays, df.Relay) } else { relays := make([]string, 0, 10) relays = append(relays, df.Relay) - batches = append(batches, batch{filter: df.Filter, relays: relays}) + batches = append(batches, batch{filters: df.Filters, relays: relays}) } } @@ -341,7 +343,7 @@ func (pool *SimplePool) batchedSubMany( for _, b := range batches { go func(b batch) { - for ie := range subFn(ctx, b.relays, Filters{b.filter}, true) { + for ie := range subFn(ctx, b.relays, b.filters, true) { res <- ie } }(b) @@ -351,11 +353,11 @@ func (pool *SimplePool) batchedSubMany( } // BatchedSubMany fires subscriptions only to specific relays, but batches them when they are the same. -func (pool *SimplePool) BatchedSubMany(ctx context.Context, dfs []DirectedFilter) chan IncomingEvent { +func (pool *SimplePool) BatchedSubMany(ctx context.Context, dfs []DirectedFilters) chan IncomingEvent { return pool.batchedSubMany(ctx, dfs, pool.subMany) } // BatchedSubManyEose is like BatchedSubMany, but ends upon receiving EOSE from all relays. -func (pool *SimplePool) BatchedSubManyEose(ctx context.Context, dfs []DirectedFilter) chan IncomingEvent { +func (pool *SimplePool) BatchedSubManyEose(ctx context.Context, dfs []DirectedFilters) chan IncomingEvent { return pool.batchedSubMany(ctx, dfs, pool.subManyEose) }