Skip to content

Commit

Permalink
nip77: simplify direction selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Dec 17, 2024
1 parent b02f0d6 commit ade3996
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion nip77/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
38 changes: 20 additions & 18 deletions nip77/nip77.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"]
}
}

0 comments on commit ade3996

Please sign in to comment.