Skip to content
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

refactor: use swift-concurrency-extras #158

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,23 @@ let package = Package(
.package(url: "https://github.com/kishikawakatsumi/KeychainAccess", from: "4.2.2"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.8.1"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"),
],
targets: [
.target(name: "_Helpers"),
.target(name: "Functions", dependencies: ["_Helpers"]),
.testTarget(name: "FunctionsTests", dependencies: ["Functions"]),
.testTarget(
name: "FunctionsTests",
dependencies: [
"Functions",
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
]
),
.target(
name: "GoTrue",
dependencies: [
"_Helpers",
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "KeychainAccess", package: "KeychainAccess"),
]
),
Expand All @@ -49,7 +57,13 @@ let package = Package(
],
resources: [.process("Resources")]
),
.target(name: "PostgREST", dependencies: ["_Helpers"]),
.target(
name: "PostgREST",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
"_Helpers",
]
),
.testTarget(
name: "PostgRESTTests",
dependencies: [
Expand All @@ -60,13 +74,20 @@ let package = Package(
exclude: ["__Snapshots__"]
),
.testTarget(name: "PostgRESTIntegrationTests", dependencies: ["PostgREST"]),
.target(name: "Realtime", dependencies: ["_Helpers"]),
.target(
name: "Realtime",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
"_Helpers",
]
),
.testTarget(name: "RealtimeTests", dependencies: ["Realtime"]),
.target(name: "Storage", dependencies: ["_Helpers"]),
.testTarget(name: "StorageTests", dependencies: ["Storage"]),
.target(
name: "Supabase",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
"GoTrue",
"Storage",
"Realtime",
Expand Down
2 changes: 1 addition & 1 deletion Sources/GoTrue/Internal/Dependencies.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ConcurrencyExtras
import Foundation
@_spi(Internal) import _Helpers

struct Dependencies: Sendable {
static let current = LockIsolated(Dependencies?.none)
Expand Down
2 changes: 1 addition & 1 deletion Sources/GoTrue/Internal/EventEmitter.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ConcurrencyExtras
import Foundation
@_spi(Internal) import _Helpers

struct EventEmitter: Sendable {
var attachListener: @Sendable () -> (
Expand Down
5 changes: 3 additions & 2 deletions Sources/PostgREST/PostgrestBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ConcurrencyExtras
import Foundation
@_spi(Internal) import _Helpers

Expand All @@ -18,7 +19,7 @@ public class PostgrestBuilder: @unchecked Sendable {
var fetchOptions: FetchOptions
}

let mutableState: ActorIsolated<MutableState>
let mutableState: LockIsolated<MutableState>

init(
configuration: PostgrestClient.Configuration,
Expand All @@ -27,7 +28,7 @@ public class PostgrestBuilder: @unchecked Sendable {
self.configuration = configuration
http = HTTPClient(fetchHandler: configuration.fetch)

mutableState = ActorIsolated(
mutableState = LockIsolated(
MutableState(
request: request,
fetchOptions: FetchOptions()
Expand Down
3 changes: 2 additions & 1 deletion Sources/Realtime/RealtimeChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Foundation
import Swift
@_spi(Internal) import _Helpers
import ConcurrencyExtras

/// Container class of bindings to the channel
struct Binding {
Expand Down Expand Up @@ -439,7 +440,7 @@ public class RealtimeChannel {
}
}

self.bindings.withValue {
self.bindings.withValue { [newPostgresBindings] in
$0["postgres_changes"] = newPostgresBindings
}
callback?(.subscribed, nil)
Expand Down
17 changes: 9 additions & 8 deletions Sources/Realtime/RealtimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import Foundation
@_spi(Internal) import _Helpers
import ConcurrencyExtras

public enum SocketError: Error {
case abnormalClosureError
Expand Down Expand Up @@ -396,7 +397,7 @@ public class RealtimeClient: PhoenixTransportDelegate {
var delegated = Delegated<URLResponse?, Void>()
delegated.manuallyDelegate(with: callback)

return stateChangeCallbacks.open.withValue {
return stateChangeCallbacks.open.withValue { [delegated] in
self.append(callback: delegated, to: &$0)
}
}
Expand Down Expand Up @@ -439,7 +440,7 @@ public class RealtimeClient: PhoenixTransportDelegate {
var delegated = Delegated<URLResponse?, Void>()
delegated.delegate(to: owner, with: callback)

return stateChangeCallbacks.open.withValue {
return stateChangeCallbacks.open.withValue { [delegated] in
self.append(callback: delegated, to: &$0)
}
}
Expand Down Expand Up @@ -474,7 +475,7 @@ public class RealtimeClient: PhoenixTransportDelegate {
var delegated = Delegated<(Int, String?), Void>()
delegated.manuallyDelegate(with: callback)

return stateChangeCallbacks.close.withValue {
return stateChangeCallbacks.close.withValue { [delegated] in
self.append(callback: delegated, to: &$0)
}
}
Expand Down Expand Up @@ -517,7 +518,7 @@ public class RealtimeClient: PhoenixTransportDelegate {
var delegated = Delegated<(Int, String?), Void>()
delegated.delegate(to: owner, with: callback)

return stateChangeCallbacks.close.withValue {
return stateChangeCallbacks.close.withValue { [delegated] in
self.append(callback: delegated, to: &$0)
}
}
Expand All @@ -537,7 +538,7 @@ public class RealtimeClient: PhoenixTransportDelegate {
var delegated = Delegated<(Error, URLResponse?), Void>()
delegated.manuallyDelegate(with: callback)

return stateChangeCallbacks.error.withValue {
return stateChangeCallbacks.error.withValue { [delegated] in
self.append(callback: delegated, to: &$0)
}
}
Expand All @@ -561,7 +562,7 @@ public class RealtimeClient: PhoenixTransportDelegate {
var delegated = Delegated<(Error, URLResponse?), Void>()
delegated.delegate(to: owner, with: callback)

return stateChangeCallbacks.error.withValue {
return stateChangeCallbacks.error.withValue { [delegated] in
self.append(callback: delegated, to: &$0)
}
}
Expand All @@ -582,7 +583,7 @@ public class RealtimeClient: PhoenixTransportDelegate {
var delegated = Delegated<Message, Void>()
delegated.manuallyDelegate(with: callback)

return stateChangeCallbacks.message.withValue {
return stateChangeCallbacks.message.withValue { [delegated] in
append(callback: delegated, to: &$0)
}
}
Expand All @@ -606,7 +607,7 @@ public class RealtimeClient: PhoenixTransportDelegate {
var delegated = Delegated<Message, Void>()
delegated.delegate(to: owner, with: callback)

return stateChangeCallbacks.message.withValue {
return stateChangeCallbacks.message.withValue { [delegated] in
self.append(callback: delegated, to: &$0)
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ConcurrencyExtras
import Foundation
@_spi(Internal) import _Helpers
@_exported import Functions
Expand Down
28 changes: 0 additions & 28 deletions Sources/_Helpers/ActorIsolated.swift

This file was deleted.

21 changes: 0 additions & 21 deletions Sources/_Helpers/AsyncStream.swift

This file was deleted.

57 changes: 0 additions & 57 deletions Sources/_Helpers/LockIsolated.swift

This file was deleted.

17 changes: 0 additions & 17 deletions Sources/_Helpers/Task.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Tests/FunctionsTests/FunctionsClientTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import XCTest

import ConcurrencyExtras
@_spi(Internal) import _Helpers
@testable import Functions

Expand All @@ -14,7 +14,7 @@ final class FunctionsClientTests: XCTestCase {
let _request = ActorIsolated(URLRequest?.none)

let sut = FunctionsClient(url: self.url, headers: ["apikey": apiKey]) { request in
_request.setValue(request)
await _request.setValue(request)
return (
Data(), HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)!
)
Expand All @@ -27,7 +27,7 @@ final class FunctionsClientTests: XCTestCase {
options: .init(headers: ["X-Custom-Key": "value"], body: body)
)

let request = _request.value
let request = await _request.value

XCTAssertEqual(request?.url, url)
XCTAssertEqual(request?.httpMethod, "POST")
Expand Down
6 changes: 4 additions & 2 deletions Tests/GoTrueTests/GoTrueClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import XCTest
@_spi(Internal) import _Helpers
import ConcurrencyExtras

@testable import GoTrue

Expand All @@ -28,7 +29,7 @@ final class GoTrueClientTests: XCTestCase {

let streamTask = Task {
for await (event, _) in authStateStream {
events.withValue {
await events.withValue {
$0.append(event)
}

Expand All @@ -38,7 +39,8 @@ final class GoTrueClientTests: XCTestCase {

await fulfillment(of: [expectation])

XCTAssertEqual(events.value, [.initialSession])
let events = await events.value
XCTAssertEqual(events, [.initialSession])

streamTask.cancel()
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/GoTrueTests/Mocks/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func withDependencies(
let current = Dependencies.current.value ?? .mock
var copy = current
mutation(&copy)
Dependencies.current.withValue { $0 = copy }
Dependencies.current.withValue { [copy] in $0 = copy }
defer { Dependencies.current.setValue(current) }
try await operation()
}
Expand Down
Loading