Skip to content

Commit

Permalink
Uncaching in executors will use the Identity Model Repo
Browse files Browse the repository at this point in the history
* When Requests are uncached in the executors, hook them up to the same identity model instance via the Identity Model Repo
  • Loading branch information
nan-li committed Apr 29, 2024
1 parent f9cf364 commit a0df196
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ class OSIdentityOperationExecutor: OSOperationExecutor {
if var deltaQueue = OneSignalUserDefaults.initShared().getSavedCodeableData(forKey: OS_IDENTITY_EXECUTOR_DELTA_QUEUE_KEY, defaultValue: []) as? [OSDelta] {
// Hook each uncached Delta to the model in the store
for (index, delta) in deltaQueue.enumerated().reversed() {
if let modelInStore = OneSignalUserManagerImpl.sharedInstance.identityModelStore.getModel(modelId: delta.model.modelId) {
// The model exists in the store, set it to be the Delta's model
if let modelInStore = OneSignalUserManagerImpl.sharedInstance.getIdentityModel(delta.model.modelId) {
// The model exists in the repo, set it to be the Delta's model
delta.model = modelInStore
} else {
// The model does not exist, drop this Delta
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSIdentityOperationExecutor.init dropped \(delta)")
deltaQueue.remove(at: index)
}
}
Expand All @@ -59,14 +60,15 @@ class OSIdentityOperationExecutor: OSOperationExecutor {
if var addRequestQueue = OneSignalUserDefaults.initShared().getSavedCodeableData(forKey: OS_IDENTITY_EXECUTOR_ADD_REQUEST_QUEUE_KEY, defaultValue: []) as? [OSRequestAddAliases] {
// Hook each uncached Request to the model in the store
for (index, request) in addRequestQueue.enumerated().reversed() {
if let identityModel = OneSignalUserManagerImpl.sharedInstance.identityModelStore.getModel(modelId: request.identityModel.modelId) {
// 1. The model exists in the store, so set it to be the Request's models
if let identityModel = OneSignalUserManagerImpl.sharedInstance.getIdentityModel(request.identityModel.modelId) {
// 1. The model exists in the repo, so set it to be the Request's models
request.identityModel = identityModel
} else if let identityModel = OSUserExecutor.identityModels[request.identityModel.modelId] {
// 2. The model exists in the user executor
request.identityModel = identityModel
} else if !request.prepareForExecution() {
// 3. The models do not exist AND this request cannot be sent, drop this Request
} else if request.prepareForExecution() {
// 2. The request can be sent, add the model to the repo
OneSignalUserManagerImpl.sharedInstance.addIdentityModelToRepo(request.identityModel)
} else {
// 3. The model do not exist AND this request cannot be sent, drop this Request
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSIdentityOperationExecutor.init dropped \(request)")
addRequestQueue.remove(at: index)
}
}
Expand All @@ -79,14 +81,15 @@ class OSIdentityOperationExecutor: OSOperationExecutor {
if var removeRequestQueue = OneSignalUserDefaults.initShared().getSavedCodeableData(forKey: OS_IDENTITY_EXECUTOR_REMOVE_REQUEST_QUEUE_KEY, defaultValue: []) as? [OSRequestRemoveAlias] {
// Hook each uncached Request to the model in the store
for (index, request) in removeRequestQueue.enumerated().reversed() {
if let identityModel = OneSignalUserManagerImpl.sharedInstance.identityModelStore.getModel(modelId: request.identityModel.modelId) {
// 1. The model exists in the store, so set it to be the Request's model
if let identityModel = OneSignalUserManagerImpl.sharedInstance.getIdentityModel(request.identityModel.modelId) {
// 1. The model exists in the repo, so set it to be the Request's model
request.identityModel = identityModel
} else if let identityModel = OSUserExecutor.identityModels[request.identityModel.modelId] {
// 2. The model exists in the user executor
request.identityModel = identityModel
} else if !request.prepareForExecution() {
} else if request.prepareForExecution() {
// 2. The request can be sent, add the model to the repo
OneSignalUserManagerImpl.sharedInstance.addIdentityModelToRepo(request.identityModel)
} else {
// 3. The model does not exist AND this request cannot be sent, drop this Request
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSIdentityOperationExecutor.init dropped \(request)")
removeRequestQueue.remove(at: index)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ class OSPropertyOperationExecutor: OSOperationExecutor {
// Read unfinished deltas from cache, if any...
// Note that we should only have deltas for the current user as old ones are flushed..
if var deltaQueue = OneSignalUserDefaults.initShared().getSavedCodeableData(forKey: OS_PROPERTIES_EXECUTOR_DELTA_QUEUE_KEY, defaultValue: []) as? [OSDelta] {
// Hook each uncached Delta to the model in the store
for (index, delta) in deltaQueue.enumerated().reversed() {
if let modelInStore = OneSignalUserManagerImpl.sharedInstance.propertiesModelStore.getModel(modelId: delta.model.modelId) {
// 1. The model exists in the properties model store, set it to be the Delta's model
delta.model = modelInStore
} else {
// 2. The model does not exist, drop this Delta
if OneSignalUserManagerImpl.sharedInstance.getIdentityModel(delta.identityModelId) == nil {
// The identity model does not exist, drop this Delta
OneSignalLog.onesignalLog(.LL_WARN, message: "OSPropertyOperationExecutor.init dropped: \(delta)")
deltaQueue.remove(at: index)
}
Expand All @@ -61,13 +57,13 @@ class OSPropertyOperationExecutor: OSOperationExecutor {
if var updateRequestQueue = OneSignalUserDefaults.initShared().getSavedCodeableData(forKey: OS_PROPERTIES_EXECUTOR_UPDATE_REQUEST_QUEUE_KEY, defaultValue: []) as? [OSRequestUpdateProperties] {
// Hook each uncached Request to the model in the store
for (index, request) in updateRequestQueue.enumerated().reversed() {
if let identityModel = OneSignalUserManagerImpl.sharedInstance.identityModelStore.getModel(modelId: request.identityModel.modelId) {
// 1. The identity model exist in the store, set it to be the Request's models
request.identityModel = identityModel
} else if let identityModel = OSUserExecutor.identityModels[request.identityModel.modelId] {
// 2. The model exists in the user executor
if let identityModel = OneSignalUserManagerImpl.sharedInstance.getIdentityModel(request.identityModel.modelId) {
// 1. The identity model exist in the repo, set it to be the Request's model
request.identityModel = identityModel
} else if !request.prepareForExecution() {
} else if request.prepareForExecution() {
// 2. The request can be sent, add the model to the repo
OneSignalUserManagerImpl.sharedInstance.addIdentityModelToRepo(request.identityModel)
} else {
// 3. The identitymodel do not exist AND this request cannot be sent, drop this Request
OneSignalLog.onesignalLog(.LL_WARN, message: "OSPropertyOperationExecutor.init dropped: \(request)")
updateRequestQueue.remove(at: index)
Expand Down Expand Up @@ -99,11 +95,18 @@ class OSPropertyOperationExecutor: OSOperationExecutor {
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSPropertyOperationExecutor processDeltaQueue with queue: \(self.deltaQueue)")
}
for delta in self.deltaQueue {
guard let identityModel = OneSignalUserManagerImpl.sharedInstance.getIdentityModel(delta.identityModelId)
else {
// drop this delta
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSPropertyOperationExecutor.processDeltaQueue dropped: \(delta)")
continue
}

let request = OSRequestUpdateProperties(
properties: [delta.property: delta.value],
deltas: nil,
refreshDeviceMetadata: false, // Sort this out.
identityModel: OneSignalUserManagerImpl.sharedInstance.user.identityModel // TODO: Make sure this is ok
identityModel: identityModel
)
self.updateRequestQueue.append(request)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class OSSubscriptionOperationExecutor: OSOperationExecutor {
delta.model = modelInStore
} else {
// The model does not exist, drop this Delta
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSSubscriptionOperationExecutor.init dropped \(delta)")
deltaQueue.remove(at: index)
}
}
Expand Down Expand Up @@ -75,14 +76,15 @@ class OSSubscriptionOperationExecutor: OSOperationExecutor {
subscriptionModels[request.subscriptionModel.modelId] = request.subscriptionModel
}
// 2. Hook up the identity model
if let identityModel = OneSignalUserManagerImpl.sharedInstance.identityModelStore.getModel(modelId: request.identityModel.modelId) {
// a. The model exist in the store
if let identityModel = OneSignalUserManagerImpl.sharedInstance.getIdentityModel(request.identityModel.modelId) {
// a. The model exist in the repo
request.identityModel = identityModel
} else if let identityModel = OSUserExecutor.identityModels[request.identityModel.modelId] {
// b. The model exist in the user executor
request.identityModel = identityModel
} else if !request.prepareForExecution() {
// The model do not exist AND this request cannot be sent, drop this Request
} else if request.prepareForExecution() {
// b. The request can be sent, add the model to the repo
OneSignalUserManagerImpl.sharedInstance.addIdentityModelToRepo(request.identityModel)
} else {
// c. The model do not exist AND this request cannot be sent, drop this Request
OneSignalLog.onesignalLog(.LL_WARN, message: "OSSubscriptionOperationExecutor.init dropped: \(request)")
continue
}
requestQueue.append(request)
Expand All @@ -104,6 +106,7 @@ class OSSubscriptionOperationExecutor: OSOperationExecutor {
request.subscriptionModel = subscriptionModel
} else if !request.prepareForExecution() {
// 3. The model does not exist AND this request cannot be sent, drop this Request
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSSubscriptionOperationExecutor.init dropped \(request)")
removeRequestQueue.remove(at: index)
}
}
Expand All @@ -124,6 +127,7 @@ class OSSubscriptionOperationExecutor: OSOperationExecutor {
request.subscriptionModel = subscriptionModel
} else if !request.prepareForExecution() {
// 3. The models do not exist AND this request cannot be sent, drop this Request
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSSubscriptionOperationExecutor.init dropped \(request)")
updateRequestQueue.remove(at: index)
}
}
Expand Down Expand Up @@ -161,29 +165,34 @@ class OSSubscriptionOperationExecutor: OSOperationExecutor {
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSSubscriptionOperationExecutor processDeltaQueue with queue: \(deltaQueue)")
}
for delta in deltaQueue {
guard let model = delta.model as? OSSubscriptionModel else {
// Log error
guard let subModel = delta.model as? OSSubscriptionModel
else {
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSSubscriptionOperationExecutor.processDeltaQueue dropped \(delta)")
continue
}

switch delta.name {
case OS_ADD_SUBSCRIPTION_DELTA:
let request = OSRequestCreateSubscription(
subscriptionModel: model,
identityModel: OneSignalUserManagerImpl.sharedInstance.user.identityModel // TODO: Make sure this is ok
)
addRequestQueue.append(request)

// Only create the request if the identity model exists
if let identityModel = OneSignalUserManagerImpl.sharedInstance.getIdentityModel(delta.identityModelId) {
let request = OSRequestCreateSubscription(
subscriptionModel: subModel,
identityModel: identityModel
)
addRequestQueue.append(request)
} else {
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSSubscriptionOperationExecutor.processDeltaQueue dropped \(delta)")
}
case OS_REMOVE_SUBSCRIPTION_DELTA:
let request = OSRequestDeleteSubscription(
subscriptionModel: model
subscriptionModel: subModel
)
removeRequestQueue.append(request)

case OS_UPDATE_SUBSCRIPTION_DELTA:
let request = OSRequestUpdateSubscription(
subscriptionObject: [delta.property: delta.value],
subscriptionModel: model
subscriptionModel: subModel
)
updateRequestQueue.append(request)

Expand Down
Loading

0 comments on commit a0df196

Please sign in to comment.