@@ -13,8 +13,8 @@ import enum AwsCommonRuntimeKit.CommonRunTimeError
13
13
import Foundation
14
14
15
15
/// AnalyticsEventRecording saves and submits pinpoint events
16
- protocol AnalyticsEventRecording {
17
- var pinpointClient : PinpointClientProtocol { get }
16
+ protocol AnalyticsEventRecording : Actor {
17
+ nonisolated var pinpointClient : PinpointClientProtocol { get }
18
18
19
19
/// Saves a pinpoint event to storage
20
20
/// - Parameter event: A PinpointEvent
@@ -35,12 +35,13 @@ protocol AnalyticsEventRecording {
35
35
}
36
36
37
37
/// An AnalyticsEventRecording implementation that stores and submits pinpoint events
38
- class EventRecorder : AnalyticsEventRecording {
39
- let appId : String
40
- let storage : AnalyticsEventStorage
41
- let pinpointClient : PinpointClientProtocol
42
- let endpointClient : EndpointClientBehaviour
38
+ actor EventRecorder : AnalyticsEventRecording {
39
+ private let appId : String
40
+ private let storage : AnalyticsEventStorage
43
41
private var submittedEvents : [ PinpointEvent ] = [ ]
42
+ private var submissionTask : Task < [ PinpointEvent ] , Error > ?
43
+ nonisolated let endpointClient : EndpointClientBehaviour
44
+ nonisolated let pinpointClient : PinpointClientProtocol
44
45
45
46
/// Initializer for Event Recorder
46
47
/// - Parameters:
@@ -66,31 +67,37 @@ class EventRecorder: AnalyticsEventRecording {
66
67
func save( _ event: PinpointEvent ) throws {
67
68
log. verbose ( " saveEvent: \( event) " )
68
69
try storage. saveEvent ( event)
69
- try self . storage. checkDiskSize ( limit: Constants . pinpointClientByteLimitDefault)
70
+ try storage. checkDiskSize ( limit: Constants . pinpointClientByteLimitDefault)
70
71
}
71
72
72
73
func updateAttributesOfEvents( ofType eventType: String ,
73
74
withSessionId sessionId: PinpointSession . SessionId ,
74
75
setAttributes attributes: [ String : String ] ) throws {
75
- try self . storage. updateEvents ( ofType: eventType,
76
+ try storage. updateEvents ( ofType: eventType,
76
77
withSessionId: sessionId,
77
78
setAttributes: attributes)
78
79
}
79
80
80
- /// Submit all locally stored events in batches
81
- /// If event submission fails, the event retry count is increment otherwise event is marked dirty and available for deletion in the local storage if retry count exceeds 3
82
- /// If event submission succeeds , the event is removed from local storage
81
+ /// Submit all locally stored events in batches. If a previous submission is in progress, it waits until it's completed before proceeding.
82
+ /// When the submission for an event is accepted, the event is removed from local storage
83
+ /// When the submission for an event is rejected , the event retry count is incremented in the local storage. Events that exceed the maximum retry count (3) are purged.
83
84
/// - Returns: A collection of events submitted to Pinpoint
84
85
func submitAllEvents( ) async throws -> [ PinpointEvent ] {
85
- submittedEvents = [ ]
86
- let eventsBatch = try getBatchRecords ( )
87
- if eventsBatch. count > 0 {
88
- let endpointProfile = await endpointClient. currentEndpointProfile ( )
89
- try await processBatch ( eventsBatch, endpointProfile: endpointProfile)
90
- } else {
91
- log. verbose ( " No events to submit " )
86
+ let task = Task { [ submissionTask] in
87
+ // Wait for the previous submission to complete, regardless of its result
88
+ _ = try ? await submissionTask? . value
89
+ submittedEvents = [ ]
90
+ let eventsBatch = try getBatchRecords ( )
91
+ if eventsBatch. count > 0 {
92
+ let endpointProfile = await endpointClient. currentEndpointProfile ( )
93
+ try await processBatch ( eventsBatch, endpointProfile: endpointProfile)
94
+ } else {
95
+ log. verbose ( " No events to submit " )
96
+ }
97
+ return submittedEvents
92
98
}
93
- return submittedEvents
99
+ submissionTask = task
100
+ return try await task. value
94
101
}
95
102
96
103
private func getBatchRecords( ) throws -> [ PinpointEvent ] {
@@ -343,7 +350,7 @@ extension EventRecorder: DefaultLogger {
343
350
public static var log : Logger {
344
351
Amplify . Logging. logger ( forCategory: CategoryType . analytics. displayName, forNamespace: String ( describing: self ) )
345
352
}
346
- public var log : Logger {
353
+ nonisolated public var log : Logger {
347
354
Self . log
348
355
}
349
356
}
0 commit comments