From 78a3b2dd998f5cb814d0dc058002b9ed32b27bd0 Mon Sep 17 00:00:00 2001 From: Andre Terron Date: Mon, 8 Jun 2020 21:11:00 -0700 Subject: [PATCH] Replace successes with PromiseKit's when --- Sources/Application.swift | 23 +++++++++++---------- Sources/Successes+PromiseKit.swift | 32 ------------------------------ Swindler.xcodeproj/project.pbxproj | 5 +---- 3 files changed, 13 insertions(+), 47 deletions(-) delete mode 100644 Sources/Successes+PromiseKit.swift diff --git a/Sources/Application.swift b/Sources/Application.swift index 0c25638..04bd26f 100644 --- a/Sources/Application.swift +++ b/Sources/Application.swift @@ -242,18 +242,19 @@ final class OSXApplicationDelegate< // Initialize OSXWindowDelegates from the window elements. let windowPromises = windowElements.map({ windowElement in self.createWindowForElementIfNotExists(windowElement) + // Log any errors we encounter, but don't fail. + .recover { (error: Error) -> Promise in + log.debug({ + let description: String = + (try? windowElement.attribute(.description) ?? "") ?? "" + return "Couldn't initialize window for element \(windowElement) " + + "(\(description)) of \(self): \(error)" + }()) + return Promise.value(nil) + } }) - - return successes(windowPromises, onError: { index, error in - // Log any errors we encounter, but don't fail. - let windowElement = windowElements[index] - log.debug({ - let description: String = - (try? windowElement.attribute(.description) ?? "") ?? "" - return "Couldn't initialize window for element \(windowElement) " - + "(\(description)) of \(self): \(error)" - }()) - }).asVoid() + + return when(fulfilled: windowPromises).asVoid() } } diff --git a/Sources/Successes+PromiseKit.swift b/Sources/Successes+PromiseKit.swift deleted file mode 100644 index 6ddd107..0000000 --- a/Sources/Successes+PromiseKit.swift +++ /dev/null @@ -1,32 +0,0 @@ -// Based on Any+PromiseKit.swift, -// Created by Daniel Tartaglia on 11/4/15. -// Copyright © 2015. MIT License. - -import PromiseKit - -/** Waits on all provided promises, then resolves to the result of the successful ones. -- Param onError: A callback that is called each time any promise fails, with the index of the - promise and the error. -- Returns: A new promise that resolves once all the provided promises resolve, containing an array - of the results from the successful promises. -*/ -func successes(_ promises: [Promise], onError: @escaping (Int, Error) -> Void) --> Promise<[T]> { - guard !promises.isEmpty else { return .value([]) } - return Promise<[T]> { seal in - var values = [T]() - var countdown = promises.count - for (index, promise) in promises.enumerated() { - promise.done { value in - values.append(value) - }.ensure { - countdown -= 1 - if countdown == 0 { - seal.fulfill(values) - } - }.catch { error in - onError(index, error) - } - } - } -} diff --git a/Swindler.xcodeproj/project.pbxproj b/Swindler.xcodeproj/project.pbxproj index 0ce1730..ef4b652 100644 --- a/Swindler.xcodeproj/project.pbxproj +++ b/Swindler.xcodeproj/project.pbxproj @@ -10,7 +10,6 @@ 2F00BBD92142F32700457FB2 /* FakeSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F00BBD82142F32700457FB2 /* FakeSpec.swift */; }; 2F00BBDB21432A9000457FB2 /* FakeAXSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F00BBDA21432A9000457FB2 /* FakeAXSwift.swift */; }; 2F00BBDD2143769C00457FB2 /* FakeSwindler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F00BBDC2143769C00457FB2 /* FakeSwindler.swift */; }; - 2F55343D1C0AC86500B87C42 /* Successes+PromiseKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F55343C1C0AC86500B87C42 /* Successes+PromiseKit.swift */; }; 2F61898F1C22718E00C422C0 /* ApplicationSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F61898B1C22718E00C422C0 /* ApplicationSpec.swift */; }; 2F6189901C22718E00C422C0 /* StateSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F61898C1C22718E00C422C0 /* StateSpec.swift */; }; 2F6189921C22718E00C422C0 /* WindowSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F61898E1C22718E00C422C0 /* WindowSpec.swift */; }; @@ -56,7 +55,6 @@ 2F00BBD82142F32700457FB2 /* FakeSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeSpec.swift; sourceTree = ""; }; 2F00BBDA21432A9000457FB2 /* FakeAXSwift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeAXSwift.swift; sourceTree = ""; }; 2F00BBDC2143769C00457FB2 /* FakeSwindler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeSwindler.swift; sourceTree = ""; }; - 2F55343C1C0AC86500B87C42 /* Successes+PromiseKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Successes+PromiseKit.swift"; sourceTree = ""; }; 2F61898B1C22718E00C422C0 /* ApplicationSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApplicationSpec.swift; sourceTree = ""; }; 2F61898C1C22718E00C422C0 /* StateSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateSpec.swift; sourceTree = ""; }; 2F61898E1C22718E00C422C0 /* WindowSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WindowSpec.swift; sourceTree = ""; }; @@ -195,7 +193,6 @@ 2F86CEE11BD6D9E1007E8C29 /* Events.swift */, 2F7F01011C11570D00C97949 /* Errors.swift */, 2F7F01031C11578800C97949 /* AXSwiftProtocols.swift */, - 2F55343C1C0AC86500B87C42 /* Successes+PromiseKit.swift */, 2F86CECB1BD60117007E8C29 /* Info.plist */, 2F9E31A81BF050AF00EAD248 /* Property.swift */, 2FEEA2E21C1E6E9200CD515E /* Log.swift */, @@ -345,6 +342,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, Base, ); @@ -507,7 +505,6 @@ 2F7F01041C11578800C97949 /* AXSwiftProtocols.swift in Sources */, 2F9E31A91BF050AF00EAD248 /* Property.swift in Sources */, 2F6314291C49C0DD001D1E24 /* Screen.swift in Sources */, - 2F55343D1C0AC86500B87C42 /* Successes+PromiseKit.swift in Sources */, 2F7F01021C11570D00C97949 /* Errors.swift in Sources */, 2F86CEE41BD6DAD7007E8C29 /* State.swift in Sources */, 2F86CEE21BD6D9E1007E8C29 /* Events.swift in Sources */,