github.com/crspeller/mattermost-server@v0.0.0-20190328001957-a200beb3d111/config/emitter.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package config
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/crspeller/mattermost-server/model"
    10  )
    11  
    12  // emitter enables threadsafe registration and broadcasting to configuration listeners
    13  type emitter struct {
    14  	listeners sync.Map
    15  }
    16  
    17  // AddListener adds a callback function to invoke when the configuration is modified.
    18  func (e *emitter) AddListener(listener Listener) string {
    19  	id := model.NewId()
    20  
    21  	e.listeners.Store(id, listener)
    22  
    23  	return id
    24  }
    25  
    26  // RemoveListener removes a callback function using an id returned from AddListener.
    27  func (e *emitter) RemoveListener(id string) {
    28  	e.listeners.Delete(id)
    29  }
    30  
    31  // invokeConfigListeners synchronously notifies all listeners about the configuration change.
    32  func (e *emitter) invokeConfigListeners(oldCfg, newCfg *model.Config) {
    33  	e.listeners.Range(func(key, value interface{}) bool {
    34  		listener := value.(Listener)
    35  		listener(oldCfg, newCfg)
    36  
    37  		return true
    38  	})
    39  }