Skip to content

Commit

Permalink
simplify some of this code to reduce number of tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
ndurell committed Nov 6, 2024
1 parent ad19510 commit 59a5daa
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions Sources/KlaviyoSwift/Klaviyo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import Foundation
import KlaviyoCore
import UIKit

func dispatchOnMainThread(action: KlaviyoAction) {
Task {
await klaviyoSwiftEnvironment.send(action)
}
func dispatchStoreAction(action: KlaviyoAction) async {
_ = await klaviyoSwiftEnvironment.send(action)
}

/// The main interface for the Klaviyo SDK.
Expand Down Expand Up @@ -61,7 +59,7 @@ public final class KlaviyoSDK {
public func initialize(with apiKey: String) -> KlaviyoSDK {
Task {
let appContextInfo = await environment.appContextInfo()
dispatchOnMainThread(action: .initialize(apiKey, appContextInfo))
await dispatchStoreAction(action: .initialize(apiKey, appContextInfo))
}
return self
}
Expand All @@ -74,7 +72,7 @@ public final class KlaviyoSDK {
public func set(profile: Profile) {
Task {
let appContextInfo = await environment.appContextInfo()
dispatchOnMainThread(action: .enqueueProfile(profile, appContextInfo))
await dispatchStoreAction(action: .enqueueProfile(profile, appContextInfo))
}
}

Expand All @@ -85,7 +83,7 @@ public final class KlaviyoSDK {
public func resetProfile() {
Task {
let appContextInfo = await environment.appContextInfo()
dispatchOnMainThread(action: .resetProfile(appContextInfo))
await dispatchStoreAction(action: .resetProfile(appContextInfo))
}
}

Expand All @@ -96,7 +94,7 @@ public final class KlaviyoSDK {
public func set(email: String) -> KlaviyoSDK {
Task {
let appContextInfo = await environment.appContextInfo()
dispatchOnMainThread(action: .setEmail(email, appContextInfo))
await dispatchStoreAction(action: .setEmail(email, appContextInfo))
}
return self
}
Expand All @@ -111,7 +109,7 @@ public final class KlaviyoSDK {
public func set(phoneNumber: String) -> KlaviyoSDK {
Task {
let appContextInfo = await environment.appContextInfo()
dispatchOnMainThread(action: .setPhoneNumber(phoneNumber, appContextInfo))
await dispatchStoreAction(action: .setPhoneNumber(phoneNumber, appContextInfo))
}
return self
}
Expand All @@ -126,7 +124,7 @@ public final class KlaviyoSDK {
public func set(externalId: String) -> KlaviyoSDK {
Task {
let appContextInfo = await environment.appContextInfo()
dispatchOnMainThread(action: .setExternalId(externalId, appContextInfo))
await dispatchStoreAction(action: .setExternalId(externalId, appContextInfo))
}
return self
}
Expand All @@ -138,7 +136,9 @@ public final class KlaviyoSDK {
@discardableResult
public func set(profileAttribute: Profile.ProfileKey, value: Any) -> KlaviyoSDK {
// This seems tricky to implement with Any - might need to restrict to something equatable, encodable....
dispatchOnMainThread(action: .setProfileProperty(profileAttribute, AnyEncodable(value)))
Task {
await dispatchStoreAction(action: .setProfileProperty(profileAttribute, AnyEncodable(value)))
}
return self
}

Expand All @@ -147,7 +147,7 @@ public final class KlaviyoSDK {
public func create(event: Event) {
Task {
let appContextInfo = await environment.appContextInfo()
dispatchOnMainThread(action: .enqueueEvent(event, appContextInfo))
await dispatchStoreAction(action: .enqueueEvent(event, appContextInfo))
}
}

Expand All @@ -165,7 +165,7 @@ public final class KlaviyoSDK {
let enablement = await environment.getNotificationSettings()
let background = klaviyoSwiftEnvironment.getBackgroundSetting()
let appContextInfo = await environment.appContextInfo()
dispatchOnMainThread(action: .setPushToken(pushToken, enablement, background, appContextInfo))
await dispatchStoreAction(action: .setPushToken(pushToken, enablement, background, appContextInfo))
}
}

Expand Down

0 comments on commit 59a5daa

Please sign in to comment.