Skip to content

Instantly share code, notes, and snippets.

@jdoiwork
Last active November 29, 2017 01:50
Show Gist options
  • Save jdoiwork/65224b34b784447adb3c25c9b621e017 to your computer and use it in GitHub Desktop.
Save jdoiwork/65224b34b784447adb3c25c9b621e017 to your computer and use it in GitHub Desktop.
# 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