Last active
December 18, 2015 16:39
-
-
Save pierr/5813065 to your computer and use it in GitHub Desktop.
Mediator pattern in coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Pierre | |
# Implémentation d'un médiateur | |
### | |
Mediator | |
publish (event [args]) | |
subscribe (event, cb) | |
unsubscribe (event, cb) | |
### | |
## Correction de Chistophe | |
Mediator = | |
subscriptions:{} | |
subscribe: (event, cb) -> | |
container = @subscriptions[event] ?= [] | |
return false if cb in container | |
container.push cb | |
true | |
unsubscribe: (event, cb) -> | |
return unless container = @subscriptions[event] | |
return false unless i = container.indexOf cb | |
container.splice i, 1 | |
true | |
publish: (event, args...)-> | |
cb args... for cb in @subscriptions[event] ? [] | |
true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment