<commit_before>#
# TODO: Describe this module
#
# @author DeadbraiN
#
module Event
  export Observer
  export on
  export off
  export fire

  #
  # Contains events and they handlers
  #
  type Observer
    #
    # {Dict{ASCIIString, Array{Function}}} array of events and they handlers.
    # For example: {"event"=>[fn1, fn2,...],...}
    # 
    events::Dict{ASCIIString, Array{Function}}
  end

  #
  # Creates new observer instance. It's used in functions like on(), fire(),...
  # @return {Observer}
  #
  function create()
    Observer(Dict{ASCIIString, Array{Function}}())
  end
  #
  # Adds a handler for specified event
  # @param {Observer}    obs   Events container
  # @param {ASCIIString} event Event name. All in lowercase
  # @param {Function}    fn    Event handler
  #
  function on(obs::Observer, event::ASCIIString, fn::Function)
    if !haskey(obs.events, event) obs.events[event] = Function[] end
    push!(obs.events[event], fn)
  end
  #
  # Removed a handler from handlers list for appropriate event
  # @param {Observer}    obs   Events container
  # @param {ASCIIString} event Event name. All in lowercase
  # @param {Function}    fn    Event handler
  #
  function off(obs::Observer, event::ASCIIString, fn::Function)
    index = findfirst(obs.events[event], fn)
    if index > 0 deleteat!(obs.events[event], index) end
  end
  #
  # Fires an event with parameters. User may cancel current action 
  # by returning false in one of event handlers.
  # @param {Observer} obs Events container
  # @param  {ASCIIString} event Event name
  # @param  {Any} args Other arguments
  # @return {Bool} true means that object, which fired an event
  # may continue it's work. false - otherwise.
  #
  function fire(obs::Observer, event::ASCIIString, args...)
    if !haskey(obs.events, event) return nothing end
    fns = obs.events[event]
    for i in fns
      apply(i, args)
    end
  end
end<commit_msg>+ Added test for Event module<commit_after>#
# Implements Listener\Observer pattern. First, you have to create observer
# object by calling create(). After that, it may fire events with parameters.
# 
#
# @author DeadbraiN
#
module Event
  export Observer
  export create
  export on
  export off
  export fire

  #
  # Contains events and they handlers
  #
  type Observer
    #
    # {Dict{ASCIIString, Array{Function}}} array of events and they handlers.
    # For example: {"event"=>[fn1, fn2,...],...}
    # 
    events::Dict{ASCIIString, Array{Function}}
  end

  #
  # Creates new observer instance. It's used in functions like on(), fire(),...
  # @return {Observer}
  #
  function create()
    Observer(Dict{ASCIIString, Array{Function}}())
  end
  #
  # Adds a handler for specified event
  # @param {Observer}    obs   Events container
  # @param {ASCIIString} event Event name. All in lowercase
  # @param {Function}    fn    Event handler
  #
  function on(obs::Observer, event::ASCIIString, fn::Function)
    if !haskey(obs.events, event) obs.events[event] = Function[] end
    push!(obs.events[event], fn)
  end
  #
  # Removed a handler from handlers list for appropriate event
  # @param {Observer}    obs   Events container
  # @param {ASCIIString} event Event name. All in lowercase
  # @param {Function}    fn    Event handler
  #
  function off(obs::Observer, event::ASCIIString, fn::Function)
    index = findfirst(obs.events[event], fn)
    if index > 0 deleteat!(obs.events[event], index) end
  end
  #
  # Fires an event with parameters. User may cancel current action 
  # by returning false in one of event handlers.
  # @param {Observer} obs Events container
  # @param  {ASCIIString} event Event name
  # @param  {Any} args Other arguments
  # @return {Bool} true means that object, which fired an event
  # may continue it's work. false - otherwise.
  #
  function fire(obs::Observer, event::ASCIIString, args...)
    if !haskey(obs.events, event) return nothing end
    fns = obs.events[event]
    for i in fns
      i(args...)
    end
  end
end
{'fraction_non_alphanumeric': 0.11058293720290217, 'fraction_numerical': 0.0015011258443832875, 'mean_word_length': 3.2989247311827956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5838906', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1214, 'n_words': 432}