-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathProtocol.swift
More file actions
116 lines (95 loc) · 2.66 KB
/
Protocol.swift
File metadata and controls
116 lines (95 loc) · 2.66 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import JavaScriptKit
@JS enum Direction {
case north
case south
case east
case west
}
@JS enum ExampleEnum: String {
case test = "test"
case test2 = "test2"
}
@JS enum Result {
case success(String)
case failure(Int)
}
@JS enum Priority: Int {
case low = -1
case medium = 0
case high = 1
}
@JS class Helper {
@JS var value: Int
@JS init(value: Int) {
self.value = value
}
@JS func increment() {
value += 1
}
}
@JS protocol MyViewControllerDelegate {
var eventCount: Int { get set }
var delegateName: String { get }
var optionalName: String? { get set }
var optionalRawEnum: ExampleEnum? { get set }
var rawStringEnum: ExampleEnum { get set }
var result: Result { get set }
var optionalResult: Result? { get set }
var direction: Direction { get set }
var directionOptional: Direction? { get set }
var priority: Priority { get set }
var priorityOptional: Priority? { get set }
func onSomethingHappened()
func onValueChanged(_ value: String)
func onCountUpdated(count: Int) -> Bool
func onLabelUpdated(_ prefix: String, _ suffix: String)
func isCountEven() -> Bool
func onHelperUpdated(_ helper: Helper)
func createHelper() -> Helper
func onOptionalHelperUpdated(_ helper: Helper?)
func createOptionalHelper() -> Helper?
func createEnum() -> ExampleEnum
func handleResult(_ result: Result)
func getResult() -> Result
}
@JS class MyViewController {
@JS
var delegate: MyViewControllerDelegate
@JS
var secondDelegate: MyViewControllerDelegate?
@JS init(delegate: MyViewControllerDelegate) {
self.delegate = delegate
}
@JS func triggerEvent() {
delegate.onSomethingHappened()
}
@JS func updateValue(_ value: String) {
delegate.onValueChanged(value)
}
@JS func updateCount(_ count: Int) -> Bool {
return delegate.onCountUpdated(count: count)
}
@JS func updateLabel(_ prefix: String, _ suffix: String) {
delegate.onLabelUpdated(prefix, suffix)
}
@JS func checkEvenCount() -> Bool {
return delegate.isCountEven()
}
@JS func sendHelper(_ helper: Helper) {
delegate.onHelperUpdated(helper)
}
}
// Protocol array support
@JS class DelegateManager {
@JS
var delegates: [MyViewControllerDelegate]
@JS init(delegates: [MyViewControllerDelegate]) {
self.delegates = delegates
}
@JS func notifyAll() {
for delegate in delegates {
delegate.onSomethingHappened()
}
}
}
@JS func processDelegates(_ delegates: [MyViewControllerDelegate]) -> [MyViewControllerDelegate]