-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEzyClients.swift
95 lines (81 loc) · 2.82 KB
/
EzyClients.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
92
93
94
95
//
// EzyClients.swift
// hello-swift
//
// Created by Dung Ta Van on 10/30/18.
// Copyright © 2018 Young Monkeys. All rights reserved.
//
import Foundation
class EzyClients {
private var started : Bool
private var defaultClientName : String
private var clients : [String : EzyClient]
private var proxy = EzyClientProxy.getInstance()
private static let INSTANCE = EzyClients()
public static func getInstance() -> EzyClients! {
return INSTANCE;
}
private init() {
started = false
defaultClientName = ""
clients = [String : EzyClient]()
}
public func newClient(config: EzyClientConfig) -> EzyClient {
return newClient(config: config.toDictionary())
}
public func newClient(config: NSDictionary) -> EzyClient {
let client = EzyClient(config: config)
self.addClient(client : client)
if(defaultClientName == "") {
defaultClientName = client.name
}
return client;
}
public func newDefaultClient(config: NSDictionary) -> EzyClient {
let client = self.newClient(config : config)
self.defaultClientName = client.name
return client;
}
public func addClient(client: EzyClient) -> Void {
client.postInit()
self.clients[client.name] = client
}
public func getClient(clientName: String) -> EzyClient {
let client = self.clients[clientName]!
return client;
}
public func getDefaultClient() -> EzyClient {
let client = self.clients[defaultClientName]!
return client
}
public func processEvents() -> Void {
if(started) {
EzyLogger.info(msg: "clients has already started")
}
else {
started = true
addEventDataListers()
startEventsLoop()
}
}
private func startEventsLoop() -> Void {
proxy.run("processEvents", params: NSDictionary() as! [AnyHashable : Any])
}
private func addEventDataListers() {
let eventEmitter = EzyEventEmitter.getInstance()
eventEmitter.setEventListener(String("ezy.event"), listener: {
params in
let client = self.getClient(clientName: params["clientName"] as! String)
let eventType = params["eventType"] as! String
let data = params["data"] as! NSDictionary
client.handleEvent(eventType: eventType, data: data)
});
eventEmitter.setEventListener(String("ezy.data"), listener: {
params in
let client = self.getClient(clientName: params["clientName"] as! String)
let command = params["command"] as! String
let data = params["data"] as! NSArray
client.handleData(command: command, data: data)
});
}
}