-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c359cae
commit 7cda5b2
Showing
8 changed files
with
185 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Hao Yu on 11/9/22. | ||
// | ||
|
||
import Foundation | ||
|
||
internal class Mediator { | ||
// create an array with certain type. | ||
internal var plugins = [Plugin]() | ||
|
||
internal func add(plugin: Plugin) { | ||
plugins.append(plugin) | ||
} | ||
|
||
internal func remove(plugin: Plugin) { | ||
plugins.removeAll { (storedPlugin) -> Bool in | ||
return storedPlugin === plugin | ||
} | ||
} | ||
|
||
internal func execute(event: BaseEvent) -> BaseEvent? { | ||
var result : BaseEvent? = event; | ||
plugins.forEach { plugin in | ||
if let r = result { | ||
if plugin is DestinationPlugin { | ||
_ = plugin.execute(event: r) | ||
} else if let p = plugin as? EventPlugin { | ||
result = p.execute(event: r) | ||
if let rr = result { | ||
if let identifyEvent = rr as? IdentifyEvent { | ||
result = p.identify(event: identifyEvent) | ||
} else if let groupIdentifyEvent = rr as? GroupIdentifyEvent { | ||
result = p.groupIdentify(event: groupIdentifyEvent) | ||
} else if let revenueEvent = rr as? RevenueEvent { | ||
result = p.revenue(event: revenueEvent) | ||
} else { | ||
result = p.track(event: rr) | ||
} | ||
} | ||
} else { | ||
result = plugin.execute(event: event) | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
internal func applyClosure(_ closure: (Plugin) -> Void) { | ||
plugins.forEach { plugin in | ||
closure(plugin) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
@testable import Amplitude_Swift | ||
|
||
class testEnrichmentPlugin : Plugin { | ||
let type: PluginType | ||
var amplitude: Amplitude? | ||
let trackCompletion: (() -> Bool)? | ||
|
||
init(trackCompletion: (() -> Bool)? = nil) { | ||
self.type = PluginType.enrichment | ||
self.trackCompletion = trackCompletion | ||
} | ||
|
||
func setup(amplitude: Amplitude) { | ||
self.amplitude = amplitude; | ||
} | ||
|
||
func execute(event: BaseEvent) -> BaseEvent? { | ||
var returnEvent: BaseEvent? = event | ||
if let completion = trackCompletion { | ||
if !completion() { | ||
returnEvent = nil | ||
} | ||
} | ||
return returnEvent | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Foundation | ||
import XCTest | ||
|
||
@testable import Amplitude_Swift | ||
|
||
final class TimelineTest: XCTestCase { | ||
private var timeline: Timeline! | ||
|
||
func testTimeline() { | ||
let expectation = XCTestExpectation(description: "First Plugin") | ||
let testPlugin = testEnrichmentPlugin { | ||
expectation.fulfill() | ||
return true | ||
} | ||
|
||
let amplitude = Amplitude(configuration: Configuration(apiKey: "testApiKey")) | ||
amplitude.add(plugin: testPlugin) | ||
amplitude.track(event: BaseEvent(eventType: "testEvent")) | ||
|
||
wait(for: [expectation], timeout: 1.0) | ||
} | ||
|
||
func testTimelineWithTwoPlugin() { | ||
let expectation = XCTestExpectation(description: "First Plugin") | ||
let expectation2 = XCTestExpectation(description: "Second Plugin") | ||
let testPlugin = testEnrichmentPlugin { | ||
expectation.fulfill() | ||
return true | ||
} | ||
|
||
let testPlugin2 = testEnrichmentPlugin { | ||
expectation2.fulfill() | ||
return true | ||
} | ||
|
||
let amplitude = Amplitude(configuration: Configuration(apiKey: "testApiKey")) | ||
amplitude.add(plugin: testPlugin) | ||
amplitude.add(plugin: testPlugin2) | ||
amplitude.track(event: BaseEvent(eventType: "testEvent")) | ||
|
||
wait(for: [expectation, expectation2], timeout: 1.0) | ||
} | ||
} |