Skip to content

Instantly share code, notes, and snippets.

@patrickfox
Last active August 29, 2015 14:23
Show Gist options
  • Save patrickfox/c9d29ab6f319364dfe3f to your computer and use it in GitHub Desktop.
Save patrickfox/c9d29ab6f319364dfe3f to your computer and use it in GitHub Desktop.
PubShlub - YAPS library

#PubShlub

do ($) ->
	a = {}
	$.publish = (d, c) ->
		a[d] and $.each(a[d], ->
			if @apply
				@apply $, c or []
			)

	$.subscribe = (c, d) ->
		a[c] = []  unless a[c]
		a[c].push d
		[c, d]

	$.unsubscribe = (event_name, handler) ->
		if a[event_name]
			counter = 0
			for registered_handler in a[event_name]
				if registered_handler is handler then a[event_name].splice counter, 1
				counter++

	$.subscriptions = a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment