package orm import ( "gnd.la/signals" ) type ormSignal struct { s *signals.Signal } func (s *ormSignal) Listen(handler func(o *Orm)) signals.Listener { return s.s.Listen(func(data interface{}) { handler(data.(*Orm)) }) } func (s *ormSignal) emit(o *Orm) { s.s.Emit(o) } // Signals declares the signals emitted by this package. See // gnd.la/signals for more information. var Signals = struct { // WillInitialize is emitted just before a gnd.la/orm.Orm is // initialized. WillInitialize *ormSignal }{ &ormSignal{signals.New("will-initialize")}, }
handler(data.(*Message)) }) } // emit is unexported, so only this package can emit the signal. Note that // a package might choose to export Emit too. func (s *messageSignal) emit(msg *Message) { s.s.Emit(msg) } // Signals is exported, so package callers can access it and its fields // as e.g. pkg.Signals.MessageReceived.Listen(...) var Signals = struct { MessageReceived *messageSignal }{ &messageSignal{signals.New("message-received")}, } func ExampleSignal() { listener := Signals.MessageReceived.Listen(func(msg *Message) { fmt.Println(msg.Text, msg.Timestamp.Unix()) }) Signals.MessageReceived.emit(&Message{ Text: "hello", Timestamp: time.Unix(42, 0), }) listener.Remove() // Emitting again won't cause the listener to be called // since the listener was removed. Signals.MessageReceived.emit(&Message{ Text: "hello again",
func (s *appSignal) Listen(handler func(app *App)) signals.Listener { return s.signal.Listen(func(data interface{}) { handler(data.(*App)) }) } func (s *appSignal) emit(app *App) { s.signal.Emit(app) } // Signals declares the signals emitted by this package. See // gnd.la/signals for more information. var Signals = struct { // WillListen is emitted just before a *gnd.la/app.App will // start listening. WillListen *appSignal // DidListen is emitted after a *gnd.la/app.App starts // listening. DidListen *appSignal // WillPrepare is emitted at the beginning of App.Prepare. WillPrepare *appSignal // DidPrepare is emitted when App.Prepare ends without errors. DidPrepare *appSignal }{ WillListen: &appSignal{signals.New("will-listen")}, DidListen: &appSignal{signals.New("did-listen")}, WillPrepare: &appSignal{signals.New("will-prepare")}, DidPrepare: &appSignal{signals.New("did-prepare")}, }