<commit_before>{safePush, safeDelete} = require './helpers'

class Event
  constructor: (@object, @name, @options) ->
    @prop = "_s_#{@name}_listeners"
    @queue_name = "_s_#{name}_queue"
    @queue = if @object.hasOwnProperty(@queue_name)
      @object[@queue_name]
    else
      @object[@queue_name] = []

  trigger: (args...) ->
    @queue.push(args)
    if @options.async
      clearTimeout(@queue.timeout)
      @queue.timeout = setTimeout((=> @resolve()), 0)
    else
      @resolve()

  bind: (fun) ->
    @options.bind.call(@object, fun) if @options.bind
    safePush(@object, @prop, fun)

  one: (fun) ->
    unbind = (fun) => @unbind(fun)
    @bind ->
      unbind(arguments.callee)
      fun.apply(@, arguments)

  unbind: (fun) ->
    safeDelete(@object, @prop, fun)
    @options.unbind.call(@object, fun) if @options.unbind

  resolve: ->
    perform = (args) =>
      if @object[@prop]
        @object[@prop].forEach (fun) =>
          fun.apply(@object, args)
    if @options.optimize
      perform(@options.optimize(@queue))
    else
      perform(args) for args in @queue
    @queue = @object[@queue_name] = []

  Object.defineProperty @prototype, "listeners", get: ->
    @object[@prop]<commit_msg>We can just use the old timeout, no need to reset it<commit_after>{safePush, safeDelete} = require './helpers'

class Event
  constructor: (@object, @name, @options) ->
    @prop = "_s_#{@name}_listeners"
    @queue_name = "_s_#{name}_queue"
    @queue = if @object.hasOwnProperty(@queue_name)
      @object[@queue_name]
    else
      @object[@queue_name] = []

  trigger: (args...) ->
    @queue.push(args)
    if @options.async
      @queue.timeout or= setTimeout((=> @resolve()), 0)
    else
      @resolve()

  bind: (fun) ->
    @options.bind.call(@object, fun) if @options.bind
    safePush(@object, @prop, fun)

  one: (fun) ->
    unbind = (fun) => @unbind(fun)
    @bind ->
      unbind(arguments.callee)
      fun.apply(@, arguments)

  unbind: (fun) ->
    safeDelete(@object, @prop, fun)
    @options.unbind.call(@object, fun) if @options.unbind

  resolve: ->
    perform = (args) =>
      if @object[@prop]
        @object[@prop].forEach (fun) =>
          fun.apply(@object, args)
    if @options.optimize
      perform(@options.optimize(@queue))
    else
      perform(args) for args in @queue
    @queue = @object[@queue_name] = []

  Object.defineProperty @prototype, "listeners", get: ->
    @object[@prop]
{'fraction_non_alphanumeric': 0.17211774325429272, 'fraction_numerical': 0.0008176614881439084, 'mean_word_length': 2.946774193548387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '758770', 'n_tokens_mistral': 820, 'n_tokens_neox': 815, 'n_words': 178}