-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Moved core code form ikMail share extension, fine tuned and uni…
…t tested
- Loading branch information
1 parent
1a473e0
commit bda03ca
Showing
8 changed files
with
459 additions
and
3 deletions.
There are no files selected for viewing
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
73 changes: 73 additions & 0 deletions
73
Sources/InfomaniakCore/Asynchronous/SendableDictionary.swift
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,73 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2023 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// A thread safe Dictionary wrapper that does not require `await`. Conforms to Sendable. | ||
/// | ||
/// Useful when dealing with UI. | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public final class SendableDictionary<T: Hashable, U>: @unchecked Sendable { | ||
let lock = DispatchQueue(label: "com.infomaniak.core.SendableDictionary.lock") | ||
private(set) var content = [T: U]() | ||
|
||
public init() { | ||
// META: keep SonarCloud happy | ||
} | ||
|
||
public var values: Dictionary<T, U>.Values { | ||
var buffer: Dictionary<T, U>.Values! | ||
lock.sync { | ||
buffer = content.values | ||
} | ||
return buffer | ||
} | ||
|
||
public func value(for key: T) -> U? { | ||
var buffer: U? | ||
lock.sync { | ||
buffer = content[key] | ||
} | ||
return buffer | ||
} | ||
|
||
public func setValue(_ value: U?, for key: T) { | ||
lock.sync { | ||
content[key] = value | ||
} | ||
} | ||
|
||
@discardableResult | ||
public func removeValue(forKey key: T) -> U? { | ||
var buffer: U? | ||
lock.sync { | ||
buffer = content.removeValue(forKey: key) | ||
} | ||
return buffer | ||
} | ||
|
||
/// Bracket get / set pattern | ||
public subscript(_ key: T) -> U? { | ||
get { | ||
value(for: key) | ||
} | ||
set { | ||
setValue(newValue, for: key) | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
Sources/InfomaniakCore/ClosureKit/AsyncCurriedClosure.swift
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,49 @@ | ||
/* | ||
Infomaniak kDrive - iOS App | ||
Copyright (C) 2023 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// Represents `any` (ie. all of them not the type) curried closure, of arbitrary type. | ||
/// | ||
/// Supports concurrency and error | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public typealias AsyncCurriedClosure<Input, Output> = (Input) async throws -> Output | ||
|
||
/// Execute the closure without waiting, discarding result | ||
postfix operator ~ | ||
|
||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public postfix func ~ (x: @escaping AsyncCurriedClosure<Void, Any>) { | ||
Task { | ||
try? await x(()) | ||
} | ||
} | ||
|
||
/// A closure that take no argument and return nothing, but technically curried. | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public typealias AsyncClosure = AsyncCurriedClosure<Void, Void> | ||
|
||
/// Append an AsyncClosure to another one | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public func + (_ lhs: @escaping AsyncClosure, _ rhs: @escaping AsyncClosure) -> AsyncClosure { | ||
let closure: AsyncClosure = { _ in | ||
try await lhs(()) | ||
try await rhs(()) | ||
} | ||
return closure | ||
} |
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,39 @@ | ||
/* | ||
Infomaniak kDrive - iOS App | ||
Copyright (C) 2023 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// Represents `any` (ie. all of them not the type) curried closure, of arbitrary type. | ||
/// | ||
/// see `AsyncCurriedClosure` if you need support for structured concurrency or error feedback | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public typealias CurriedClosure<Input, Output> = (Input) -> Output | ||
|
||
/// A closure that take no argument and return nothing, but technically curried. | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public typealias SimpleClosure = CurriedClosure<Void, Void> | ||
|
||
/// Append a SimpleClosure to another one | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public func + (_ lhs: @escaping SimpleClosure, _ rhs: @escaping SimpleClosure) -> SimpleClosure { | ||
let closure: SimpleClosure = { _ in | ||
lhs(()) | ||
rhs(()) | ||
} | ||
return closure | ||
} |
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,122 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2023 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import InfomaniakCore | ||
import XCTest | ||
|
||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public final class CountedFulfillmentTestExpectation: XCTestExpectation { | ||
private(set) var currentFulfillmentCount = 0 | ||
|
||
override public func fulfill() { | ||
currentFulfillmentCount += 1 | ||
super.fulfill() | ||
} | ||
} | ||
|
||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
final class UTAsyncCurriedClosure: XCTestCase { | ||
func testAppendToClosure() async { | ||
// GIVEN | ||
let expectationA = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectationB = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectations = [expectationA, expectationB] | ||
|
||
let a: AsyncClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 0, "expectationA should not be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 0, "expectationB should not be fulfilled") | ||
expectationA.fulfill() | ||
|
||
let randomShortTime = UInt64.random(in: 1 ... 100) | ||
try await Task.sleep(nanoseconds: randomShortTime) | ||
} | ||
|
||
let b: AsyncClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 1, "expectationA should be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 0, "expectationB should not be fulfilled") | ||
expectationB.fulfill() | ||
|
||
let randomShortTime = UInt64.random(in: 1 ... 100) | ||
try await Task.sleep(nanoseconds: randomShortTime) | ||
} | ||
|
||
// WHEN | ||
let computation = a + b | ||
do { | ||
let _ = try await computation~ | ||
} | ||
|
||
// THEN | ||
catch { | ||
XCTFail("error: \(error)") | ||
} | ||
|
||
await fulfillment(of: expectations) | ||
} | ||
|
||
func testAppendToClosure_3chain() async { | ||
// GIVEN | ||
let expectationA = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectationB = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectationC = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectations = [expectationA, expectationB, expectationC] | ||
|
||
let a: AsyncClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 0, "expectationA should not be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 0, "expectationB should not be fulfilled") | ||
XCTAssertEqual(expectationC.currentFulfillmentCount, 0, "expectationC should not be fulfilled") | ||
expectationA.fulfill() | ||
|
||
let randomShortTime = UInt64.random(in: 1 ... 100) | ||
try await Task.sleep(nanoseconds: randomShortTime) | ||
} | ||
|
||
let b: AsyncClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 1, "expectationA should be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 0, "expectationB should not be fulfilled") | ||
XCTAssertEqual(expectationC.currentFulfillmentCount, 0, "expectationC should not be fulfilled") | ||
expectationB.fulfill() | ||
|
||
let randomShortTime = UInt64.random(in: 1 ... 100) | ||
try await Task.sleep(nanoseconds: randomShortTime) | ||
} | ||
|
||
let c: AsyncClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 1, "expectation should be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 1, "expectation should be fulfilled") | ||
XCTAssertEqual(expectationC.currentFulfillmentCount, 0, "expectationC should not be fulfilled") | ||
expectationC.fulfill() | ||
|
||
let randomShortTime = UInt64.random(in: 1 ... 100) | ||
try await Task.sleep(nanoseconds: randomShortTime) | ||
} | ||
|
||
// WHEN | ||
let computation = a + b + c | ||
do { | ||
let _ = try await computation~ | ||
} | ||
|
||
// THEN | ||
catch { | ||
XCTFail("error: \(error)") | ||
} | ||
|
||
await fulfillment(of: expectations) | ||
} | ||
} |
Oops, something went wrong.