Skip to content
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

Expanded pluginfind capabilities. #272

Merged
merged 1 commit into from
Nov 20, 2023
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
12 changes: 12 additions & 0 deletions Sources/Segment/Plugins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ extension DestinationPlugin {
timeline.remove(plugin: plugin)
}

public func find<T: Plugin>(pluginType: T.Type) -> T? {
return timeline.find(pluginType: pluginType)
}

public func findAll<T: Plugin>(pluginType: T.Type) -> [T]? {
return timeline.findAll(pluginType: pluginType)
}

}

extension Analytics {
Expand Down Expand Up @@ -212,6 +220,10 @@ extension Analytics {
return timeline.find(pluginType: pluginType)
}

public func findAll<T: Plugin>(pluginType: T.Type) -> [T]? {
return timeline.findAll(pluginType: pluginType)
}

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

internal func findAll<T: Plugin>(pluginType: T.Type) -> [T]? {
var found = [Plugin]()
for type in PluginType.allCases {
if let mediator = plugins[type] {
found.append(contentsOf: mediator.plugins.filter { (plugin) -> Bool in
return plugin is T
})
}
}
return found as? [T]
}

internal func find(key: String) -> DestinationPlugin? {
var found = [Plugin]()
if let mediator = plugins[.destination] {
Expand Down
25 changes: 25 additions & 0 deletions Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -754,4 +754,29 @@ final class Analytics_Tests: XCTestCase {
// it's running in sync mode.
XCTAssertEqual(analytics.pendingUploads!.count, 0)
}

func testFindAll() {
let analytics = Analytics(configuration: Configuration(writeKey: "testFindAll")
.flushInterval(9999)
.flushAt(9999)
.operatingMode(.synchronous))

analytics.add(plugin: ZiggyPlugin())
analytics.add(plugin: ZiggyPlugin())
analytics.add(plugin: ZiggyPlugin())

let myDestination = MyDestination()
myDestination.add(plugin: GooberPlugin())
myDestination.add(plugin: GooberPlugin())

analytics.add(plugin: myDestination)

waitUntilStarted(analytics: analytics)

let ziggysFound = analytics.findAll(pluginType: ZiggyPlugin.self)
let goobersFound = myDestination.findAll(pluginType: GooberPlugin.self)

XCTAssertEqual(ziggysFound!.count, 3)
XCTAssertEqual(goobersFound!.count, 2)
}
}