Skip to content

Commit 741331d

Browse files
authored
Merge pull request #36 from GoodRequest/feature/transient-session
feat: Transient session struct
2 parents fa451ea + 1fbf36f commit 741331d

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

Package.resolved

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/GoodNetworking/Protocols/ResourceOperations.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,6 @@ public extension Updatable where UpdateResponse == Resource {
397397

398398
}
399399

400-
// MARK: - Deletable.swift
401-
// GoodNetworking
402-
//
403-
// Created by Andrej Jasso on 15/10/2024.
404-
//
405-
406400
// MARK: - Deletable
407401

408402
/// Represents a resource that can be deleted from a remote server.

Sources/GoodNetworking/Session/FutureSession.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ public actor FutureSession {
1515
/// The type alias for a supplier function that provides a `NetworkSession` asynchronously.
1616
public typealias FutureSessionSupplier = (@Sendable () async -> NetworkSession)
1717

18-
/// The supplier function responsible for providing the network session when needed.
1918
private var supplier: FutureSessionSupplier
20-
21-
/// The cached instance of the network session, used for subsequent requests after it is first resolved.
2219
private var sessionCache: NetworkSession?
2320

2421
/// Provides access to the cached network session.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// TransientSession.swift
3+
// GoodNetworking
4+
//
5+
// Created by Filip Šašala on 23/10/2024.
6+
//
7+
8+
/// A network session that is resolved asynchronously when required, without caching.
9+
///
10+
/// This is useful when you need to resolve a session asynchronously depending on
11+
/// external circumstances, such as whether an authentication token is available.
12+
///
13+
/// `TransientSession` acts as a wrapper around `NetworkSession`, providing
14+
/// a mechanism for deferring the resolution of a session.
15+
///
16+
/// The session is supplied asynchronously via a `TransientSessionSupplier`.
17+
public struct TransientSession {
18+
19+
/// Function that will resolve a `NetworkSession` asynchronously when required.
20+
public typealias TransientSessionSupplier = (@Sendable () async -> NetworkSession)
21+
22+
private let supplier: TransientSessionSupplier
23+
24+
/// Creates `TransientSession` with a supplier for network session resolution.
25+
///
26+
/// - Parameter supplier: A function that will provide a `NetworkSession`.
27+
public init(_ supplier: @escaping TransientSessionSupplier) {
28+
self.supplier = supplier
29+
}
30+
31+
/// Resolves an appropriate session by calling the supplier.
32+
/// - Returns: Network session resolved and returned from the supplier.
33+
public func resolve() async -> NetworkSession {
34+
return await supplier()
35+
}
36+
37+
/// Sugared resolution function. See ``resolve``.
38+
public func callAsFunction() async -> NetworkSession {
39+
return await resolve()
40+
}
41+
42+
}

0 commit comments

Comments
 (0)