-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add AuthStateChangeListenerRegistration type (#248)
- Loading branch information
Showing
4 changed files
with
88 additions
and
21 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
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,41 @@ | ||
// | ||
// AuthStateChangeListener.swift | ||
// | ||
// | ||
// Created by Guilherme Souza on 17/02/24. | ||
// | ||
|
||
import ConcurrencyExtras | ||
import Foundation | ||
|
||
/// A listener that can be removed by calling ``AuthStateChangeListenerRegistration/remove()``. | ||
/// | ||
/// - Note: Listener is automatically removed on deinit. | ||
public protocol AuthStateChangeListenerRegistration: Sendable, AnyObject { | ||
/// Removes the listener. After the initial call, subsequent calls have no effect. | ||
func remove() | ||
} | ||
|
||
final class AuthStateChangeListenerHandle: AuthStateChangeListenerRegistration { | ||
let _onRemove = LockIsolated((@Sendable () -> Void)?.none) | ||
|
||
public func remove() { | ||
_onRemove.withValue { | ||
if $0 == nil { | ||
return | ||
} | ||
|
||
$0?() | ||
$0 = nil | ||
} | ||
} | ||
|
||
deinit { | ||
remove() | ||
} | ||
} | ||
|
||
public typealias AuthStateChangeListener = @Sendable ( | ||
_ event: AuthChangeEvent, | ||
_ session: Session? | ||
) -> Void |
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,44 @@ | ||
// | ||
// AuthStateChangeListenerHandleTests.swift | ||
// | ||
// | ||
// Created by Guilherme Souza on 17/02/24. | ||
// | ||
|
||
@testable import Auth | ||
import ConcurrencyExtras | ||
import Foundation | ||
import XCTest | ||
|
||
final class AuthStateChangeListenerHandleTests: XCTestCase { | ||
func testRemove() { | ||
let handle = AuthStateChangeListenerHandle() | ||
|
||
let onRemoveCallCount = LockIsolated(0) | ||
handle._onRemove.setValue { | ||
onRemoveCallCount.withValue { | ||
$0 += 1 | ||
} | ||
} | ||
|
||
handle.remove() | ||
handle.remove() | ||
|
||
XCTAssertEqual(onRemoveCallCount.value, 1) | ||
} | ||
|
||
func testDeinit() { | ||
var handle: AuthStateChangeListenerHandle? = AuthStateChangeListenerHandle() | ||
|
||
let onRemoveCallCount = LockIsolated(0) | ||
handle?._onRemove.setValue { | ||
onRemoveCallCount.withValue { | ||
$0 += 1 | ||
} | ||
} | ||
|
||
handle = nil | ||
|
||
XCTAssertEqual(onRemoveCallCount.value, 1) | ||
} | ||
} |