Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemoveListener support closure function; fix: #9 #41

Merged
merged 7 commits into from
Jan 17, 2023
Merged

RemoveListener support closure function; fix: #9 #41

merged 7 commits into from
Jan 17, 2023

Conversation

RelicOfTesla
Copy link
Contributor

@RelicOfTesla RelicOfTesla commented Jan 14, 2023

fix #9

@inhere inhere changed the title RemoveListener support closure function; #9 RemoveListener support closure function; fix: #9 Jan 15, 2023
@inhere inhere added the enhancement New feature or request label Jan 15, 2023
@RelicOfTesla
Copy link
Contributor Author

还有个简易方法,使用reflect.Value做比较。

func getListenCompareKey(src Listener) reflect.Value {
	return reflect.ValueOf(src)
}

缺点就是不支持传入reflect.Struct的Listener,必须是指针型的Listener或func。也就是必须On(...)时,对传入的Listener进行判断,panic掉struct的Listener,强制要求&xxx{}或new或ListenerFunc类型进来

func (em *Manager) addListenerItem(name string, li *ListenerItem) {
	if name != Wildcard {
		name = goodName(name)
	}
	if li.Listener == nil {
		panic("event: the event '" + name + "' listener cannot be empty")
	}
	if reflect.ValueOf(li.Listener).Kind() == reflect.Struct {
		panic("must use pointer Listener, don't use struct Listener")
	}

	// exists, append it.
	if lq, ok := em.listeners[name]; ok {
		lq.Push(li)
	} else { // first add.
		em.listenedNames[name] = 1
		em.listeners[name] = (&ListenerQueue{}).Push(li)
	}
}

我另外push一个过来

@inhere
Copy link
Member

inhere commented Jan 17, 2023

@RelicOfTesla 感谢PR 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RemoveListener not support closure function
2 participants