Skip to content

Added ability to find a plugin by destination key. #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/Segment/Plugins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,8 @@ extension Analytics {
public func find<T: Plugin>(pluginType: T.Type) -> T? {
return timeline.find(pluginType: pluginType)
}

public func find(key: String) -> DestinationPlugin? {
return timeline.find(key: key)
}
}
11 changes: 11 additions & 0 deletions Sources/Segment/Timeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ extension Timeline {
}
return found.first as? T
}

internal func find(key: String) -> DestinationPlugin? {
var found = [Plugin]()
if let mediator = plugins[.destination] {
found.append(contentsOf: mediator.plugins.filter{ plugin in
guard let p = plugin as? DestinationPlugin else { return false }
return p.key == key
})
}
return found.first as? DestinationPlugin
}
}

// MARK: - Plugin Timeline Execution
Expand Down
4 changes: 4 additions & 0 deletions Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ final class Analytics_Tests: XCTestCase {

analytics.track(name: "testDestinationEnabled")

let dest = analytics.find(key: myDestination.key)
XCTAssertNotNil(dest)
XCTAssertTrue(dest is MyDestination)

wait(for: [expectation], timeout: 1.0)
}

Expand Down