-
Notifications
You must be signed in to change notification settings - Fork 25
/
Events.swift
91 lines (81 loc) · 3.49 KB
/
Events.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// Events.swift
// Updated for Swift 6.2 with Xcode 6.3 beta 2
//
// Copyright (c) 2014 Stephen Haney
// MIT License
//
import Foundation
class EventManager {
// using NSMutableArray as Swift arrays can't change size inside dictionaries (yet, probably)
var listeners = Dictionary<String, NSMutableArray>();
// Create a new event listener, not expecting information from the trigger
// + eventName: Matching trigger eventNames will cause this listener to fire
// + action: The block of code you want executed when the event triggers
func listenTo(eventName:String, action: @escaping (()->())) {
let newListener = EventListenerAction(callback: action);
addListener(eventName: eventName, newEventListener: newListener);
}
// Create a new event listener, expecting information from the trigger
// + eventName: Matching trigger eventNames will cause this listener to fire
// + action: The block of code you want executed when the event triggers
func listenTo(eventName:String, action: @escaping ((Any?)->())) {
let newListener = EventListenerAction(callback: action);
addListener(eventName: eventName, newEventListener: newListener);
}
internal func addListener(eventName:String, newEventListener:EventListenerAction) {
if let listenerArray = self.listeners[eventName] {
// action array exists for this event, add new action to it
listenerArray.add(newEventListener);
}
else {
// no listeners created for this event yet, create a new array
self.listeners[eventName] = [newEventListener] as NSMutableArray;
}
}
// Removes all listeners by default, or specific listeners through paramters
// + eventName: If an event name is passed, only listeners for that event will be removed
func removeListeners(eventNameToRemoveOrNil:String?) {
if let eventNameToRemove = eventNameToRemoveOrNil {
// remove listeners for a specific event
if let actionArray = self.listeners[eventNameToRemove] {
// actions for this event exist
actionArray.removeAllObjects();
}
}
else {
// no specific parameters - remove all listeners on this object
self.listeners.removeAll(keepingCapacity: false);
}
}
// Triggers an event
// + eventName: Matching listener eventNames will fire when this is called
// + information: pass values to your listeners
func trigger(eventName:String, information:Any? = nil) {
if let actionObjects = self.listeners[eventName] {
for actionObject in actionObjects {
if let actionToPerform = actionObject as? EventListenerAction {
if let methodToCall = actionToPerform.actionExpectsInfo {
methodToCall(information);
}
else if let methodToCall = actionToPerform.action {
methodToCall();
}
}
}
}
}
}
// Class to hold actions to live in NSMutableArray
class EventListenerAction {
let action:(() -> ())?;
let actionExpectsInfo:((Any?) -> ())?;
init(callback: @escaping (() -> ()) ) {
self.action = callback;
self.actionExpectsInfo = nil;
}
init(callback: @escaping ((Any?) -> ()) ) {
self.actionExpectsInfo = callback;
self.action = nil;
}
}