github.com/zly-app/zapp@v1.3.3/component/component.go (about)

     1  /*
     2  -------------------------------------------------
     3     Author :       zlyuancn
     4     date:         2021/1/20
     5     Description :
     6  -------------------------------------------------
     7  */
     8  
     9  package component
    10  
    11  import (
    12  	"github.com/zly-app/zapp/component/gpool"
    13  	"github.com/zly-app/zapp/component/msgbus"
    14  	"github.com/zly-app/zapp/core"
    15  	"github.com/zly-app/zapp/logger"
    16  )
    17  
    18  var defaultComponent core.IComponent
    19  
    20  type ComponentCli struct {
    21  	app    core.IApp
    22  	config *core.Config
    23  	core.ILogger
    24  
    25  	core.IGPools
    26  	core.IMsgbus
    27  }
    28  
    29  func (c *ComponentCli) App() core.IApp       { return c.app }
    30  func (c *ComponentCli) Config() *core.Config { return c.config }
    31  
    32  func (c *ComponentCli) Close() {
    33  	c.IGPools.Close()
    34  	c.IMsgbus.Close()
    35  }
    36  
    37  func NewComponent(app core.IApp) core.IComponent {
    38  	var c core.IComponent = &ComponentCli{
    39  		app:     app,
    40  		config:  app.GetConfig().Config(),
    41  		ILogger: app.GetLogger(),
    42  
    43  		IGPools: gpool.NewGPools(),
    44  		IMsgbus: msgbus.NewMsgbus(),
    45  	}
    46  	defaultComponent = c
    47  	return c
    48  }
    49  
    50  // 获取component
    51  func GetComponent() core.IComponent {
    52  	if defaultComponent == nil {
    53  		logger.Log.Panic("Component is uninitialized")
    54  	}
    55  	return defaultComponent
    56  }
    57  
    58  // 重置component
    59  func ResetComponent(component core.IComponent) {
    60  	defaultComponent = component
    61  }