- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
feat: Session Replay App Life Cycle #45
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
                    Changes from 8 commits
      Commits
    
    
            Show all changes
          
          
            12 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3d1100a
              
                remove import
              
              
                abelonogov-ld f48d4f1
              
                Merge branch 'andrey/sr-cleanup' into andrey/app-lyfecycle
              
              
                abelonogov-ld c058eea
              
                feat: SnapshotTaker pausing
              
              
                abelonogov-ld 0022e80
              
                initial version applifecycler logger
              
              
                abelonogov-ld 20b21db
              
                Merge branch 'main' into andrey/app-lyfecycle-logger
              
              
                abelonogov-ld 723f7c4
              
                Merge branch 'main' into andrey/app-lyfecycle-logger
              
              
                abelonogov-ld 7401b60
              
                Merge branch 'main' into andrey/app-lyfecycle-logger
              
              
                abelonogov-ld 0685d67
              
                delete logging stuff
              
              
                abelonogov-ld 1c39c74
              
                Fix: memory leak
              
              
                abelonogov-ld 2ddcfca
              
                Adress coments
              
              
                abelonogov-ld 49146f1
              
                fix unit tests
              
              
                abelonogov-ld 388fb96
              
                Merge branch 'main' into andrey/sr-app-lifecycle
              
              
                abelonogov-ld File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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,57 @@ | ||
| import Foundation | ||
|  | ||
| public actor Broadcaster<Value: Sendable> { | ||
| private var continuations = [Int: AsyncStream<Value>.Continuation]() | ||
| private var nextIdentifier = 0 | ||
| private var finished = false | ||
|  | ||
| public init() { } | ||
|  | ||
| public func stream( | ||
| bufferingPolicy: AsyncStream<Value>.Continuation.BufferingPolicy = .unbounded | ||
| ) -> AsyncStream<Value> { | ||
| let id = nextIdentifier | ||
| nextIdentifier &+= 1 | ||
| var continuationRef: AsyncStream<Value>.Continuation? | ||
|  | ||
| let stream = AsyncStream<Value>(bufferingPolicy: bufferingPolicy) { continuation in | ||
| continuationRef = continuation | ||
| continuation.onTermination = { [weak self] _ in | ||
| guard let self else { return } | ||
| Task { await self.removeContinuation(id: id) } | ||
| } | ||
| } | ||
|  | ||
| if let continuation = continuationRef { | ||
| if finished { | ||
| continuation.finish() | ||
| } else { | ||
| continuations[id] = continuation | ||
| } | ||
| } | ||
|  | ||
| return stream | ||
| } | ||
|  | ||
| public func send(_ event: Value) { | ||
| guard !finished else { return } | ||
| for continuation in continuations.values { | ||
| _ = continuation.yield(event) | ||
| } | ||
| } | ||
|  | ||
| public func finish() { | ||
| guard !finished else { return } | ||
| finished = true | ||
| for continuation in continuations.values { | ||
| continuation.finish() | ||
| } | ||
| continuations.removeAll() | ||
| } | ||
|  | ||
| private func removeContinuation(id: Int) { | ||
| continuations.removeValue(forKey: id) | ||
| } | ||
| } | ||
|  | ||
|  | ||
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,86 @@ | ||
| import UIKit | ||
| import Common | ||
|  | ||
| public enum AppLifeCycleEvent { | ||
| case didFinishLaunching | ||
| case willEnterForeground | ||
| case didBecomeActive | ||
| case willResignActive | ||
| case didEnterBackground | ||
| case willTerminate | ||
| } | ||
|  | ||
| public protocol AppLifecycleManaging: AnyObject { | ||
| func events() async -> AsyncStream<AppLifeCycleEvent> | ||
| } | ||
|  | ||
| final class AppLifecycleManager: AppLifecycleManaging { | ||
| private let broadcaster = Broadcaster<AppLifeCycleEvent>() | ||
| private var observers = [NSObjectProtocol]() | ||
|  | ||
| init() { | ||
| observeLifecycleNotifications() | ||
| } | ||
|  | ||
| private func observeLifecycleNotifications() { | ||
| observers.append(NotificationCenter.default.addObserver(forName: UIApplication.didFinishLaunchingNotification, object: nil, queue: .main) { [weak self] _ in | ||
| self?.didFinishLaunching() | ||
| }) | ||
| observers.append(NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: .main) { [weak self] _ in | ||
| self?.handleDidBecomeActive() | ||
| }) | ||
| observers.append(NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: .main) { [weak self] _ in | ||
| self?.handleWillResignActive() | ||
| }) | ||
| observers.append(NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: .main) { [weak self] _ in | ||
| self?.handleDidEnterBackground() | ||
| }) | ||
| observers.append(NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: .main) { [weak self] _ in | ||
| self?.handleWillEnterForeground() | ||
| }) | ||
| observers.append(NotificationCenter.default.addObserver(forName: UIApplication.willTerminateNotification, object: nil, queue: .main) { [weak self] _ in | ||
| self?.handleWillTerminate() | ||
| }) | ||
| } | ||
|  | ||
| deinit { | ||
| observers.forEach { | ||
| NotificationCenter.default.removeObserver($0) | ||
| } | ||
| Task { | ||
| await broadcaster.finish() | ||
| } | ||
| } | ||
|  | ||
| func events() async -> AsyncStream<AppLifeCycleEvent> { | ||
| await broadcaster.stream() | ||
| } | ||
|  | ||
| private func send(_ event: AppLifeCycleEvent) { | ||
| Task { await broadcaster.send(event) } | ||
| } | ||
|  | ||
| private func didFinishLaunching() { | ||
| send(.didFinishLaunching) | ||
| } | ||
|  | ||
| private func handleDidBecomeActive() { | ||
| send(.didBecomeActive) | ||
| } | ||
|  | ||
| private func handleWillResignActive() { | ||
| send(.willResignActive) | ||
| } | ||
|  | ||
| private func handleDidEnterBackground() { | ||
| send(.didEnterBackground) | ||
| } | ||
|  | ||
| private func handleWillEnterForeground() { | ||
| send(.willEnterForeground) | ||
| } | ||
|  | ||
| private func handleWillTerminate() { | ||
| send(.willTerminate) | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.