source: launchers/macosx/I2PLauncher/Utils/EventManager.swift @ 3b38f5a

Last change on this file since 3b38f5a was 3b38f5a, checked in by meeh <meeh@…>, 23 months ago

Mac OS X Launcher:

  • In general bugfixes
  • Introduced event manager for better control flow
  • Splitted RouterStatusView? to own file
  • Added shell script to setup and produce dmg file
  • Property mode set to 100644
File size: 2.8 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
11class EventManager {
12  var listeners = Dictionary<String, NSMutableArray>();
13 
14  // Create a new event listener, not expecting information from the trigger
15  // @param eventName: Matching trigger eventNames will cause this listener to fire
16  // @param action: The function/lambda you want executed when the event triggers
17  func listenTo(eventName:String, action: @escaping (()->())) {
18    let newListener = EventListenerAction(callback: action)
19    addListener(eventName: eventName, newEventListener: newListener)
20  }
21 
22  // Create a new event listener, expecting information from the trigger
23  // @param eventName: Matching trigger eventNames will cause this listener to fire
24  // @param action: The function/lambda you want executed when the event triggers
25  func listenTo(eventName:String, action: @escaping ((Any?)->())) {
26    let newListener = EventListenerAction(callback: action)
27    addListener(eventName: eventName, newEventListener: newListener)
28  }
29 
30  internal func addListener(eventName:String, newEventListener:EventListenerAction) {
31    if let listenerArray = self.listeners[eventName] {
32      listenerArray.add(newEventListener)
33    } else {
34      self.listeners[eventName] = [newEventListener] as NSMutableArray
35    }
36  }
37 
38  // Removes all listeners by default, or specific listeners through paramters
39  // @param eventName: If an event name is passed, only listeners for that event will be removed
40  func removeListeners(eventNameToRemoveOrNil:String?) {
41    if let eventNameToRemove = eventNameToRemoveOrNil {
42      if let actionArray = self.listeners[eventNameToRemove] {
43        actionArray.removeAllObjects()
44      }
45    } else {
46      self.listeners.removeAll(keepingCapacity: false)
47    }
48  }
49 
50  // Triggers an event
51  // @param eventName: Matching listener eventNames will fire when this is called
52  // @param information: pass values to your listeners
53  func trigger(eventName:String, information:Any? = nil) {
54    if let actionObjects = self.listeners[eventName] {
55      for actionObject in actionObjects {
56        if let actionToPerform = actionObject as? EventListenerAction {
57          if let methodToCall = actionToPerform.actionExpectsInfo {
58            methodToCall(information)
59          }
60          else if let methodToCall = actionToPerform.action {
61            methodToCall()
62          }
63        }
64      }
65    }
66  }
67}
68
69// Class to hold actions to live in NSMutableArray
70class EventListenerAction {
71  let action:(() -> ())?
72  let actionExpectsInfo:((Any?) -> ())?
73 
74  init(callback: @escaping (() -> ()) ) {
75    self.action = callback
76    self.actionExpectsInfo = nil
77  }
78 
79  init(callback: @escaping ((Any?) -> ()) ) {
80    self.actionExpectsInfo = callback
81    self.action = nil
82  }
83}
Note: See TracBrowser for help on using the repository browser.