Skip to content

Commit

Permalink
Flush delta queue on start
Browse files Browse the repository at this point in the history
Motivation: we need to flush the delta queue on start in order to immediately trigger updates that would return a RYW token so we can unblock IAM fetch as soon as possible.
  • Loading branch information
rgomezp committed Sep 30, 2024
1 parent eeb702c commit 576cfcd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,21 @@ public class OSOperationRepo: NSObject {
// TODO: We can make this method internal once there is no manual adding of a Delta except through stores.
This can happen when session data and purchase data use the model / store / listener infrastructure.
*/
public func enqueueDelta(_ delta: OSDelta) {
public func enqueueDelta(_ delta: OSDelta, flush: Bool = false) {
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
return
}
start()
self.dispatchQueue.async {
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSOperationRepo enqueueDelta: \(delta)")
self.deltaQueue.append(delta)

// Persist the deltas (including new delta) to storage
OneSignalUserDefaults.initShared().saveCodeableData(forKey: OS_OPERATION_REPO_DELTA_QUEUE_KEY, withValue: self.deltaQueue)

if flush {
self.flushDeltaQueue()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ extension OneSignalUserManagerImpl {

userExecutor!.executePendingRequests()
OSOperationRepo.sharedInstance.paused = false
updatePropertiesDeltas(property: .session_count, value: 1)
updatePropertiesDeltas(property: .session_count, value: 1, flush: true)

// Fetch the user's data if there is a onesignal_id
if let onesignalId = onesignalId {
Expand All @@ -567,7 +567,7 @@ extension OneSignalUserManagerImpl {
/// It enqueues an OSDelta to the Operation Repo.
///
/// - Parameter property:Expected inputs are `.session_time"`, `.session_count"`, and `.purchases"`.
func updatePropertiesDeltas(property: OSPropertiesSupportedProperty, value: Any) {
func updatePropertiesDeltas(property: OSPropertiesSupportedProperty, value: Any, flush: Bool = false) {
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "updatePropertiesDeltas") else {
return
}
Expand All @@ -583,7 +583,7 @@ extension OneSignalUserManagerImpl {
property: property.rawValue,
value: value
)
OSOperationRepo.sharedInstance.enqueueDelta(delta)
OSOperationRepo.sharedInstance.enqueueDelta(delta, flush: flush)
}

/// Time processors forward the session time to this method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ final class OneSignalUserTests: XCTestCase {

OneSignalUserManagerImpl.sharedInstance.sendSessionTime(100)

// This adds a `session_count` property with value of 1
// It also sets `refresh_device_metadata` to `true`
OneSignalUserManagerImpl.sharedInstance.startNewSession()

OneSignalUserManagerImpl.sharedInstance.setLanguage("lang_1")

OneSignalUserManagerImpl.sharedInstance.addTag(key: "tag_1", value: "value_1")
Expand All @@ -108,7 +104,6 @@ final class OneSignalUserTests: XCTestCase {

OneSignalUserManagerImpl.sharedInstance.addTags(["a": "a", "b": "b", "c": "c"])

OneSignalUserManagerImpl.sharedInstance.startNewSession()

let purchases = [
["sku": "sku1", "amount": "1.25", "iso": "USD"],
Expand All @@ -118,6 +113,10 @@ final class OneSignalUserTests: XCTestCase {
OneSignalUserManagerImpl.sharedInstance.sendPurchases(purchases as [[String: AnyObject]])

OneSignalUserManagerImpl.sharedInstance.setLocation(latitude: 111.111, longitude: 222.222)

// This adds a `session_count` property with value of 1
// It also sets `refresh_device_metadata` to `true`
OneSignalUserManagerImpl.sharedInstance.startNewSession()

/* Then */

Expand Down

0 comments on commit 576cfcd

Please sign in to comment.