forked from StephenHaney/Swift-Custom-Events
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvents.swift
More file actions
68 lines (59 loc) · 2.32 KB
/
Events.swift
File metadata and controls
68 lines (59 loc) · 2.32 KB
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
//
// Events.swift
//
// 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
// + 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:(([Any])->())) {
let newListenerAction = EventListenerAction(action);
if let listenerArray = self.listeners[eventName] {
// action array exists for this event, add new action to it
listenerArray.addObject(newListenerAction);
}
else {
// no listeners created for this event yet, create a new array
self.listeners[eventName] = [newListenerAction] 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(keepCapacity: 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] = []) {
if let actions = self.listeners[eventName] {
for actionToPerform in actions {
if actionToPerform is EventListenerAction {
(actionToPerform as EventListenerAction).action(information);
}
}
}
}
}
// Class to hold actions to live in NSMutableArray
class EventListenerAction {
let action:(([Any]) -> ());
init(callback:(([Any]) -> ())) {
self.action = callback;
}
}