Skip to content

Commit 108afe9

Browse files
5dlawmicha
andauthored
feat(datastore): refactor datastore category to use APICategoryGraphQLBehavior (#3666)
* WIP * DataStore compiles without SDK dependency * refactor(datastore-v2): use api plugin with async sequences * change to use Publisher operators for auth type streams * add nondeterminsitc operation for better testability * fix unit test cases * fix broken unit test cases of AWSAPIPlugin * fix broken AWSDataStorePlugin unit test cases * fix OutgoingMutationQueue test case * remove unused methods * fix broken test cases of SyncMutationToCloudOperationTests * fix broken unit test cases of API and DataStore * resolve plugins build issues (#3654) * remove lock from SyncMutationToCloudOperation * remove test case of retryable for signOut error * resolve comments * fix(datastore): propagate remote mutationEvents to Hub for sync received (#3697) * rename the package to InternalAmplifyCredentials * rewrite NondeterminsticOperation constructor with makeStream * resolve broken test case after merging latest orgin/main * feat(amplify): make GraphQLOperationType extends from String (#3719) * refactor(datastore): new enum to represent inferred and designated authType (#3694) * refactor(datastore): new enum to represent inferred and designated auth type * resolve failed multi auth integ tests * resolve comments * fix(datastore): use error description to produce clearer error info (#3733) --------- Co-authored-by: Michael Law <[email protected]>
1 parent 0e4dc69 commit 108afe9

File tree

124 files changed

+1818
-1492
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+1818
-1492
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/Amplify-Package.xcscheme

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,48 @@
482482
ReferencedContainer = "container:">
483483
</BuildableReference>
484484
</BuildActionEntry>
485+
<BuildActionEntry
486+
buildForTesting = "YES"
487+
buildForRunning = "YES"
488+
buildForProfiling = "YES"
489+
buildForArchiving = "YES"
490+
buildForAnalyzing = "YES">
491+
<BuildableReference
492+
BuildableIdentifier = "primary"
493+
BlueprintIdentifier = "AWSPluginsSDKCore"
494+
BuildableName = "AWSPluginsSDKCore"
495+
BlueprintName = "AWSPluginsSDKCore"
496+
ReferencedContainer = "container:">
497+
</BuildableReference>
498+
</BuildActionEntry>
499+
<BuildActionEntry
500+
buildForTesting = "YES"
501+
buildForRunning = "YES"
502+
buildForProfiling = "YES"
503+
buildForArchiving = "YES"
504+
buildForAnalyzing = "YES">
505+
<BuildableReference
506+
BuildableIdentifier = "primary"
507+
BlueprintIdentifier = "AmplifyCredentials"
508+
BuildableName = "AmplifyCredentials"
509+
BlueprintName = "AmplifyCredentials"
510+
ReferencedContainer = "container:">
511+
</BuildableReference>
512+
</BuildActionEntry>
513+
<BuildActionEntry
514+
buildForTesting = "YES"
515+
buildForRunning = "YES"
516+
buildForProfiling = "YES"
517+
buildForArchiving = "YES"
518+
buildForAnalyzing = "YES">
519+
<BuildableReference
520+
BuildableIdentifier = "primary"
521+
BlueprintIdentifier = "InternalAmplifyCredentials"
522+
BuildableName = "InternalAmplifyCredentials"
523+
BlueprintName = "InternalAmplifyCredentials"
524+
ReferencedContainer = "container:">
525+
</BuildableReference>
526+
</BuildActionEntry>
485527
</BuildActionEntries>
486528
</BuildAction>
487529
<TestAction
@@ -741,6 +783,56 @@
741783
ReferencedContainer = "container:">
742784
</BuildableReference>
743785
</TestableReference>
786+
<TestableReference
787+
skipped = "NO">
788+
<BuildableReference
789+
BuildableIdentifier = "primary"
790+
BlueprintIdentifier = "AmplifyNetworkUnitTests"
791+
BuildableName = "AmplifyNetworkUnitTests"
792+
BlueprintName = "AmplifyNetworkUnitTests"
793+
ReferencedContainer = "container:">
794+
</BuildableReference>
795+
</TestableReference>
796+
<TestableReference
797+
skipped = "NO">
798+
<BuildableReference
799+
BuildableIdentifier = "primary"
800+
BlueprintIdentifier = "InternalAmplifyNetworkUnitTests"
801+
BuildableName = "InternalAmplifyNetworkUnitTests"
802+
BlueprintName = "InternalAmplifyNetworkUnitTests"
803+
ReferencedContainer = "container:">
804+
</BuildableReference>
805+
</TestableReference>
806+
<TestableReference
807+
skipped = "NO">
808+
<BuildableReference
809+
BuildableIdentifier = "primary"
810+
BlueprintIdentifier = "AWSPluginsSDKCoreTests"
811+
BuildableName = "AWSPluginsSDKCoreTests"
812+
BlueprintName = "AWSPluginsSDKCoreTests"
813+
ReferencedContainer = "container:">
814+
</BuildableReference>
815+
</TestableReference>
816+
<TestableReference
817+
skipped = "NO">
818+
<BuildableReference
819+
BuildableIdentifier = "primary"
820+
BlueprintIdentifier = "AmplifyCredentialsTests"
821+
BuildableName = "AmplifyCredentialsTests"
822+
BlueprintName = "AmplifyCredentialsTests"
823+
ReferencedContainer = "container:">
824+
</BuildableReference>
825+
</TestableReference>
826+
<TestableReference
827+
skipped = "NO">
828+
<BuildableReference
829+
BuildableIdentifier = "primary"
830+
BlueprintIdentifier = "InternalAmplifyCredentialsTests"
831+
BuildableName = "InternalAmplifyCredentialsTests"
832+
BlueprintName = "InternalAmplifyCredentialsTests"
833+
ReferencedContainer = "container:">
834+
</BuildableReference>
835+
</TestableReference>
744836
</Testables>
745837
</TestAction>
746838
<LaunchAction
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
9+
import Combine
10+
/**
11+
A non-deterministic operation offers multiple paths to accomplish its task.
12+
It attempts the next path if all preceding paths have failed with an error that allows for continuation.
13+
*/
14+
enum NondeterminsticOperationError: Error {
15+
case totalFailure
16+
case cancelled
17+
}
18+
19+
final class NondeterminsticOperation<T> {
20+
/// operation that to be eval
21+
typealias Operation = () async throws -> T
22+
typealias OnError = (Error) -> Bool
23+
24+
private let operations: AsyncStream<Operation>
25+
private var shouldTryNextOnError: OnError = { _ in true }
26+
private var cancellables = Set<AnyCancellable>()
27+
private var task: Task<Void, Never>?
28+
29+
deinit {
30+
cancel()
31+
}
32+
33+
init(operations: AsyncStream<Operation>, shouldTryNextOnError: OnError? = nil) {
34+
self.operations = operations
35+
if let shouldTryNextOnError {
36+
self.shouldTryNextOnError = shouldTryNextOnError
37+
}
38+
}
39+
40+
convenience init(
41+
operationStream: AnyPublisher<Operation, Never>,
42+
shouldTryNextOnError: OnError? = nil
43+
) {
44+
var cancellables = Set<AnyCancellable>()
45+
let (asyncStream, continuation) = AsyncStream.makeStream(of: Operation.self)
46+
operationStream.sink { _ in
47+
continuation.finish()
48+
} receiveValue: {
49+
continuation.yield($0)
50+
}.store(in: &cancellables)
51+
52+
self.init(
53+
operations: asyncStream,
54+
shouldTryNextOnError: shouldTryNextOnError
55+
)
56+
self.cancellables = cancellables
57+
}
58+
59+
/// Synchronous version of executing the operations
60+
func execute() -> Future<T, Error> {
61+
Future { [weak self] promise in
62+
self?.task = Task { [weak self] in
63+
do {
64+
if let self {
65+
promise(.success(try await self.run()))
66+
} else {
67+
promise(.failure(NondeterminsticOperationError.cancelled))
68+
}
69+
} catch {
70+
promise(.failure(error))
71+
}
72+
}
73+
}
74+
}
75+
76+
/// Asynchronous version of executing the operations
77+
func run() async throws -> T {
78+
for await operation in operations {
79+
if Task.isCancelled {
80+
throw NondeterminsticOperationError.cancelled
81+
}
82+
do {
83+
return try await operation()
84+
} catch {
85+
if shouldTryNextOnError(error) {
86+
continue
87+
} else {
88+
throw error
89+
}
90+
}
91+
}
92+
throw NondeterminsticOperationError.totalFailure
93+
}
94+
95+
/// Cancel the operation
96+
func cancel() {
97+
task?.cancel()
98+
cancellables = Set<AnyCancellable>()
99+
}
100+
}

0 commit comments

Comments
 (0)