Skip to content

Commit 2d34440

Browse files
bsneedBrandon Sneed
and
Brandon Sneed
authored
Added ability to find a plugin by destination key. (#143)
Co-authored-by: Brandon Sneed <[email protected]>
1 parent ca8668c commit 2d34440

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Sources/Segment/Plugins.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,8 @@ extension Analytics {
159159
public func find<T: Plugin>(pluginType: T.Type) -> T? {
160160
return timeline.find(pluginType: pluginType)
161161
}
162+
163+
public func find(key: String) -> DestinationPlugin? {
164+
return timeline.find(key: key)
165+
}
162166
}

Sources/Segment/Timeline.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ extension Timeline {
135135
}
136136
return found.first as? T
137137
}
138+
139+
internal func find(key: String) -> DestinationPlugin? {
140+
var found = [Plugin]()
141+
if let mediator = plugins[.destination] {
142+
found.append(contentsOf: mediator.plugins.filter{ plugin in
143+
guard let p = plugin as? DestinationPlugin else { return false }
144+
return p.key == key
145+
})
146+
}
147+
return found.first as? DestinationPlugin
148+
}
138149
}
139150

140151
// MARK: - Plugin Timeline Execution

Tests/Segment-Tests/Analytics_Tests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ final class Analytics_Tests: XCTestCase {
7676

7777
analytics.track(name: "testDestinationEnabled")
7878

79+
let dest = analytics.find(key: myDestination.key)
80+
XCTAssertNotNil(dest)
81+
XCTAssertTrue(dest is MyDestination)
82+
7983
wait(for: [expectation], timeout: 1.0)
8084
}
8185

0 commit comments

Comments
 (0)