Move to a callback for notifications.

Rather than using a channel for notifictions, use a callback instead.
There are several issues that arise with async notifications via a
channel, so use a callback instead.  The caller can always make the
notifications async by using channels in the provided callback if
needed.
This commit is contained in:
Dave Collins 2013-10-02 21:16:38 -05:00
parent e54fb96d80
commit 667eaa1562
2 changed files with 17 additions and 13 deletions

View file

@ -144,7 +144,7 @@ func removeChildNode(children []*blockNode, node *blockNode) []*blockNode {
type BlockChain struct {
db btcdb.Db
btcnet btcwire.BitcoinNet
notifications chan *Notification
notifications NotificationCallback
root *blockNode
bestChain *blockNode
index map[btcwire.ShaHash]*blockNode
@ -960,12 +960,12 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block) err
}
// New returns a BlockChain instance for the passed bitcoin network using the
// provided backing database. It accepts a channel on which asynchronous
// notifications will be sent when various events take place. See the
// documentation for Notification and NotificationType for details on the
// types and contents of notifications. The provided channel can be nil if the
// caller is not interested in receiving notifications.
func New(db btcdb.Db, btcnet btcwire.BitcoinNet, c chan *Notification) *BlockChain {
// provided backing database. It accepts a callback on which notifications
// will be sent when various events take place. See the documentation for
// Notification and NotificationType for details on the types and contents of
// notifications. The provided callback can be nil if the caller is not
// interested in receiving notifications.
func New(db btcdb.Db, btcnet btcwire.BitcoinNet, c NotificationCallback) *BlockChain {
b := BlockChain{
db: db,
btcnet: btcnet,

View file

@ -11,6 +11,10 @@ import (
// NotificationType represents the type of a notification message.
type NotificationType int
// NotificationCallback is used for a caller to provide a callback for
// notifications about various chain events.
type NotificationCallback func(*Notification)
// Constants for the type of a notification message.
const (
// NTOrphanBlock indicates an orphan block was processed and the
@ -50,10 +54,9 @@ func (n NotificationType) String() string {
return fmt.Sprintf("Unknown Notification Type (%d)", int(n))
}
// Notification defines an asynchronous notification that is sent to the caller
// over the notification channel provided during the call to New and consists
// of a notification type as well as associated data that depends on the type as
// follows:
// Notification defines notification that is sent to the caller via the callback
// function provided during the call to New and consists of a notification type
// as well as associated data that depends on the type as follows:
// - NTOrphanBlock: *btcwire.ShaHash
// - NTBlockAccepted: *btcutil.Block
// - NTBlockConnected: *btcutil.Block
@ -64,7 +67,8 @@ type Notification struct {
}
// sendNotification sends a notification with the passed type and data if the
// caller requested notifications by providing a channel in the call to New.
// caller requested notifications by providing a callback function in the call
// to New.
func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
// Ignore it if the caller didn't request notifications.
if b.notifications == nil {
@ -73,5 +77,5 @@ func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
// Generate and send the notification.
n := Notification{Type: typ, Data: data}
b.notifications <- &n
b.notifications(&n)
}