Last active
November 29, 2017 01:50
-
-
Save jdoiwork/65224b34b784447adb3c25c9b621e017 to your computer and use it in GitHub Desktop.
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
# Eventクラスの作成 | |
# Immutable.jsが必要 | |
class Event | |
constructor: -> | |
@funcs = new Immutable.Set() | |
register: (func) -> | |
@funcs = @funcs.add(func) | |
unregister: (func) -> | |
@funcs = @funcs.delete(func) | |
fire: () -> | |
@funcs.forEach (func) -> | |
func() | |
true | |
Event.create = () -> | |
event = new Event() | |
methods = ['register', 'unregister', 'fire'] | |
methods.forEach (method) -> | |
event[method] = event[method].bind(event) | |
event | |
# Eventクラスの使い方 | |
e = Event.create() | |
e.fire() | |
f = () -> console.log 'Hi' | |
f2 = () -> console.log 'Ho' | |
e.register f | |
e.register f2 | |
e.fire() | |
e.unregister f | |
e.fire() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment