Skip to content

Commit

Permalink
Adopt LockedState and remove swift-concurrency-extras
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Jan 16, 2024
1 parent e398853 commit 4e2bfa1
Show file tree
Hide file tree
Showing 46 changed files with 362 additions and 292 deletions.
21 changes: 2 additions & 19 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import PackageDescription
var dependencies: [Package.Dependency] = [
.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"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
]

var goTrueDependencies: [Target.Dependency] = [
"_Helpers",
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "Crypto", package: "swift-crypto"),
]

Expand Down Expand Up @@ -48,18 +45,11 @@ let package = Package(
],
dependencies: dependencies,
targets: [
.target(
name: "_Helpers",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
]
),
.target(name: "Functions", dependencies: ["_Helpers"]),
.target(name: "Functions"),
.testTarget(
name: "FunctionsTests",
dependencies: [
"Functions",
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
]
),
.target(
Expand All @@ -70,7 +60,6 @@ let package = Package(
name: "AuthTests",
dependencies: [
"Auth",
"_Helpers",
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
],
Expand All @@ -82,15 +71,12 @@ let package = Package(
.target(
name: "PostgREST",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
"_Helpers",
]
),
.testTarget(
name: "PostgRESTTests",
dependencies: [
"PostgREST",
"_Helpers",
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
],
exclude: ["__Snapshots__"]
Expand All @@ -99,17 +85,14 @@ let package = Package(
.target(
name: "Realtime",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
"_Helpers",
]
),
.testTarget(name: "RealtimeTests", dependencies: ["Realtime"]),
.target(name: "Storage", dependencies: ["_Helpers"]),
.target(name: "Storage"),
.testTarget(name: "StorageTests", dependencies: ["Storage"]),
.target(
name: "Supabase",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
"Auth",
"Storage",
"Realtime",
Expand Down
19 changes: 9 additions & 10 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
@_spi(Internal) import _Helpers

#if canImport(FoundationNetworking)
import FoundationNetworking
Expand Down Expand Up @@ -52,27 +51,27 @@ public actor AuthClient {
}

private var configuration: Configuration {
Dependencies.current.value!.configuration
Dependencies.current.withLock { $0!.configuration }
}

private var api: APIClient {
Dependencies.current.value!.api
Dependencies.current.withLock { $0!.api }
}

private var sessionManager: SessionManager {
Dependencies.current.value!.sessionManager
Dependencies.current.withLock { $0!.sessionManager }
}

private var codeVerifierStorage: CodeVerifierStorage {
Dependencies.current.value!.codeVerifierStorage
Dependencies.current.withLock { $0!.codeVerifierStorage }
}

private var eventEmitter: EventEmitter {
Dependencies.current.value!.eventEmitter
Dependencies.current.withLock { $0!.eventEmitter }
}

private var currentDate: @Sendable () -> Date {
Dependencies.current.value!.currentDate
Dependencies.current.withLock { $0!.currentDate }
}

/// Returns the session, refreshing it if necessary.
Expand Down Expand Up @@ -147,8 +146,8 @@ public actor AuthClient {
) {
mfa = AuthMFA()

Dependencies.current.setValue(
Dependencies(
Dependencies.current.withLock {
$0 = Dependencies(
configuration: configuration,
sessionManager: sessionManager,
api: api,
Expand All @@ -161,7 +160,7 @@ public actor AuthClient {
),
codeVerifierStorage: codeVerifierStorage
)
)
}
}

/// Listen for auth state changes.
Expand Down
9 changes: 4 additions & 5 deletions Sources/Auth/AuthMFA.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import Foundation
@_spi(Internal) import _Helpers

/// Contains the full multi-factor authentication API.
public actor AuthMFA {
private var api: APIClient {
Dependencies.current.value!.api
Dependencies.current.withLock { $0!.api }
}

private var sessionManager: SessionManager {
Dependencies.current.value!.sessionManager
Dependencies.current.withLock { $0!.sessionManager }
}

private var configuration: AuthClient.Configuration {
Dependencies.current.value!.configuration
Dependencies.current.withLock { $0!.configuration }
}

private var eventEmitter: EventEmitter {
Dependencies.current.value!.eventEmitter
Dependencies.current.withLock { $0!.eventEmitter }
}

/// Starts the enrollment process for a new Multi-Factor Authentication (MFA) factor. This method
Expand Down
1 change: 0 additions & 1 deletion Sources/Auth/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Guilherme Souza on 14/12/23.
//

@_spi(Internal) import _Helpers
import Foundation

extension AuthClient.Configuration {
Expand Down
5 changes: 2 additions & 3 deletions Sources/Auth/Internal/APIClient.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
@_spi(Internal) import _Helpers

struct APIClient: Sendable {
var execute: @Sendable (_ request: Request) async throws -> Response
Expand All @@ -8,7 +7,7 @@ struct APIClient: Sendable {
extension APIClient {
static func live(http: HTTPClient) -> Self {
var configuration: AuthClient.Configuration {
Dependencies.current.value!.configuration
Dependencies.current.withLock { $0!.configuration }
}

return APIClient(
Expand All @@ -35,7 +34,7 @@ extension APIClient {
extension APIClient {
@discardableResult
func authorizedExecute(_ request: Request) async throws -> Response {
let session = try await Dependencies.current.value!.sessionManager.session()
let session = try await Dependencies.current.withLock { $0!.sessionManager }.session()

var request = request
request.headers["Authorization"] = "Bearer \(session.accessToken)"
Expand Down
3 changes: 1 addition & 2 deletions Sources/Auth/Internal/CodeVerifierStorage.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
@_spi(Internal) import _Helpers

struct CodeVerifierStorage: Sendable {
var getCodeVerifier: @Sendable () throws -> String?
Expand All @@ -10,7 +9,7 @@ struct CodeVerifierStorage: Sendable {
extension CodeVerifierStorage {
static var live: Self = {
var localStorage: AuthLocalStorage {
Dependencies.current.value!.configuration.localStorage
Dependencies.current.withLock { $0!.configuration.localStorage }
}

let key = "supabase.code-verifier"
Expand Down
3 changes: 1 addition & 2 deletions Sources/Auth/Internal/Dependencies.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import ConcurrencyExtras
import Foundation

struct Dependencies: Sendable {
static let current = LockIsolated(Dependencies?.none)
static let current = LockedState(initialState: Dependencies?.none)

var configuration: AuthClient.Configuration
var sessionManager: SessionManager
Expand Down
15 changes: 8 additions & 7 deletions Sources/Auth/Internal/EventEmitter.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ConcurrencyExtras
import Foundation

struct EventEmitter: Sendable {
Expand All @@ -17,8 +16,8 @@ extension EventEmitter {

extension EventEmitter {
static var live: Self = {
let continuations = LockIsolated(
[UUID: AsyncStream<(event: AuthChangeEvent, session: Session?)>.Continuation]()
let continuations = LockedState(
initialState: [UUID: AsyncStream<(event: AuthChangeEvent, session: Session?)>.Continuation]()
)

return Self(
Expand All @@ -29,12 +28,12 @@ extension EventEmitter {
.makeStream()

continuation.onTermination = { [id] _ in
continuations.withValue {
continuations.withLock {
$0[id] = nil
}
}

continuations.withValue {
continuations.withLock {
$0[id] = continuation
}

Expand All @@ -50,9 +49,11 @@ extension EventEmitter {
]
)
if let id {
continuations.value[id]?.yield((event, session))
_ = continuations.withLock {
$0[id]?.yield((event, session))
}
} else {
for continuation in continuations.value.values {
for continuation in continuations.withLock(\.values) {
continuation.yield((event, session))
}
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/Auth/Internal/SessionManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
@_spi(Internal) import _Helpers

struct SessionRefresher: Sendable {
var refreshSession: @Sendable (_ refreshToken: String) async throws -> Session
Expand Down Expand Up @@ -30,11 +29,11 @@ actor _LiveSessionManager {
private var task: Task<Session, Error>?

private var storage: SessionStorage {
Dependencies.current.value!.sessionStorage
Dependencies.current.withLock { $0!.sessionStorage }
}

private var sessionRefresher: SessionRefresher {
Dependencies.current.value!.sessionRefresher
Dependencies.current.withLock { $0!.sessionRefresher }
}

func session(shouldValidateExpiration: Bool) async throws -> Session {
Expand Down
3 changes: 1 addition & 2 deletions Sources/Auth/Internal/SessionStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
@_spi(Internal) import _Helpers

/// A locally stored ``Session``, it contains metadata such as `expirationDate`.
struct StoredSession: Codable {
Expand All @@ -32,7 +31,7 @@ struct SessionStorage: Sendable {
extension SessionStorage {
static var live: Self = {
var localStorage: AuthLocalStorage {
Dependencies.current.value!.configuration.localStorage
Dependencies.current.withLock { $0!.configuration.localStorage }
}

return Self(
Expand Down
3 changes: 0 additions & 3 deletions Sources/Auth/Types.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import Foundation
@_spi(Internal) import _Helpers

public typealias AnyJSON = _Helpers.AnyJSON

public enum AuthChangeEvent: String, Sendable {
case initialSession = "INITIAL_SESSION"
Expand Down
1 change: 1 addition & 0 deletions Sources/Auth/_Internal
3 changes: 0 additions & 3 deletions Sources/Functions/FunctionsClient.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import Foundation
@_spi(Internal) import _Helpers

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

let version = _Helpers.version

/// An actor representing a client for invoking functions.
public actor FunctionsClient {
/// Fetch handler used to make requests.
Expand Down
1 change: 1 addition & 0 deletions Sources/Functions/_Internal
3 changes: 0 additions & 3 deletions Sources/PostgREST/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
//

import Foundation
@_spi(Internal) import _Helpers

let version = _Helpers.version

extension PostgrestClient.Configuration {
private static let supportedDateFormatters: [ISO8601DateFormatter] = [
Expand Down
Loading

0 comments on commit 4e2bfa1

Please sign in to comment.