From ade3996fbf5a34a39580dc9bcb2a8cd021bb7f06 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 17 Dec 2024 13:30:04 -0300 Subject: [PATCH] nip77: simplify direction selection. --- nip77/example/example.go | 2 +- nip77/nip77.go | 38 ++++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/nip77/example/example.go b/nip77/example/example.go index db25ebb..d0fde7a 100644 --- a/nip77/example/example.go +++ b/nip77/example/example.go @@ -45,7 +45,7 @@ func main() { } err := nip77.NegentropySync(ctx, - local, "ws://localhost:7777", nostr.Filter{}) + local, "ws://localhost:7777", nostr.Filter{}, nip77.Both) if err != nil { panic(err) } diff --git a/nip77/nip77.go b/nip77/nip77.go index 72ab1c2..a6374af 100644 --- a/nip77/nip77.go +++ b/nip77/nip77.go @@ -17,7 +17,21 @@ type direction struct { target nostr.RelayStore } -func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, filter nostr.Filter, d string) error { +type Direction int + +const ( + Up = 0 + Down = 1 + Both = 2 +) + +func NegentropySync( + ctx context.Context, + store nostr.RelayStore, + url string, + filter nostr.Filter, + dir Direction, +) error { id := "go-nostr-tmp" // for now we can't have more than one subscription in the same connection data, err := store.QuerySync(ctx, filter) @@ -80,13 +94,13 @@ func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, fil pool := newidlistpool(50) // Define sync directions - directions := map[string][]direction{ - "up": {{"up", neg.Haves, store, r}}, - "down": {{"down", neg.HaveNots, r, store}}, - "both": {{"up", neg.Haves, store, r}, {"down", neg.HaveNots, r, store}}, + directions := [][]direction{ + {{"up", neg.Haves, store, r}}, + {{"down", neg.HaveNots, r, store}}, + {{"up", neg.Haves, store, r}, {"down", neg.HaveNots, r, store}}, } - for _, dir := range selectDir(directions, d) { + for _, dir := range directions[dir] { wg.Add(1) go func(dir direction) { defer wg.Done() @@ -141,15 +155,3 @@ func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, fil return nil } - -// selectDir returns the directions to sync based on the user input -func selectDir(directions map[string][]direction, d string) []direction { - switch d { - case "up": - return directions["up"] - case "down": - return directions["down"] - default: - return directions["both"] - } -}