-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Request Deduplication #24
Open
matusklasovity
wants to merge
4
commits into
main
Choose a base branch
from
feature/deduplication
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...ple/GoodNetworking-Sample.xcodeproj/xcshareddata/xcschemes/GoodNetworking-Sample.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1540" | ||
version = "1.7"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES" | ||
buildArchitectures = "Automatic"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "EACEC3F529953DCB008242AA" | ||
BuildableName = "GoodNetworking-Sample.app" | ||
BlueprintName = "GoodNetworking-Sample" | ||
ReferencedContainer = "container:GoodNetworking-Sample.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
shouldAutocreateTestPlan = "YES"> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
<BuildableProductRunnable | ||
runnableDebuggingMode = "0"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "EACEC3F529953DCB008242AA" | ||
BuildableName = "GoodNetworking-Sample.app" | ||
BlueprintName = "GoodNetworking-Sample" | ||
ReferencedContainer = "container:GoodNetworking-Sample.xcodeproj"> | ||
</BuildableReference> | ||
</BuildableProductRunnable> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<BuildableProductRunnable | ||
runnableDebuggingMode = "0"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "EACEC3F529953DCB008242AA" | ||
BuildableName = "GoodNetworking-Sample.app" | ||
BlueprintName = "GoodNetworking-Sample" | ||
ReferencedContainer = "container:GoodNetworking-Sample.xcodeproj"> | ||
</BuildableReference> | ||
</BuildableProductRunnable> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// ExecutorTask.swift | ||
// | ||
// | ||
// Created by Matus Klasovity on 06/08/2024. | ||
// | ||
|
||
import Foundation | ||
import Alamofire | ||
|
||
final class ExecutorTask<T: Decodable & Sendable> { | ||
|
||
var finishDate: Date? | ||
let taskID: String | ||
let task: Task<DataResponse<T, AFError>, Never> | ||
|
||
init(taskID: String, task: Task<DataResponse<T, AFError>, Never>) { | ||
self.taskID = taskID | ||
self.task = task | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// RequestExecutor.swift | ||
// | ||
// | ||
// Created by Matus Klasovity on 06/08/2024. | ||
// | ||
|
||
import Foundation | ||
import Alamofire | ||
|
||
actor RequestExecutor { | ||
|
||
private let logger: SessionLogger = { | ||
if #available(iOS 14, *) { | ||
return OSLogLogger() | ||
} else { | ||
return PrintLogger() | ||
} | ||
}() | ||
|
||
private var runningRequestTasks: [String: Any] = [:] | ||
|
||
func execute<SuccessType: Decodable>( | ||
_ request: DataRequest, | ||
taskID: String, | ||
deduplicate: Bool, | ||
validResponseCodes: Set<Int>, | ||
emptyResponseCodes: Set<Int>, | ||
emptyResponseMethods: Set<HTTPMethod> | ||
) async -> DataResponse<SuccessType, AFError> { | ||
let randomUUID = UUID().uuidString | ||
return await execute( | ||
request, | ||
taskID: deduplicate ? taskID : randomUUID, | ||
validResponseCodes: validResponseCodes, | ||
emptyResponseCodes: emptyResponseCodes, | ||
emptyResponseMethods: emptyResponseMethods | ||
) | ||
} | ||
|
||
private func execute<SuccessType: Decodable & Sendable>( | ||
_ request: DataRequest, | ||
taskID: String, | ||
validResponseCodes: Set<Int>, | ||
emptyResponseCodes: Set<Int>, | ||
emptyResponseMethods: Set<HTTPMethod> | ||
) async -> DataResponse<SuccessType, AFError> { | ||
if let runningTask = runningRequestTasks[taskID] { | ||
let executorTask = runningTask as! ExecutorTask<SuccessType> | ||
logger.log(level: .info, message: "🚀 taskID: \(taskID) Cached value used") | ||
return await executorTask.task.value | ||
} else { | ||
let requestTask = Task { | ||
return await request.goodifyAsync( | ||
validResponseCodes: validResponseCodes, | ||
emptyResponseCodes: emptyResponseCodes, | ||
emptyResponseMethods: emptyResponseMethods | ||
) as DataResponse<SuccessType, AFError> | ||
} | ||
|
||
logger.log(level: .info, message: "🚀 taskID: \(taskID): Task created") | ||
let executorTask: ExecutorTask = ExecutorTask( | ||
taskID: taskID, | ||
task: requestTask | ||
) | ||
|
||
runningRequestTasks[taskID] = executorTask | ||
|
||
let result = await requestTask.value | ||
|
||
logger.log(level: .info, message: "🚀 taskID: \(taskID): Task finished successfully") | ||
return result | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// FutureExtensions.swift | ||
// | ||
// | ||
// Created by Matus Klasovity on 06/08/2024. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
extension Future where Failure == Error { | ||
|
||
static func create(asyncThrowableFunc: @Sendable @escaping () async throws -> Output) -> Self { | ||
Self.init { promise in | ||
nonisolated(unsafe) let promise = promise | ||
Task { | ||
do { | ||
let result = try await asyncThrowableFunc() | ||
await MainActor.run { | ||
promise(.success(result)) | ||
} | ||
} catch { | ||
await MainActor.run { | ||
promise(.failure(error)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
extension Future where Failure == Never { | ||
|
||
static func create(asyncFunc: @Sendable @escaping () async -> Output) -> Self { | ||
Self.init { promise in | ||
nonisolated(unsafe) let promise = promise | ||
Task { | ||
let result = await asyncFunc() | ||
await MainActor.run { | ||
promise(.success(result)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GoodLogger použi