Skip to content

Commit

Permalink
Merge pull request #140 from treastrain/event-stream-cancellation
Browse files Browse the repository at this point in the history
Support cancellation in NFCReaderSessions' eventStream
  • Loading branch information
treastrain authored Dec 22, 2024
2 parents 70b9419 + cd059bf commit dff358e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Sources/Core/NFCNDEFReaderSession+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ private final class NFCNDEFReaderSessionDelegateHandleObjectForMessage: NSObject
var continuation: AsyncStream<NFCNDEFReaderSession.MessageEvent>.Continuation?

func readerSessionDidBecomeActive(_ session: NFCNDEFReaderSession) {
let invalidator = NFCReaderSessionInvalidator(session)
continuation?.onTermination = { @Sendable in
switch $0 {
case .finished:
break
case .cancelled:
invalidator.invalidate(errorMessage: invalidator.alertMessage)
@unknown default:
break
}
}
continuation?.yield(.sessionBecomeActive)
}

Expand All @@ -117,6 +128,17 @@ private final class NFCNDEFReaderSessionDelegateHandleObjectForTag: NSObject, NF
var continuation: AsyncStream<NFCNDEFReaderSession.TagEvent>.Continuation?

func readerSessionDidBecomeActive(_ session: NFCNDEFReaderSession) {
let invalidator = NFCReaderSessionInvalidator(session)
continuation?.onTermination = { @Sendable in
switch $0 {
case .finished:
break
case .cancelled:
invalidator.invalidate(errorMessage: invalidator.alertMessage)
@unknown default:
break
}
}
continuation?.yield(.sessionBecomeActive)
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/Core/NFCReaderSessionInvalidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ package final class NFCReaderSessionInvalidator<Base: NFCReaderSessionProtocol>:
self.readerSession = readerSession
}

package var alertMessage: String {
readerSession?.alertMessage ?? ""
}

package func invalidate(errorMessage: String) {
readerSession?.invalidate(errorMessage: errorMessage)
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/Core/NFCTagReaderSession+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ private final class NFCTagReaderSessionDelegateHandleObject: NSObject, NFCTagRea
var continuation: AsyncStream<NFCTagReaderSession.Event>.Continuation?

func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {
let invalidator = NFCReaderSessionInvalidator(session)
continuation?.onTermination = { @Sendable in
switch $0 {
case .finished:
break
case .cancelled:
invalidator.invalidate(errorMessage: invalidator.alertMessage)
@unknown default:
break
}
}
continuation?.yield(.sessionBecomeActive)
}

Expand Down
11 changes: 11 additions & 0 deletions Sources/Core/NFCVASReaderSession+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ private final class NFCVASReaderSessionDelegateHandleObject: NSObject, NFCVASRea
var continuation: AsyncStream<NFCVASReaderSession.Event>.Continuation?

func readerSessionDidBecomeActive(_ session: NFCVASReaderSession) {
let invalidator = NFCReaderSessionInvalidator(session)
continuation?.onTermination = { @Sendable in
switch $0 {
case .finished:
break
case .cancelled:
invalidator.invalidate(errorMessage: invalidator.alertMessage)
@unknown default:
break
}
}
continuation?.yield(.sessionBecomeActive)
}

Expand Down

0 comments on commit dff358e

Please sign in to comment.