~whereswaldon/forest-go

d3d4a318419f05b403681d2ccbafb2b5c2cc7e06 — Chris Waldon a month ago 529ac92
feat: suppress extra archive notifications

Previously, archive.Add-ing a node would *always* send a
notification to all subscribers, but this behavior wasn't
relied upon anywhere. It's much more useful if the notification
is only sent when the node is not already in the archive, so
this commit changes the behavior to enforce that.
1 files changed, 8 insertions(+), 7 deletions(-)

M store/archive.go
M store/archive.go => store/archive.go +8 -7
@@ 177,20 177,21 @@ func (m *Archive) Recent(nodeType fields.NodeType, quantity int) (nodes []forest
// Add inserts a node into the underlying store. Importantly, this will send a notification
// of a new node to *all* subscribers. If the calling code is a subscriber, it will still
// be notified of the new node. To supress this, use AddAs() instead.
//
// Subscribers will only be notified if the node is not already present in the archive.
func (m *Archive) Add(node forest.Node) (err error) {
	m.executeAsync(func() {
		m.notifySubscribed(m.preAddSubscribers, node, neverAssigned)
		if err = m.store.Add(node); err == nil {
			m.notifySubscribed(m.postAddSubscribers, node, neverAssigned)
		}
	})
	return
	return m.AddAs(node, neverAssigned)
}

// AddAs allows adding a node to the underlying store without being notified
// of it as a new node. The addedByID (subscription id returned from SubscribeToNewMessages)
// will not be notified of the new nodes, but all other subscribers will be.
//
// Subscribers will only be notified if the node is not already present in the archive.
func (m *Archive) AddAs(node forest.Node, addedByID Subscription) (err error) {
	if _, has, _ := m.Get(node.ID()); has {
		return
	}
	m.executeAsync(func() {
		m.notifySubscribed(m.preAddSubscribers, node, addedByID)
		if err = m.store.Add(node); err == nil {