pool: deprecate methods that take more than one filter.

This commit is contained in:
fiatjaf
2025-02-12 15:50:08 -03:00
parent 332a1642f0
commit c37ed1a309
3 changed files with 66 additions and 26 deletions

View File

@@ -82,24 +82,37 @@ func loadWalletFromPool(
kinds = append(kinds, 7376)
}
eoseChan := make(chan struct{})
events := pool.SubManyNotifyEOSE(
eoseChanE := make(chan struct{})
events := pool.SubscribeManyNotifyEOSE(
ctx,
relays,
nostr.Filters{
{Kinds: kinds, Authors: []string{pk}},
{Kinds: []int{5}, Tags: nostr.TagMap{"k": []string{"7375"}}, Authors: []string{pk}},
},
eoseChan,
nostr.Filter{Kinds: kinds, Authors: []string{pk}},
eoseChanE,
)
return loadWallet(ctx, kr, events, eoseChan)
eoseChanD := make(chan struct{})
deletions := pool.SubscribeManyNotifyEOSE(
ctx,
relays,
nostr.Filter{Kinds: []int{5}, Tags: nostr.TagMap{"k": []string{"7375"}}, Authors: []string{pk}},
eoseChanD,
)
eoseChan := make(chan struct{})
go func() {
<-eoseChanD
<-eoseChanE
close(eoseChan)
}()
return loadWallet(ctx, kr, events, deletions, eoseChan)
}
func loadWallet(
ctx context.Context,
kr nostr.Keyer,
events chan nostr.RelayEvent,
deletions chan nostr.RelayEvent,
eoseChan chan struct{},
) *Wallet {
w := &Wallet{
@@ -126,10 +139,8 @@ func loadWallet(
}()
go func() {
for ie := range events {
for ie := range deletions {
w.Lock()
switch ie.Event.Kind {
case 5:
if !eosed {
for _, tag := range ie.Event.Tags.All([]string{"e", ""}) {
w.pendingDeletions = append(w.pendingDeletions, tag[1])
@@ -139,6 +150,14 @@ func loadWallet(
w.removeDeletedToken(tag[1])
}
}
w.Unlock()
}
}()
go func() {
for ie := range events {
w.Lock()
switch ie.Event.Kind {
case 17375:
if err := w.parse(ctx, kr, ie.Event); err != nil {
if w.Processed != nil {

View File

@@ -161,7 +161,7 @@ func TestWallet(t *testing.T) {
}()
// load wallet from events
loaded := loadWallet(ctx, kr, evtChan, eoseChan)
loaded := loadWallet(ctx, kr, evtChan, make(chan nostr.RelayEvent), eoseChan)
loaded.Processed = func(evt *nostr.Event, err error) {
fmt.Println("processed", evt.Kind, err)
}

35
pool.go
View File

@@ -224,8 +224,29 @@ func (pool *SimplePool) PublishMany(ctx context.Context, urls []string, evt Even
return ch
}
// SubMany opens a subscription with the given filters to multiple relays
// the subscriptions only end when the context is canceled
// SubscribeMany opens a subscription with the given filter to multiple relays
// the subscriptions ends when the context is canceled or when all relays return a CLOSED.
func (pool *SimplePool) SubscribeMany(
ctx context.Context,
urls []string,
filter Filter,
opts ...SubscriptionOption,
) chan RelayEvent {
return pool.subMany(ctx, urls, Filters{filter}, nil, opts...)
}
// FetchMany opens a subscription, much like SubscribeMany, but it ends as soon as all Relays
// return an EOSE message.
func (pool *SimplePool) FetchMany(
ctx context.Context,
urls []string,
filter Filter,
opts ...SubscriptionOption,
) chan RelayEvent {
return pool.SubManyEose(ctx, urls, Filters{filter}, opts...)
}
// Deprecated: use SubscribeMany instead.
func (pool *SimplePool) SubMany(
ctx context.Context,
urls []string,
@@ -235,16 +256,16 @@ func (pool *SimplePool) SubMany(
return pool.subMany(ctx, urls, filters, nil, opts...)
}
// SubManyNotifyEOSE is like SubMany, but takes a channel that is closed when
// SubscribeManyNotifyEOSE is like SubscribeMany, but takes a channel that is closed when
// all subscriptions have received an EOSE
func (pool *SimplePool) SubManyNotifyEOSE(
func (pool *SimplePool) SubscribeManyNotifyEOSE(
ctx context.Context,
urls []string,
filters Filters,
filter Filter,
eoseChan chan struct{},
opts ...SubscriptionOption,
) chan RelayEvent {
return pool.subMany(ctx, urls, filters, eoseChan, opts...)
return pool.subMany(ctx, urls, Filters{filter}, eoseChan, opts...)
}
func (pool *SimplePool) subMany(
@@ -426,7 +447,7 @@ func (pool *SimplePool) subMany(
return events
}
// SubManyEose is like SubMany, but it stops subscriptions and closes the channel when gets a EOSE
// Deprecated: use FetchMany instead.
func (pool *SimplePool) SubManyEose(
ctx context.Context,
urls []string,