-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUM-5248 feat: setup ContextReceiver which can observe to single prop…
…erty in context message
- Loading branch information
Showing
16 changed files
with
346 additions
and
19 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
51 changes: 51 additions & 0 deletions
51
DatadogCore/Sources/Core/Context/MemoryWarningPublisher.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,51 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
import DatadogInternal | ||
import UIKit | ||
|
||
/// Tracks the memory warnings history and publishes it to the subscribers. | ||
internal final class MemoryWarningPublisher: ContextValuePublisher { | ||
private let queue: DispatchQueue | ||
var initialValue: MemoryWarningsHistory | ||
private var receiver: ContextValueReceiver<MemoryWarningsHistory>? | ||
private let notificationCenter: NotificationCenter | ||
private var history: MemoryWarningsHistory | ||
|
||
private static let defaultQueue = DispatchQueue( | ||
label: "com.datadoghq.memory-warning-publisher", | ||
target: .global(qos: .utility) | ||
) | ||
|
||
init( | ||
notificationCenter: NotificationCenter = .default, | ||
queue: DispatchQueue = MemoryWarningPublisher.defaultQueue, | ||
initialValue: MemoryWarningsHistory = .init() | ||
) { | ||
self.notificationCenter = notificationCenter | ||
self.queue = queue | ||
self.initialValue = initialValue | ||
self.history = initialValue | ||
} | ||
|
||
func publish(to receiver: @escaping ContextValueReceiver<MemoryWarningsHistory>) { | ||
queue.async { self.receiver = receiver } | ||
notificationCenter.addObserver(self, selector: #selector(didReceiveMemoryWarning), name: UIApplication.didReceiveMemoryWarningNotification, object: nil) | ||
} | ||
|
||
@objc | ||
func didReceiveMemoryWarning() { | ||
queue.async { | ||
self.history.append(warning: .init()) | ||
self.receiver?(self.history) | ||
} | ||
} | ||
|
||
func cancel() { | ||
notificationCenter.removeObserver(self) | ||
} | ||
} |
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
Oops, something went wrong.