github.com/hyperledger/aries-framework-go@v0.3.2/pkg/client/messaging/notifier.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package messaging
     8  
     9  // NotificationPayload represent notification payload.
    10  type NotificationPayload struct {
    11  	Topic string
    12  	Raw   []byte
    13  }
    14  
    15  // FilterNotification filters incoming notifications.
    16  type FilterNotification func(string, []byte) bool
    17  
    18  // BasicNotifier is channel based implementation of basic notifier.
    19  type BasicNotifier struct {
    20  	connection chan<- NotificationPayload
    21  	filter     FilterNotification
    22  }
    23  
    24  // NewNotifier return notifier instance.
    25  func NewNotifier(connection chan NotificationPayload, filter FilterNotification) *BasicNotifier {
    26  	if filter == nil {
    27  		filter = func(string, []byte) bool { return true }
    28  	}
    29  
    30  	return &BasicNotifier{connection: connection, filter: filter}
    31  }
    32  
    33  // Notify sends the given message to the subscribers based on filter logic.
    34  func (n *BasicNotifier) Notify(topic string, message []byte) error {
    35  	if n.filter(topic, message) {
    36  		n.connection <- NotificationPayload{
    37  			Topic: topic,
    38  			Raw:   message,
    39  		}
    40  	}
    41  
    42  	return nil
    43  }