@@ -32,8 +32,7 @@ import OneSignalCore
32
32
The OSOperationRepo is a static singleton.
33
33
OSDeltas are enqueued when model store observers observe changes to their models, and sorted to their appropriate executors.
34
34
*/
35
- public class OSOperationRepo : NSObject {
36
- public static let sharedInstance = OSOperationRepo ( )
35
+ public class OSOperationRepo {
37
36
private var hasCalledStart = false
38
37
39
38
// The Operation Repo dispatch queue, serial. This synchronizes access to `deltaQueue` and flushing behavior.
@@ -47,16 +46,37 @@ public class OSOperationRepo: NSObject {
47
46
// TODO: This could come from a config, plist, method, remote params
48
47
var pollIntervalMilliseconds = Int ( POLL_INTERVAL_MS)
49
48
public var paused = false
49
+ let jwtConfig : OSUserJwtConfig
50
50
51
51
/**
52
- Initilize this Operation Repo. Read from the cache. Executors may not be available by this time.
53
- If everything starts up on initialize(), order can matter, ideally not but it can.
54
- Likely call init on this from oneSignal but exeuctors can come from diff modules.
52
+ Sets the jwt config and uncaches
53
+ */
54
+ public init ( jwtConfig: OSUserJwtConfig ) {
55
+ self . jwtConfig = jwtConfig
56
+ self . jwtConfig. subscribe ( self , key: OS_OPERATION_REPO)
57
+ print ( " ❌ OSOperationRepo init( \( String ( describing: jwtConfig. isRequired) ) ) called " )
58
+
59
+ // Read the Deltas from cache, if any...
60
+ guard let deltaQueue = OneSignalUserDefaults . initShared ( ) . getSavedCodeableData ( forKey: OS_OPERATION_REPO_DELTA_QUEUE_KEY, defaultValue: [ ] ) as? [ OSDelta ] else {
61
+ OneSignalLog . onesignalLog ( . LL_ERROR, message: " OSOperationRepo is unable to uncache the OSDelta queue. " )
62
+ return
63
+ }
64
+ self . deltaQueue = deltaQueue
65
+ }
66
+
67
+ /**
68
+ Start this Operation Repo.
55
69
*/
56
70
public func start( ) {
57
71
guard !OneSignalConfigManager. shouldAwaitAppIdAndLogMissingPrivacyConsent ( forMethod: nil ) else {
58
72
return
59
73
}
74
+
75
+ guard jwtConfig. isRequired != nil else {
76
+ print ( " ❌ OSOperationRepo.start() returning early due to unknown Identity Verification status. " )
77
+ return
78
+ }
79
+
60
80
guard !hasCalledStart else {
61
81
return
62
82
}
@@ -68,13 +88,6 @@ public class OSOperationRepo: NSObject {
68
88
selector: #selector( self . addFlushDeltaQueueToDispatchQueue) ,
69
89
name: Notification . Name ( OS_ON_USER_WILL_CHANGE) ,
70
90
object: nil )
71
- // Read the Deltas from cache, if any...
72
- if let deltaQueue = OneSignalUserDefaults . initShared ( ) . getSavedCodeableData ( forKey: OS_OPERATION_REPO_DELTA_QUEUE_KEY, defaultValue: [ ] ) as? [ OSDelta ] {
73
- self . deltaQueue = deltaQueue
74
- OneSignalLog . onesignalLog ( . LL_VERBOSE, message: " OSOperationRepo.start() with deltaQueue: \( deltaQueue) " )
75
- } else {
76
- OneSignalLog . onesignalLog ( . LL_ERROR, message: " OSOperationRepo.start() is unable to uncache the OSDelta queue. " )
77
- }
78
91
79
92
pollFlushQueue ( )
80
93
}
@@ -87,13 +100,12 @@ public class OSOperationRepo: NSObject {
87
100
}
88
101
89
102
/**
90
- Add and start an executor.
103
+ Add an executor.
91
104
*/
92
105
public func addExecutor( _ executor: OSOperationExecutor ) {
93
106
guard !OneSignalConfigManager. shouldAwaitAppIdAndLogMissingPrivacyConsent ( forMethod: nil ) else {
94
107
return
95
108
}
96
- start ( )
97
109
executors. append ( executor)
98
110
for delta in executor. supportedDeltas {
99
111
deltasToExecutorMap [ delta] = executor
@@ -111,7 +123,6 @@ public class OSOperationRepo: NSObject {
111
123
guard !OneSignalConfigManager. shouldAwaitAppIdAndLogMissingPrivacyConsent ( forMethod: nil ) else {
112
124
return
113
125
}
114
- start ( )
115
126
self . dispatchQueue. async {
116
127
OneSignalLog . onesignalLog ( . LL_VERBOSE, message: " OSOperationRepo enqueueDelta: \( delta) " )
117
128
self . deltaQueue. append ( delta)
@@ -140,8 +151,6 @@ public class OSOperationRepo: NSObject {
140
151
OSBackgroundTaskManager . beginBackgroundTask ( OPERATION_REPO_BACKGROUND_TASK)
141
152
}
142
153
143
- self . start ( )
144
-
145
154
if !self . deltaQueue. isEmpty {
146
155
OneSignalLog . onesignalLog ( . LL_VERBOSE, message: " OSOperationRepo flushDeltaQueue in background: \( inBackground) with queue: \( self . deltaQueue) " )
147
156
}
@@ -174,6 +183,30 @@ public class OSOperationRepo: NSObject {
174
183
}
175
184
}
176
185
186
+ extension OSOperationRepo : OSUserJwtConfigListener {
187
+ public func onRequiresUserAuthChanged( from: Bool ? , to: Bool ? ) {
188
+ print ( " ❌ OSOperationRepo onRequiresUserAuthChanged from \( String ( describing: from) ) to \( String ( describing: to) ) " )
189
+ // If auth changed from false or unknown to true, process deltas
190
+ if to == true {
191
+ removeInvalidDeltas ( )
192
+ }
193
+ start ( )
194
+ }
195
+
196
+ public func onJwtUpdated( externalId: String , to: String ? ) {
197
+ print ( " ❌ OSOperationRepo onJwtUpdated for \( externalId) to \( String ( describing: to) ) " )
198
+ }
199
+
200
+ /**
201
+ TODO: The operation repo cannot easily remove invalid Deltas that do not have an External ID.
202
+ Deltas have an Identity Model ID only and would need to access the User module to find the corresponding Identity Model.
203
+ Executors will handle this.
204
+ */
205
+ func removeInvalidDeltas( ) {
206
+ print ( " ❌ OSOperationRepo removeInvalidDeltas TODO! " )
207
+ }
208
+ }
209
+
177
210
extension OSOperationRepo : OSLoggable {
178
211
public func logSelf( ) {
179
212
print ( " 💛 Operation Repo: deltaQueue: \( self . deltaQueue ) " )
0 commit comments