github.com/mattermost/mattermost-server/v5@v5.39.3/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/mattermost/mattermost-server/v5/model"
    10  	"github.com/mattermost/mattermost-server/v5/shared/mlog"
    11  )
    12  
    13  // Listener is a callback function invoked when the configuration changes.
    14  type Listener func(oldCfg, newCfg *model.Config)
    15  
    16  // emitter enables threadsafe registration and broadcasting to configuration listeners
    17  type emitter struct {
    18  	listeners sync.Map
    19  }
    20  
    21  // AddListener adds a callback function to invoke when the configuration is modified.
    22  func (e *emitter) AddListener(listener Listener) string {
    23  	id := model.NewId()
    24  	e.listeners.Store(id, listener)
    25  	return id
    26  }
    27  
    28  // RemoveListener removes a callback function using an id returned from AddListener.
    29  func (e *emitter) RemoveListener(id string) {
    30  	e.listeners.Delete(id)
    31  }
    32  
    33  // invokeConfigListeners synchronously notifies all listeners about the configuration change.
    34  func (e *emitter) invokeConfigListeners(oldCfg, newCfg *model.Config) {
    35  	e.listeners.Range(func(key, value interface{}) bool {
    36  		listener := value.(Listener)
    37  		listener(oldCfg, newCfg)
    38  		return true
    39  	})
    40  }
    41  
    42  // srcEmitter enables threadsafe registration and broadcasting to configuration listeners
    43  type logSrcEmitter struct {
    44  	listeners sync.Map
    45  }
    46  
    47  // AddListener adds a callback function to invoke when the configuration is modified.
    48  func (e *logSrcEmitter) AddListener(listener LogSrcListener) string {
    49  	id := model.NewId()
    50  	e.listeners.Store(id, listener)
    51  	return id
    52  }
    53  
    54  // RemoveListener removes a callback function using an id returned from AddListener.
    55  func (e *logSrcEmitter) RemoveListener(id string) {
    56  	e.listeners.Delete(id)
    57  }
    58  
    59  // invokeConfigListeners synchronously notifies all listeners about the configuration change.
    60  func (e *logSrcEmitter) invokeConfigListeners(oldCfg, newCfg mlog.LogTargetCfg) {
    61  	e.listeners.Range(func(key, value interface{}) bool {
    62  		listener := value.(LogSrcListener)
    63  		listener(oldCfg, newCfg)
    64  		return true
    65  	})
    66  }