Skip to content

Commit

Permalink
Move EventManager from Global.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
vapidinfinity committed Nov 21, 2023
1 parent ce2292e commit 1eca7b4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
34 changes: 0 additions & 34 deletions Mythic/Extensions/Base/Global.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,6 @@

import Foundation

// A class that allows for cross-script variables and communication
// Example usage:
/*
EventManager.shared.subscribe("test") { data in
if let value = data as? String {
print("chat is this \(value)")

}
}

EventManager.shared.publish("test", "real?")
*/

class EventManager {
static let shared = EventManager()

var events = [String: [(Any) -> Void]]()

func subscribe(_ event: String, _ callback: @escaping (Any) -> Void) {
if events[event] == nil {
events[event] = Array()
}
events[event]?.append(callback)
}

func publish(_ event: String, _ data: Any) {
if let callbacks = events[event] {
for callback in callbacks {
callback(data)
}
}
}
}

func isAppInstalled(bundleIdentifier: String) -> Bool {
let process = Process()
process.launchPath = "/usr/bin/env"
Expand Down
49 changes: 49 additions & 0 deletions Mythic/Utilities/Base/EventManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// EventManager.swift
// Mythic
//
// Created by Esiayo Alegbe on 21/11/2023.
//

import Foundation

// A class that allows for cross-script variables and communication
// Example usage:
/*
EventManager.shared.subscribe("test") { data in
if let value = data as? String {
print("chat is this \(value)")

}
}

EventManager.shared.publish("test", "real?")
*/

/// Allows for cross-script communication via subscribeable events
class EventManager {
/// The shared instance for events
static let shared = EventManager()

/// Event storage
private var events = [String: [(Any) -> Void]]()

/// Subscribe to events within the event manager
/// - Parameter event: The event to subscribe to.
public func subscribe(_ event: String, _ callback: @escaping (Any) -> Void) {
if events[event] == nil {
events[event] = Array()
}
events[event]?.append(callback)
}

/// Publish new values to events
/// -
public func publish(_ event: String, _ data: Any) {
if let callbacks = events[event] {
for callback in callbacks {
callback(data)
}
}
}
}

0 comments on commit 1eca7b4

Please sign in to comment.