<commit_before>###
Mediator pattern for window event listeners

Example Usage:
	mediator = require 'window-event-mediator'
	mediator.on 'resize', myCallback
	mediator.off 'scroll', mycallback
###
_ = require 'lodash'

class WindowEventMediator
	constructor: -> return

	# Stores a record on all event listener types, subscribed callbacks, and
	# Throttle/Debounce option keys
	handlers: {}

	# Default options
	defaults:
		throttle: 100
		debounce: 0

	###
	Register an event handler with the mediator

	event - string - event name ('resize', 'scroll', etc)
	callback - function
	options
		throttle [100] - int - milliseconds to throttle
		debounce [100] - int - milliseconds to debounce
	###
	on: (event, callback, options) =>
		options = _.merge {}, @defaults, options
		@set event, callback, options

	###
	Removes any reference to the event type and callback, regardless of
	Throttle or Debounce options.

	event - string - event name ('resize', 'scroll', etc)
	callback - function
	###
	off: (event, callback) =>
		_.pull @handlers[event].callbacks, callback

	###
	Create an event container for the window event type and
	add the actual listener to the window object

	event - string - event name ('resize', 'scroll', etc)
	###
	set: (event, callback, options) =>
		key = options.throttle+'-'+options.debounce

		# If the event type hasn't been added, create an object to store the callbacks
		# and a record of which throttle.debounce listeners have been added
		if !@handlers[event]?
			@handlers[event] = {callbacks: [], keys: {}}

		# Only add the window listener if it doesn't exist
		if !@handlers[event].keys[key]?
			@handlers[event].keys[key] = true
			window.addEventListener event,
				_.debounce(_.throttle(@fire, options.throttle), options.debounce)

		# Save the callback references
		@handlers[event].callbacks.push callback

	###
	Fires all events for a given window event type,
	padding the native event object through to the mediated callback

	e - Event - native event object
	###
	fire: (e) =>
		events = @handlers[e.type]
		event(e) for event in events.callbacks

module.exports = new WindowEventMediator()<commit_msg>Loop through the callbacks backwards<commit_after>###
Mediator pattern for window event listeners

Example Usage:
	mediator = require 'window-event-mediator'
	mediator.on 'resize', myCallback
	mediator.off 'scroll', mycallback
###
_ = require 'lodash'

class WindowEventMediator
	constructor: -> return

	# Stores a record on all event listener types, subscribed callbacks, and
	# Throttle/Debounce option keys
	handlers: {}

	# Default options
	defaults:
		throttle: 100
		debounce: 0

	###
	Register an event handler with the mediator

	event - string - event name ('resize', 'scroll', etc)
	callback - function
	options
		throttle [100] - int - milliseconds to throttle
		debounce [100] - int - milliseconds to debounce
	###
	on: (event, callback, options) =>
		options = _.merge {}, @defaults, options
		@set event, callback, options

	###
	Removes any reference to the event type and callback, regardless of
	Throttle or Debounce options.

	event - string - event name ('resize', 'scroll', etc)
	callback - function
	###
	off: (event, callback) =>
		_.pull @handlers[event].callbacks, callback

	###
	Create an event container for the window event type and
	add the actual listener to the window object

	event - string - event name ('resize', 'scroll', etc)
	###
	set: (event, callback, options) =>
		key = options.throttle+'-'+options.debounce

		# If the event type hasn't been added, create an object to store the callbacks
		# and a record of which throttle.debounce listeners have been added
		if !@handlers[event]?
			@handlers[event] = {callbacks: [], keys: {}}

		# Only add the window listener if it doesn't exist
		if !@handlers[event].keys[key]?
			@handlers[event].keys[key] = true
			window.addEventListener event,
				_.debounce(_.throttle(@fire, options.throttle), options.debounce)

		# Save the callback references
		@handlers[event].callbacks.push callback

	###
	Fires all events for a given window event type,
	padding the native event object through to the mediated callback

	e - Event - native event object
	###
	fire: (e) =>
		_.eachRight @handlers[e.type].callbacks, (cb) -> cb(e); true

module.exports = new WindowEventMediator()
{'fraction_non_alphanumeric': 0.10822108685555039, 'fraction_numerical': 0.004644681839294009, 'mean_word_length': 4.085005903187722, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '447286', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1265, 'n_words': 475}