Skip to content

Commit 05d327d

Browse files
author
Rodrigo Gomez Palacio
committed
IamFetch consistency use-case implementation
Motivation: custom condition to block offset retrieval until condition is met: user & subscription offsets are both available or just the user offset is available. This is because we always make a user update call (update the session count) but not always for subscription.
1 parent eb45946 commit 05d327d

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Modified MIT License
3+
4+
Copyright 2024 OneSignal
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
1. The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
2. All copies of substantial portions of the Software may only be used in connection
17+
with services provided by OneSignal.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
*/
27+
28+
import Foundation
29+
30+
public enum OSIamFetchOffsetKey: Int, OSConsistencyKeyEnum {
31+
case userCreate = 0
32+
case userUpdate = 1
33+
case subscriptionUpdate = 2
34+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Modified MIT License
3+
4+
Copyright 2024 OneSignal
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
1. The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
2. All copies of substantial portions of the Software may only be used in connection
17+
with services provided by OneSignal.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
*/
27+
28+
@objc public class OSIamFetchReadyCondition: NSObject, OSCondition {
29+
// the id used to index the token map (e.g. onesignalId)
30+
private let id: String
31+
private var hasSubscriptionUpdatePending: Bool = false
32+
33+
// Singleton shared instance initialized with default empty id
34+
private static var instance: OSIamFetchReadyCondition?
35+
36+
// Method to get or initialize the shared instance
37+
@objc public static func sharedInstance(withId id: String) -> OSIamFetchReadyCondition {
38+
if instance == nil {
39+
instance = OSIamFetchReadyCondition(id: id)
40+
}
41+
return instance!
42+
}
43+
44+
// Private initializer to prevent external instantiation
45+
private init(id: String) {
46+
self.id = id
47+
}
48+
49+
// Expose the constant to Objective-C
50+
@objc public static let CONDITIONID: String = "OSIamFetchReadyCondition"
51+
52+
public var conditionId: String {
53+
return OSIamFetchReadyCondition.CONDITIONID
54+
}
55+
56+
public func setSubscriptionUpdatePending(value: Bool) {
57+
hasSubscriptionUpdatePending = value
58+
}
59+
60+
public func isMet(indexedTokens: [String: [NSNumber: OSReadYourWriteData]]) -> Bool {
61+
guard let tokenMap = indexedTokens[id] else { return false }
62+
63+
let userCreateTokenSet = tokenMap[NSNumber(value: OSIamFetchOffsetKey.userCreate.rawValue)] != nil
64+
let userUpdateTokenSet = tokenMap[NSNumber(value: OSIamFetchOffsetKey.userUpdate.rawValue)] != nil
65+
let subscriptionTokenSet = tokenMap[NSNumber(value: OSIamFetchOffsetKey.subscriptionUpdate.rawValue)] != nil
66+
67+
if (userCreateTokenSet) {
68+
return true;
69+
}
70+
71+
if (hasSubscriptionUpdatePending) {
72+
return userUpdateTokenSet && subscriptionTokenSet
73+
}
74+
return userUpdateTokenSet
75+
}
76+
77+
public func getNewestToken(indexedTokens: [String: [NSNumber: OSReadYourWriteData]]) -> OSReadYourWriteData? {
78+
// Check if the token map for the given `id` exists
79+
guard let tokenMap = indexedTokens[id] else { return nil }
80+
81+
// Flatten all OSReadYourWriteData objects into an array
82+
let allDataObjects = tokenMap.values.compactMap { $0 }
83+
84+
// Find the object with the max rywToken (if available)
85+
let maxTokenObject = allDataObjects.max {
86+
($0.rywToken ?? "") < ($1.rywToken ?? "")
87+
}
88+
89+
return maxTokenObject
90+
}
91+
92+
}

0 commit comments

Comments
 (0)