Skip to content

Commit

Permalink
Added ability to specify custom result provider (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lumyk authored Aug 11, 2023
1 parent 0ea300c commit ebcd98e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,37 @@ import PromiseKit

public protocol AnySynchronizationCustomPerformer: AnyObject {
func customPerform<Result: AnySyncResult>(for request: URLRequest, node: SyncNode) -> Promise<Result>?
func customResult<Result: AnySyncResult>(data: Data,
request: URLRequest,
response: URLResponse,
node: SyncNode) throws -> Result?
func resolve(error: Error, model: AnySynchronizable, settings: AnySyncSettings) -> SynchronizationResolveTypes
func afterSynchronization(model: AnySynchronizable, result: AnySyncResult) -> Promise<Void>
func afterAll(model: AnySynchronizable, result: AnySyncResult) -> Promise<Void>
}

public extension AnySynchronizationCustomPerformer { // for optional
func customPerform<Result: AnySynchronizable>(for request: URLRequest, node: SyncNode) -> Promise<Result>? { nil }
func resolve(error: Error, model: AnySynchronizable) -> SynchronizationResolveTypes { .none }
func afterSynchronization(model: AnySynchronizable, result: AnySyncResult) -> Promise<Void> { .value }
func afterAll(model: AnySynchronizable, result: AnySyncResult) -> Promise<Void> { .value }

func customPerform<Result: AnySynchronizable>(for request: URLRequest, node: SyncNode) -> Promise<Result>? {
nil
}

func customResult<Result: AnySyncResult>(data: Data,
request: URLRequest,
response: URLResponse,
node: SyncNode) throws -> Result? {
nil
}

func resolve(error: Error, model: AnySynchronizable) -> SynchronizationResolveTypes {
.none
}

func afterSynchronization(model: AnySynchronizable, result: AnySyncResult) -> Promise<Void> {
.value
}

func afterAll(model: AnySynchronizable, result: AnySyncResult) -> Promise<Void> {
.value
}
}
35 changes: 16 additions & 19 deletions Sources/Synchronization-swift/SynchronizationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,6 @@
import Foundation
import PromiseKit

public enum SynchronizationResolveTypes { // TODO: move

/// Not resolve and throw error
case none

/// Retry same operation
case retry

/// Retry same operation after delay in seconds
case retryAfter(seconds: TimeInterval)

/// Retry with another URLRequest
case retryWith(request: URLRequest)

/// Reset type will call object request from AnySynchronizable
case reset
}

public final class SynchronizationManager<Result: AnySyncResult> {

private let settings: AnySyncSettings
Expand Down Expand Up @@ -72,7 +54,22 @@ public final class SynchronizationManager<Result: AnySyncResult> {
return result
} else {
return self.service.perform(for: request)
.map { try Result.from(data: $0.data, request: request, response: $0.response) }
.map { result in
let customResult: Result? = try customPerformer?.customResult(
data: result.data,
request: request,
response: result.response,
node: node
)
let result = {
try Result.from(
data: result.data,
request: request,
response: result.response
)
}
return try customResult ?? result()
}
.recover {
tryToResolve ? self.tryToResolve(
error: $0,
Expand Down
26 changes: 26 additions & 0 deletions Sources/Synchronization-swift/SynchronizationResolveTypes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// File.swift
//
//
// Created by Evegeny Kalashnikov on 11.08.2023.
//

import Foundation

public enum SynchronizationResolveTypes {

/// Not resolve and throw error
case none

/// Retry same operation
case retry

/// Retry same operation after delay in seconds
case retryAfter(seconds: TimeInterval)

/// Retry with another URLRequest
case retryWith(request: URLRequest)

/// Reset type will call object request from AnySynchronizable
case reset
}

0 comments on commit ebcd98e

Please sign in to comment.