-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(hooks): use external pubsub (#65)
- Loading branch information
1 parent
6e4ddab
commit 97a8cf7
Showing
8 changed files
with
118 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,40 @@ | ||
package hooks | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/hannahhoward/go-pubsub" | ||
"github.com/ipfs/go-graphsync" | ||
peer "github.com/libp2p/go-libp2p-core/peer" | ||
) | ||
|
||
type completedListener struct { | ||
key uint64 | ||
listener graphsync.OnResponseCompletedListener | ||
} | ||
|
||
// CompletedResponseListeners is a set of listeners for completed responses | ||
type CompletedResponseListeners struct { | ||
listenersLk sync.RWMutex | ||
nextKey uint64 | ||
listeners []completedListener | ||
pubSub *pubsub.PubSub | ||
} | ||
|
||
type internalCompletedResponseEvent struct { | ||
p peer.ID | ||
request graphsync.RequestData | ||
status graphsync.ResponseStatusCode | ||
} | ||
|
||
func completedResponseDispatcher(event pubsub.Event, subscriberFn pubsub.SubscriberFn) error { | ||
ie := event.(internalCompletedResponseEvent) | ||
listener := subscriberFn.(graphsync.OnResponseCompletedListener) | ||
listener(ie.p, ie.request, ie.status) | ||
return nil | ||
} | ||
|
||
// NewCompletedResponseListeners returns a new list of completed response listeners | ||
func NewCompletedResponseListeners() *CompletedResponseListeners { | ||
return &CompletedResponseListeners{} | ||
return &CompletedResponseListeners{pubSub: pubsub.New(completedResponseDispatcher)} | ||
} | ||
|
||
// Register registers an listener for completed responses | ||
func (crl *CompletedResponseListeners) Register(listener graphsync.OnResponseCompletedListener) graphsync.UnregisterHookFunc { | ||
crl.listenersLk.Lock() | ||
cl := completedListener{crl.nextKey, listener} | ||
crl.nextKey++ | ||
crl.listeners = append(crl.listeners, cl) | ||
crl.listenersLk.Unlock() | ||
return func() { | ||
crl.listenersLk.Lock() | ||
defer crl.listenersLk.Unlock() | ||
for i, matchListener := range crl.listeners { | ||
if cl.key == matchListener.key { | ||
crl.listeners = append(crl.listeners[:i], crl.listeners[i+1:]...) | ||
return | ||
} | ||
} | ||
} | ||
return graphsync.UnregisterHookFunc(crl.pubSub.Subscribe(listener)) | ||
} | ||
|
||
// NotifyCompletedListeners runs notifies all completed listeners that a response has completed | ||
func (crl *CompletedResponseListeners) NotifyCompletedListeners(p peer.ID, request graphsync.RequestData, status graphsync.ResponseStatusCode) { | ||
crl.listenersLk.RLock() | ||
defer crl.listenersLk.RUnlock() | ||
for _, listener := range crl.listeners { | ||
listener.listener(p, request, status) | ||
} | ||
_ = crl.pubSub.Publish(internalCompletedResponseEvent{p, request, status}) | ||
} |
Oops, something went wrong.