source: launchers/macosx/I2PLauncher/Utils/EventManager.swift @ 4887707

Last change on this file since 4887707 was 4887707, checked in by meeh <meeh@…>, 22 months ago

OSX Launcher: Log all events for debugging

  • Property mode set to 100644
File size: 3.0 KB
Line 
1//
2//  EventManager.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 22/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Foundation
10
11// TODO: Log all events?
12
13class EventManager {
14  var listeners = Dictionary<String, NSMutableArray>();
15 
16  // Create a new event listener, not expecting information from the trigger
17  // @param eventName: Matching trigger eventNames will cause this listener to fire
18  // @param action: The function/lambda you want executed when the event triggers
19  func listenTo(eventName:String, action: @escaping (()->())) {
20    let newListener = EventListenerAction(callback: action)
21    addListener(eventName: eventName, newEventListener: newListener)
22  }
23 
24  // Create a new event listener, expecting information from the trigger
25  // @param eventName: Matching trigger eventNames will cause this listener to fire
26  // @param action: The function/lambda you want executed when the event triggers
27  func listenTo(eventName:String, action: @escaping ((Any?)->())) {
28    let newListener = EventListenerAction(callback: action)
29    addListener(eventName: eventName, newEventListener: newListener)
30  }
31 
32  internal func addListener(eventName:String, newEventListener:EventListenerAction) {
33    if let listenerArray = self.listeners[eventName] {
34      listenerArray.add(newEventListener)
35    } else {
36      self.listeners[eventName] = [newEventListener] as NSMutableArray
37    }
38  }
39 
40  // Removes all listeners by default, or specific listeners through paramters
41  // @param eventName: If an event name is passed, only listeners for that event will be removed
42  func removeListeners(eventNameToRemoveOrNil:String?) {
43    if let eventNameToRemove = eventNameToRemoveOrNil {
44      if let actionArray = self.listeners[eventNameToRemove] {
45        actionArray.removeAllObjects()
46      }
47    } else {
48      self.listeners.removeAll(keepingCapacity: false)
49    }
50  }
51 
52  // Triggers an event
53  // @param eventName: Matching listener eventNames will fire when this is called
54  // @param information: pass values to your listeners
55  func trigger(eventName:String, information:Any? = nil) {
56    print("Event: ", eventName, " will trigger ", self.listeners[eventName]?.count ?? 0, " listeners")
57    if let actionObjects = self.listeners[eventName] {
58      for actionObject in actionObjects {
59        if let actionToPerform = actionObject as? EventListenerAction {
60          if let methodToCall = actionToPerform.actionExpectsInfo {
61            methodToCall(information)
62          }
63          else if let methodToCall = actionToPerform.action {
64            methodToCall()
65          }
66        }
67      }
68    }
69  }
70}
71
72// Class to hold actions to live in NSMutableArray
73class EventListenerAction {
74  let action:(() -> ())?
75  let actionExpectsInfo:((Any?) -> ())?
76 
77  init(callback: @escaping (() -> ()) ) {
78    self.action = callback
79    self.actionExpectsInfo = nil
80  }
81 
82  init(callback: @escaping ((Any?) -> ()) ) {
83    self.actionExpectsInfo = callback
84    self.action = nil
85  }
86}
Note: See TracBrowser for help on using the repository browser.