Skip to content

Commit 3d19f1f

Browse files
author
Christian Riboldi
committed
Create and use a new internal typealias for TimedEvents which is more accurate to the types stored in the dictionary.
1 parent ffd0d35 commit 3d19f1f

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

Sources/MixpanelInstance.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public protocol MixpanelDelegate: AnyObject {
3737

3838
public typealias Properties = [String: MixpanelType]
3939
typealias InternalProperties = [String: Any]
40+
typealias EventID = String
41+
typealias TimedEvents = [EventID: TimeInterval]
4042
typealias Queue = [InternalProperties]
4143

4244
protocol AppLifecycle {
@@ -210,7 +212,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
210212
var networkQueue: DispatchQueue
211213
var optOutStatus: Bool?
212214
var useUniqueDistinctId: Bool
213-
var timedEvents = InternalProperties()
215+
var timedEvents = TimedEvents()
214216

215217
let readWriteLock: ReadWriteLock
216218
#if os(iOS) && !targetEnvironment(macCatalyst)
@@ -819,7 +821,7 @@ extension MixpanelInstance {
819821

820822
MixpanelPersistence.deleteMPUserDefaultsData(instanceName: self.name)
821823
self.readWriteLock.write {
822-
self.timedEvents = InternalProperties()
824+
self.timedEvents = TimedEvents()
823825
self.anonymousId = self.defaultDeviceId()
824826
self.distinctId = self.addPrefixToDeviceId(deviceId: self.anonymousId)
825827
self.hadPersistedDistinctId = true
@@ -1027,7 +1029,7 @@ extension MixpanelInstance {
10271029
guard let self = self else {
10281030
return
10291031
}
1030-
var shadowTimedEvents = InternalProperties()
1032+
var shadowTimedEvents = TimedEvents()
10311033
var shadowSuperProperties = InternalProperties()
10321034

10331035
self.readWriteLock.read {
@@ -1187,12 +1189,12 @@ extension MixpanelInstance {
11871189
- parameter event: the name of the event to be tracked that was passed to time(event:)
11881190
*/
11891191
public func eventElapsedTime(event: String) -> Double {
1190-
var timedEvents = InternalProperties()
1192+
var timedEvents = TimedEvents()
11911193
self.readWriteLock.read {
11921194
timedEvents = self.timedEvents
11931195
}
11941196

1195-
if let startTime = timedEvents[event] as? TimeInterval {
1197+
if let startTime = timedEvents[event] {
11961198
return Date().timeIntervalSince1970 - startTime
11971199
}
11981200
return 0
@@ -1205,9 +1207,9 @@ extension MixpanelInstance {
12051207
trackingQueue.async { [weak self] in
12061208
guard let self = self else { return }
12071209
self.readWriteLock.write {
1208-
self.timedEvents = InternalProperties()
1210+
self.timedEvents = TimedEvents()
12091211
}
1210-
MixpanelPersistence.saveTimedEvents(timedEvents: InternalProperties(), instanceName: self.name)
1212+
MixpanelPersistence.saveTimedEvents(timedEvents: TimedEvents(), instanceName: self.name)
12111213
}
12121214
}
12131215

@@ -1473,7 +1475,7 @@ extension MixpanelInstance {
14731475
self.distinctId = self.addPrefixToDeviceId(deviceId: self.anonymousId)
14741476
self.hadPersistedDistinctId = true
14751477
self.superProperties = InternalProperties()
1476-
MixpanelPersistence.saveTimedEvents(timedEvents: InternalProperties(), instanceName: self.name)
1478+
MixpanelPersistence.saveTimedEvents(timedEvents: TimedEvents(), instanceName: self.name)
14771479
}
14781480
self.archive()
14791481
self.readWriteLock.write {

Sources/MixpanelPersistence.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class MixpanelPersistence {
129129
return defaults.object(forKey: "\(prefix)\(MixpanelUserDefaultsKeys.optOutStatus)") as? Bool
130130
}
131131

132-
static func saveTimedEvents(timedEvents: InternalProperties, instanceName: String) {
132+
static func saveTimedEvents(timedEvents: TimedEvents, instanceName: String) {
133133
guard let defaults = UserDefaults(suiteName: MixpanelUserDefaultsKeys.suiteName) else {
134134
return
135135
}
@@ -143,19 +143,19 @@ class MixpanelPersistence {
143143
}
144144
}
145145

146-
static func loadTimedEvents(instanceName: String) -> InternalProperties {
146+
static func loadTimedEvents(instanceName: String) -> TimedEvents {
147147
guard let defaults = UserDefaults(suiteName: MixpanelUserDefaultsKeys.suiteName) else {
148-
return InternalProperties()
148+
return TimedEvents()
149149
}
150150
let prefix = "\(MixpanelUserDefaultsKeys.prefix)-\(instanceName)-"
151151
guard let timedEventsData = defaults.data(forKey: "\(prefix)\(MixpanelUserDefaultsKeys.timedEvents)") else {
152-
return InternalProperties()
152+
return TimedEvents()
153153
}
154154
do {
155-
return try NSKeyedUnarchiver.unarchivedObject(ofClasses: archivedClasses, from: timedEventsData) as? InternalProperties ?? InternalProperties()
155+
return try NSKeyedUnarchiver.unarchivedObject(ofClasses: archivedClasses, from: timedEventsData) as? TimedEvents ?? TimedEvents()
156156
} catch {
157157
Logger.warn(message: "Failed to unarchive timed events")
158-
return InternalProperties()
158+
return TimedEvents()
159159
}
160160
}
161161

@@ -296,7 +296,7 @@ class MixpanelPersistence {
296296
peopleQueue: Queue,
297297
groupsQueue: Queue,
298298
superProperties: InternalProperties,
299-
timedEvents: InternalProperties,
299+
timedEvents: TimedEvents,
300300
distinctId: String,
301301
anonymousId: String?,
302302
userId: String?,
@@ -398,7 +398,7 @@ class MixpanelPersistence {
398398
}
399399

400400
private func unarchiveProperties() -> (InternalProperties,
401-
InternalProperties,
401+
TimedEvents,
402402
String,
403403
String?,
404404
String?,
@@ -410,7 +410,7 @@ class MixpanelPersistence {
410410
let superProperties =
411411
properties?["superProperties"] as? InternalProperties ?? InternalProperties()
412412
let timedEvents =
413-
properties?["timedEvents"] as? InternalProperties ?? InternalProperties()
413+
properties?["timedEvents"] as? TimedEvents ?? TimedEvents()
414414
let distinctId =
415415
properties?["distinctId"] as? String ?? ""
416416
let anonymousId =

Sources/Track.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class Track {
3333

3434
func track(event: String?,
3535
properties: Properties? = nil,
36-
timedEvents: InternalProperties,
36+
timedEvents: TimedEvents,
3737
superProperties: InternalProperties,
3838
mixpanelIdentity: MixpanelIdentity,
39-
epochInterval: Double) -> InternalProperties {
39+
epochInterval: Double) -> TimedEvents {
4040
var ev = "mp_event"
4141
if let event = event {
4242
ev = event
@@ -46,14 +46,15 @@ class Track {
4646
if !(mixpanelInstance?.trackAutomaticEventsEnabled ?? false) && ev.hasPrefix("$ae_") {
4747
return timedEvents
4848
}
49+
let eventID = ev
4950
assertPropertyTypes(properties)
5051
#if DEBUG
5152
if !ev.hasPrefix("$") {
5253
UserDefaults.standard.set(true, forKey: InternalKeys.mpDebugTrackedKey)
5354
}
5455
#endif
5556
let epochMilliseconds = round(epochInterval * 1000)
56-
let eventStartTime = timedEvents[ev] as? Double
57+
let eventStartTime = timedEvents[eventID]
5758
var p = InternalProperties()
5859
AutomaticProperties.automaticPropertiesLock.read {
5960
p += AutomaticProperties.properties
@@ -62,7 +63,7 @@ class Track {
6263
p["time"] = epochMilliseconds
6364
var shadowTimedEvents = timedEvents
6465
if let eventStartTime = eventStartTime {
65-
shadowTimedEvents.removeValue(forKey: ev)
66+
shadowTimedEvents.removeValue(forKey: eventID)
6667
p["$duration"] = Double(String(format: "%.3f", epochInterval - eventStartTime))
6768
}
6869
p["distinct_id"] = mixpanelIdentity.distinctID
@@ -139,7 +140,7 @@ class Track {
139140
update(&superProperties)
140141
}
141142

142-
func time(event: String?, timedEvents: InternalProperties, startTime: Double) -> InternalProperties {
143+
func time(event: String?, timedEvents: TimedEvents, startTime: Double) -> TimedEvents {
143144
if mixpanelInstance?.hasOptedOutTracking() ?? false {
144145
return timedEvents
145146
}
@@ -152,13 +153,13 @@ class Track {
152153
return updatedTimedEvents
153154
}
154155

155-
func clearTimedEvents(_ timedEvents: InternalProperties) -> InternalProperties {
156+
func clearTimedEvents(_ timedEvents: TimedEvents) -> TimedEvents {
156157
var updatedTimedEvents = timedEvents
157158
updatedTimedEvents.removeAll()
158159
return updatedTimedEvents
159160
}
160161

161-
func clearTimedEvent(event: String?, timedEvents: InternalProperties) -> InternalProperties {
162+
func clearTimedEvent(event: String?, timedEvents: TimedEvents) -> TimedEvents {
162163
var updatedTimedEvents = timedEvents
163164
guard let event = event, !event.isEmpty else {
164165
Logger.error(message: "mixpanel cannot clear an empty timed event")

0 commit comments

Comments
 (0)