-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathOSOperationRepo.swift
More file actions
180 lines (150 loc) · 7.19 KB
/
Copy pathOSOperationRepo.swift
File metadata and controls
180 lines (150 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Modified MIT License
Copyright 2022 OneSignal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import Foundation
import OneSignalCore
/**
The OSOperationRepo is a static singleton.
OSDeltas are enqueued when model store observers observe changes to their models, and sorted to their appropriate executors.
*/
public class OSOperationRepo: NSObject {
public static let sharedInstance = OSOperationRepo()
private var hasCalledStart = false
// The Operation Repo dispatch queue, serial. This synchronizes access to `deltaQueue` and flushing behavior.
private let dispatchQueue = DispatchQueue(label: "OneSignal.OSOperationRepo", target: .global())
// Maps delta names to the interfaces for the operation executors
var deltasToExecutorMap: [String: OSOperationExecutor] = [:]
var executors: [OSOperationExecutor] = []
var deltaQueue: [OSDelta] = [] // non-private for unit test access
// TODO: This could come from a config, plist, method, remote params
var pollIntervalMilliseconds = Int(POLL_INTERVAL_MS)
public var paused = false
/**
Initilize this Operation Repo. Read from the cache. Executors may not be available by this time.
If everything starts up on initialize(), order can matter, ideally not but it can.
Likely call init on this from oneSignal but exeuctors can come from diff modules.
*/
public func start() {
guard !OneSignalConfig.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
return
}
guard !hasCalledStart else {
return
}
hasCalledStart = true
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSOperationRepo calling start()")
// register as user observer
NotificationCenter.default.addObserver(self,
selector: #selector(self.addFlushDeltaQueueToDispatchQueue),
name: Notification.Name(OS_ON_USER_WILL_CHANGE),
object: nil)
// Read the Deltas from cache, if any...
if let deltaQueue = OneSignalUserDefaults.initShared().getSavedCodeableData(forKey: OS_OPERATION_REPO_DELTA_QUEUE_KEY, defaultValue: []) as? [OSDelta] {
self.deltaQueue = deltaQueue
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSOperationRepo.start() with deltaQueue: \(deltaQueue)")
} else {
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSOperationRepo.start() is unable to uncache the OSDelta queue.")
}
pollFlushQueue()
}
private func pollFlushQueue() {
self.dispatchQueue.asyncAfter(deadline: .now() + .milliseconds(pollIntervalMilliseconds)) { [weak self] in
self?.flushDeltaQueue()
self?.pollFlushQueue()
}
}
/**
Add and start an executor.
*/
public func addExecutor(_ executor: OSOperationExecutor) {
guard !OneSignalConfig.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
return
}
start()
executors.append(executor)
for delta in executor.supportedDeltas {
deltasToExecutorMap[delta] = executor
}
}
/**
Enqueueing is driven by model changes and called manually by the User Manager to
add session time, session count and purchase data.
// 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, flush: Bool = false) {
guard !OneSignalConfig.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()
}
}
}
@objc public func addFlushDeltaQueueToDispatchQueue(inBackground: Bool = false) {
self.dispatchQueue.async {
self.flushDeltaQueue(inBackground: inBackground)
}
}
private func flushDeltaQueue(inBackground: Bool = false) {
guard !paused else {
OneSignalLog.onesignalLog(.LL_DEBUG, message: "OSOperationRepo not flushing queue due to being paused")
return
}
guard !OneSignalConfig.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
return
}
if inBackground {
OSBackgroundTaskManager.beginBackgroundTask(OPERATION_REPO_BACKGROUND_TASK)
}
self.start()
if !self.deltaQueue.isEmpty {
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSOperationRepo flushDeltaQueue in background: \(inBackground) with queue: \(self.deltaQueue)")
}
var unmatched: [OSDelta] = []
for delta in self.deltaQueue {
if let executor = self.deltasToExecutorMap[delta.name] {
executor.enqueueDelta(delta)
} else {
// Keep if no executor matches yet (module may not have started).
unmatched.append(delta)
}
}
self.deltaQueue = unmatched
// Persist the deltas (including removed deltas) to storage after they are divvy'd up to executors.
OneSignalUserDefaults.initShared().saveCodeableData(forKey: OS_OPERATION_REPO_DELTA_QUEUE_KEY, withValue: self.deltaQueue)
for executor in self.executors {
executor.cacheDeltaQueue()
}
for executor in self.executors {
executor.processDeltaQueue(inBackground: inBackground)
}
if inBackground {
OSBackgroundTaskManager.endBackgroundTask(OPERATION_REPO_BACKGROUND_TASK)
}
}
}