asaskevich / EventBus

[Go] Lightweight eventbus with async compatibility for Go
MIT License
1.74k stars 220 forks source link

does it support subscibe cross package in a process? #17

Closed whidbey closed 8 years ago

whidbey commented 8 years ago

I try but fail.

bennAH commented 8 years ago

What exactly did you try? Specifically if you have sample code that illustrates your attempt, that would be helpful

whidbey commented 8 years ago

package a s:=new eventbus s.subsribe("some",funcs); package b s2:=new eventbus s2.pub(“some")

dont work.

package a var s eventbus func Pass(x eventbus){ s=x; } s.subsribe("some",funcs); package b s2:=new eventbus a.Pass(s2) s2.pub(“some")

works.

bennAH commented 8 years ago

I think this is a non-issue, more of a misunderstanding.

In your 1st example, you have created 2 event buses. Each time you call EventBus.New(), it creates a new EventBus with a new state.

If you want to ensure you only ever use the same instance of the EventBus across multiple packages, you can declare an EventBus as an exported variable of the package.

Alternatively, see this example:

https://github.com/getlantern/lantern/blob/e6fa5e830b1f4b88affb92d5fcad784982fb83aa/src/github.com/getlantern/flashlight/pubsub/pubsub.go

Feel free to correct me if I am wrong.

whidbey commented 8 years ago

you are right,thats what exactly needs.great and thank you