Skip to content

Commit b55959b

Browse files
authored
Merge pull request #79 from Rolfrider/main
Fix emitter param passing
2 parents 65c16b8 + 3ffdc85 commit b55959b

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

Sources/FeaturevisorSDK/Emitter.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public enum EventName {
55
case activation
66
}
77

8-
public typealias Listener = (@escaping (Any...) -> Void) -> Void
8+
public typealias Listener = (Any...) -> Void
99

1010
public typealias Listeners = [EventName: [Listener]]
1111

@@ -40,9 +40,7 @@ public class Emitter {
4040
public func emit(_ eventName: EventName, _ args: Any...) {
4141
if let listeners = self.listeners[eventName] {
4242
for listener in listeners {
43-
listener({
44-
(args: Any...) -> Void in
45-
})
43+
listener(args)
4644
}
4745
}
4846
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import XCTest
2+
3+
@testable import FeaturevisorSDK
4+
5+
final class EmitterTests: XCTestCase {
6+
7+
func testEmitterInvokeListener() {
8+
9+
let emitter = Emitter()
10+
var isListenerCalled: Bool = false
11+
12+
emitter.addListener(.activation) { _ in
13+
isListenerCalled = true
14+
}
15+
16+
emitter.emit(.activation, ["test", 1, true])
17+
18+
XCTAssertTrue(isListenerCalled)
19+
}
20+
21+
func testEmitterNotInvokeRemovedListener() {
22+
23+
let emitter = Emitter()
24+
var isListenerCalled: Bool = false
25+
26+
emitter.addListener(.activation) { _ in
27+
isListenerCalled = true
28+
}
29+
30+
emitter.removeAllListeners(.activation)
31+
32+
emitter.emit(.activation, ["test", 1, true])
33+
34+
XCTAssertFalse(isListenerCalled)
35+
}
36+
37+
func testEmitterPassParamsToListener() {
38+
let emitter = Emitter()
39+
var params: [Any] = []
40+
41+
emitter.addListener(.activation) { receivedParams in
42+
params = receivedParams
43+
}
44+
45+
emitter.emit(.activation, ["test", 1, true])
46+
47+
XCTAssertEqual(params.count, 3)
48+
XCTAssertEqual(params[0] as? String, "test")
49+
XCTAssertEqual(params[1] as? Int, 1)
50+
XCTAssertEqual(params[2] as? Bool, true)
51+
}
52+
53+
}

0 commit comments

Comments
 (0)