@@ -18,18 +18,26 @@ extension Notification.Name {
18
18
/// independently of the Amplify Storage plugin and this function will indiciate if it will handle the given identifier.
19
19
actor StorageBackgroundEventsRegistry {
20
20
typealias StorageBackgroundEventsContinuation = CheckedContinuation < Bool , Never >
21
- static var identifier : String ?
22
- static var continuation : StorageBackgroundEventsContinuation ?
21
+
22
+ @MainActor
23
+ static let shared = StorageBackgroundEventsRegistry ( )
24
+
25
+ private var identifier : String ?
26
+ private var continuation : StorageBackgroundEventsContinuation ?
23
27
24
28
// override for use with unit tests
25
- static var notificationCenter : NotificationCenter ?
29
+ internal private( set) var notificationCenter : NotificationCenter ?
30
+
31
+ func change( notificationCenter: NotificationCenter ? ) {
32
+ self . notificationCenter = notificationCenter
33
+ }
26
34
27
35
/// Handles background events for URLSession on iOS.
28
36
/// - Parameters:
29
37
/// - identifier: session identifier
30
38
/// - completionHandler: completion handler
31
39
/// - Returns: indicates if the identifier was registered and will be handled
32
- static func handleEventsForBackgroundURLSession( identifier: String ) async -> Bool {
40
+ func handleEventsForBackgroundURLSession( identifier: String ) async -> Bool {
33
41
guard self . identifier == identifier else { return false }
34
42
35
43
return await withCheckedContinuation { ( continuation: CheckedContinuation < Bool , Never > ) in
@@ -41,29 +49,29 @@ actor StorageBackgroundEventsRegistry {
41
49
/// Notifies observes when waiting for continuation to be resumed.
42
50
/// - Parameters:
43
51
/// - identifier: session identifier
44
- private static func notifyWaiting( for identifier: String ) {
52
+ private func notifyWaiting( for identifier: String ) {
45
53
notificationCenter? . post ( name: Notification . Name. StorageBackgroundEventsRegistryWaiting, object: identifier)
46
54
}
47
55
48
56
// The storage plugin will register the session identifier when it is configured.
49
- static func register( identifier: String ) {
57
+ func register( identifier: String ) {
50
58
self . identifier = identifier
51
59
}
52
60
53
61
// When the storage function is deinitialized it will unregister the session identifier.
54
- static func unregister( identifier: String ) {
62
+ func unregister( identifier: String ) {
55
63
if self . identifier == identifier {
56
64
self . identifier = nil
57
65
}
58
66
}
59
67
60
68
// When URLSession is done processing background events it will use this function to get the completion handler.
61
- static func getContinuation( for identifier: String ) -> StorageBackgroundEventsContinuation ? {
69
+ func getContinuation( for identifier: String ) -> StorageBackgroundEventsContinuation ? {
62
70
self . identifier == identifier ? continuation : nil
63
71
}
64
72
65
73
// Once the background event completion handler is used it can be cleared.
66
- static func removeContinuation( for identifier: String ) {
74
+ func removeContinuation( for identifier: String ) {
67
75
if self . identifier == identifier {
68
76
self . continuation = nil
69
77
}
0 commit comments