Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion Examples/destination_plugins/AdjustDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class AdjustDestination: NSObject, DestinationPlugin, RemoteNotifications

internal var settings: AdjustSettings? = nil

@Atomic var started = false
@Atomic public var started = false

required public init(name: String) {
self.name = name
Expand Down
4 changes: 4 additions & 0 deletions Examples/destination_plugins/MixpanelDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class MixpanelDestination: DestinationPlugin {
private var mixpanel: MixpanelInstance? = nil
private var settings: [String: Any]? = nil

@Atomic public var started = false

required init(name: String) {
self.name = name
type = .destination
Expand Down Expand Up @@ -74,6 +76,8 @@ class MixpanelDestination: DestinationPlugin {
mixpanel = nil
analytics?.log(message: "Could not load Mixpanel settings")
}

started = true
}

func identify(event: IdentifyEvent) -> IdentifyEvent? {
Expand Down
1 change: 1 addition & 0 deletions Examples/other_plugins/ConsentTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class ConsentTracking: Plugin {
for instance in Self.instances {
instance.replayEvents()
}
clearQueuedEvents()
}

func replayEvents() {
Expand Down
4 changes: 4 additions & 0 deletions Segment.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
46022764261E64A800A9E913 /* iOSLifecycleEvents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46022763261E64A800A9E913 /* iOSLifecycleEvents.swift */; };
4602276C261E7BF900A9E913 /* iOSDelegation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4602276B261E7BF900A9E913 /* iOSDelegation.swift */; };
46022771261F7A4800A9E913 /* Atomic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46022770261F7A4800A9E913 /* Atomic.swift */; };
46031D63266A9221009BA540 /* EventQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46031D62266A9221009BA540 /* EventQueue.swift */; };
462107F22603DA4D00EBC4A8 /* Sovran in Frameworks */ = {isa = PBXBuildFile; productRef = 462107F12603DA4D00EBC4A8 /* Sovran */; };
4621080C2605332D00EBC4A8 /* KeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4621080B2605332D00EBC4A8 /* KeyPath.swift */; };
46210811260538BE00EBC4A8 /* KeyPath_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46210810260538BE00EBC4A8 /* KeyPath_Tests.swift */; };
Expand Down Expand Up @@ -103,6 +104,7 @@
46022763261E64A800A9E913 /* iOSLifecycleEvents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSLifecycleEvents.swift; sourceTree = "<group>"; };
4602276B261E7BF900A9E913 /* iOSDelegation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSDelegation.swift; sourceTree = "<group>"; };
46022770261F7A4800A9E913 /* Atomic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Atomic.swift; sourceTree = "<group>"; };
46031D62266A9221009BA540 /* EventQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventQueue.swift; sourceTree = "<group>"; };
4621080B2605332D00EBC4A8 /* KeyPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyPath.swift; sourceTree = "<group>"; };
46210810260538BE00EBC4A8 /* KeyPath_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyPath_Tests.swift; sourceTree = "<group>"; };
46210835260BBEE400EBC4A8 /* DeviceToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceToken.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -211,6 +213,7 @@
46A018C125E5857D00F9CCD8 /* Context.swift */,
9692726725A583A6009B5298 /* SegmentDestination.swift */,
46210835260BBEE400EBC4A8 /* DeviceToken.swift */,
46031D62266A9221009BA540 /* EventQueue.swift */,
);
path = Plugins;
sourceTree = "<group>";
Expand Down Expand Up @@ -462,6 +465,7 @@
46A018D425E6C9C200F9CCD8 /* LinuxUtils.swift in Sources */,
96C33A9C25880A5E00F3D538 /* Logger.swift in Sources */,
46FE4C9725A3F35E003A7362 /* macOSLifecycleMonitor.swift in Sources */,
46031D63266A9221009BA540 /* EventQueue.swift in Sources */,
9620862C2575C0C800314F8D /* Events.swift in Sources */,
A3AEE1882581A8F1002386EB /* Deprecations.swift in Sources */,
966945D7259BDCDD00271339 /* HTTPClient.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Sources/Segment/Plugins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public protocol EventPlugin: Plugin {

public protocol DestinationPlugin: EventPlugin {
var timeline: Timeline { get }
var started: Bool { get }
func add(plugin: Plugin) -> String
func apply(closure: (Plugin) -> Void)
func remove(pluginName: String)
Expand Down
64 changes: 64 additions & 0 deletions Sources/Segment/Plugins/EventQueue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// EventQueue.swift
// Segment
//
// Created by Brandon Sneed on 6/4/21.
//

import Foundation

class EventQueue: Plugin {
static var specificName = "Segment_EventQueue"
static var queueSize = 100

let type: PluginType = .before
let name: String = specificName
var analytics: Analytics? = nil

var queuedEvents = [RawEvent]()

required init(name: String) {
// ignore name; hardcoded above.
}

func execute<T: RawEvent>(event: T?) -> T? {
// if we've been given consent, let the event pass through.
if destinationsStarted {
replayEvents()
return event
} else if let e = event {
// destinations haven't all started, so queue it up.
queuedEvents.append(e)
// don't let the queue get too large.
if queuedEvents.count > Self.queueSize {
queuedEvents.removeFirst()
}
}

// returning nil will stop processing the event in the timeline.
return nil
}
}

extension EventQueue {
var destinationsStarted: Bool {
var result = false
if let destinations = analytics?.timeline.plugins[.destination]?.plugins as? [DestinationPlugin] {
result = true
destinations.forEach { destination in
if result == true && destination.started == false {
result = false
}
}
}
return result
}

func replayEvents() {
// replay the queued events to the instance of Analytics we're working with.
for event in queuedEvents {
analytics?.process(event: event)
}
queuedEvents.removeAll()
}
}
3 changes: 3 additions & 0 deletions Sources/Segment/Plugins/SegmentDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class SegmentDestination: DestinationPlugin {
}
}

@Atomic public var started: Bool = false

private var httpClient: HTTPClient?
private var pendingURLs = [URL]()
private var uploadInProgress = false
Expand Down Expand Up @@ -55,6 +57,7 @@ public class SegmentDestination: DestinationPlugin {
if (apiHost != nil && apiKey != nil), let analytics = self.analytics {
httpClient = HTTPClient(analytics: analytics, apiKey: apiKey, apiHost: apiHost)
}
started = true
}

// MARK: - Event Handling Methods
Expand Down
2 changes: 2 additions & 0 deletions Tests/Segment-Tests/Support/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class MyDestination: DestinationPlugin {
let type: PluginType
let name: String
var analytics: Analytics?
@Atomic var started: Bool = false

required init(name: String) {
self.name = name
Expand All @@ -86,6 +87,7 @@ class MyDestination: DestinationPlugin {

func update(settings: Settings) {
//
started = true
}

}
Expand Down