Last active
August 13, 2020 12:26
-
-
Save geor-kasapidi/22ba6695480c613f25a2d98be9372471 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| final class SharedOperation<T, E: Swift.Error> { | |
| typealias Completion = (Result<T, E>) -> Void | |
| private let queue = DispatchQueue(label: "SharedOperation.queue") | |
| private let execute: (@escaping Completion) -> Void | |
| private var completions: [Completion] = [] | |
| init(_ execute: @escaping (@escaping Completion) -> Void) { | |
| self.execute = execute | |
| } | |
| func perform(_ completion: @escaping Completion) { | |
| self.queue.async { | |
| guard self.completions.isEmpty else { | |
| self.completions.append(completion) | |
| return | |
| } | |
| self.completions.append(completion) | |
| self.execute { result in | |
| self.queue.async { | |
| self.completions.forEach { | |
| $0(result) | |
| } | |
| self.completions = [] | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment