Skip to content

Commit

Permalink
fix: lints and other comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhao900914 committed Nov 14, 2022
1 parent 7cda5b2 commit 296951b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Sources/Amplitude/Amplitude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Amplitude {
) {
self.configuration = configuration
self.instanceName = instanceName
//_ = add(LifecyclePlugin())
// _ = add(LifecyclePlugin())
_ = add(plugin: ContextPlugin())
_ = add(plugin: AmplitudeDestinationPlugin())
}
Expand Down
14 changes: 6 additions & 8 deletions Sources/Amplitude/Mediator.swift
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
//
// File.swift
//
// Mediator.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;
var result: BaseEvent? = event
plugins.forEach { plugin in
if let r = result {
if plugin is DestinationPlugin {
Expand All @@ -47,7 +45,7 @@ internal class Mediator {
}
return result
}

internal func applyClosure(_ closure: (Plugin) -> Void) {
plugins.forEach { plugin in
closure(plugin)
Expand Down
15 changes: 6 additions & 9 deletions Sources/Amplitude/Timeline.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
//
// File.swift
// Timeline.swift
//
//
// Created by Marvin Liu on 10/27/22.
//

import Foundation

public class Timeline {
internal let plugins: [PluginType: Mediator]


init() {
self.plugins = [
PluginType.before: Mediator(),
PluginType.enrichment: Mediator(),
PluginType.destination: Mediator(),
PluginType.utility: Mediator()
PluginType.utility: Mediator(),
]
}

Expand All @@ -29,13 +26,13 @@ public class Timeline {

internal func applyPlugin(pluginType: PluginType, event: BaseEvent?) -> BaseEvent? {
var result: BaseEvent? = event
if let mediator = plugins[pluginType], let e = event {
result = mediator.execute(event: e)
if let mediator = plugins[pluginType] {
result = mediator.execute(event: event!)
}
return result

}

internal func add(plugin: Plugin) {
if let mediator = plugins[plugin.type] {
mediator.add(plugin: plugin)
Expand Down
6 changes: 2 additions & 4 deletions Sources/Amplitude/Types.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//
// File.swift
// Types.swift
//
//
// Created by Marvin Liu on 10/27/22.
//

import Foundation

public struct Plan {
var branch: String?
var source: String?
Expand Down Expand Up @@ -49,7 +47,7 @@ public enum PluginType: String {
public protocol Plugin: AnyObject {
var type: PluginType { get }
var amplitude: Amplitude? { get set }
func setup(amplitude: Amplitude)
func setup(amplitude: Amplitude)
func execute(event: BaseEvent) -> BaseEvent?
}

Expand Down
14 changes: 6 additions & 8 deletions Tests/AmplitudeTests/Supports/TestUtilities.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
@testable import Amplitude_Swift

class testEnrichmentPlugin : Plugin {
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;
self.amplitude = amplitude
}

func execute(event: BaseEvent) -> BaseEvent? {
var returnEvent: BaseEvent? = event
if let completion = trackCompletion {
Expand All @@ -23,7 +23,5 @@ class testEnrichmentPlugin : Plugin {
}
return returnEvent
}




}
20 changes: 10 additions & 10 deletions Tests/AmplitudeTests/Timeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ import XCTest

final class TimelineTest: XCTestCase {
private var timeline: Timeline!

func testTimeline() {
let expectation = XCTestExpectation(description: "First Plugin")
let testPlugin = testEnrichmentPlugin {
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 {
let testPlugin = TestEnrichmentPlugin {
expectation.fulfill()
return true
}
let testPlugin2 = testEnrichmentPlugin {

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)
}
}

0 comments on commit 296951b

Please sign in to comment.