-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Auth): Refactoring state machine logic to fix memory leak (#3613)
* fix(Auth): Fixing memory leaks happening because of the state machine * Update AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/hierarchical-state-machine-swift/StateMachine.swift Co-authored-by: Di Wu <[email protected]> * worked on review comment --------- Co-authored-by: Di Wu <[email protected]>
- Loading branch information
Showing
3 changed files
with
58 additions
and
84 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...nitoAuthPlugin/StateMachine/hierarchical-state-machine-swift/CancellableAsyncStream.swift
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,43 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Combine | ||
|
||
class CancellableAsyncStream<Element>: AsyncSequence { | ||
|
||
typealias AsyncIterator = AsyncStream<Element>.AsyncIterator | ||
private let asyncStream: AsyncStream<Element> | ||
private let cancellable: AnyCancellable? | ||
|
||
deinit { | ||
cancel() | ||
} | ||
|
||
init(asyncStream: AsyncStream<Element>, cancellable: AnyCancellable?) { | ||
self.asyncStream = asyncStream | ||
self.cancellable = cancellable | ||
} | ||
|
||
convenience init(with publisher: AnyPublisher<Element, Never>) { | ||
var cancellable: AnyCancellable? | ||
self.init(asyncStream: AsyncStream { continuation in | ||
cancellable = publisher.sink { _ in | ||
continuation.finish() | ||
} receiveValue: { | ||
continuation.yield($0) | ||
} | ||
}, cancellable: cancellable) | ||
} | ||
|
||
func makeAsyncIterator() -> AsyncStream<Element>.AsyncIterator { | ||
asyncStream.makeAsyncIterator() | ||
} | ||
|
||
func cancel() { | ||
cancellable?.cancel() | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
...SCognitoAuthPlugin/StateMachine/hierarchical-state-machine-swift/StateAsyncSequence.swift
This file was deleted.
Oops, something went wrong.
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