Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Groovy++ pattern for listeners
abstract static class Listener<T> {
abstract void call(T oldValue, T newValue)
}
private FList<Listener<T>> listeners = FList.emptyList
final Listener<T> addListener(Listener<T> listener, Executor executor = null) {
if(executor) {
def myListener = listener
listener = { oldValue, newValue ->
executor.execute {
myListener(oldValue, newValue)
}
}
}
for(;;) {
def old = listeners
if(listeners.compareAndSet(old, old + listener))
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment