Skip to content

Commit

Permalink
Expanded pluginfind capabilities. (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsneed authored Nov 20, 2023
1 parent c4bb71d commit 86aeff4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
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)
}
}

0 comments on commit 86aeff4

Please sign in to comment.