Skip to content

feat: add AuthStateChangeListenerRegistration type #248

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

Merged
merged 1 commit into from
Mar 8, 2024
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
22 changes: 2 additions & 20 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@ import Foundation
import FoundationNetworking
#endif

public final class AuthStateChangeListenerHandle {
var onCancel: (@Sendable () -> Void)?

public func cancel() {
onCancel?()
onCancel = nil
}

deinit {
cancel()
}
}

public typealias AuthStateChangeListener = @Sendable (
_ event: AuthChangeEvent,
_ session: Session?
) -> Void

public actor AuthClient {
/// FetchHandler is a type alias for asynchronous network request handling.
public typealias FetchHandler = @Sendable (
Expand Down Expand Up @@ -216,7 +198,7 @@ public actor AuthClient {
@discardableResult
public func onAuthStateChange(
_ listener: @escaping AuthStateChangeListener
) async -> AuthStateChangeListenerHandle {
) async -> AuthStateChangeListenerRegistration {
let handle = eventEmitter.attachListener(listener)
await emitInitialSession(forHandle: handle)
return handle
Expand All @@ -240,7 +222,7 @@ public actor AuthClient {
}

continuation.onTermination = { _ in
handle.cancel()
handle.remove()
}
}

Expand Down
41 changes: 41 additions & 0 deletions Sources/Auth/AuthStateChangeListener.swift
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
2 changes: 1 addition & 1 deletion Sources/Auth/Internal/EventEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class DefaultEventEmitter: EventEmitter {
let handle = AuthStateChangeListenerHandle()
let key = ObjectIdentifier(handle)

handle.onCancel = { [weak self] in
handle._onRemove.setValue { [weak self] in
self?.listeners.withValue {
$0[key] = nil
}
Expand Down
44 changes: 44 additions & 0 deletions Tests/AuthTests/AuthStateChangeListenerHandleTests.swift
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)
}
}