_ObjC_HeartbeatController.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. /// An object that provides API to log and flush heartbeats from a synchronized storage container.
  16. @objc(FIRHeartbeatController)
  17. @objcMembers
  18. public class _ObjC_HeartbeatController: NSObject {
  19. /// The underlying Swift object.
  20. private let heartbeatController: HeartbeatController
  21. /// Public initializer.
  22. /// - Parameter id: The `id` to associate this controller's heartbeat storage with.
  23. public init(id: String) {
  24. heartbeatController = HeartbeatController(id: id)
  25. }
  26. /// Asynchronously logs a new heartbeat, if needed.
  27. ///
  28. /// - Note: This API is thread-safe.
  29. /// - Parameter agent: The string agent (i.e. Firebase User Agent) to associate the logged
  30. /// heartbeat with.
  31. public func log(_ agent: String) {
  32. heartbeatController.log(agent)
  33. }
  34. /// Synchronously flushes heartbeats from storage into a heartbeats payload.
  35. ///
  36. /// - Note: This API is thread-safe.
  37. /// - Returns: A heartbeats payload for the flushed heartbeat(s).
  38. public func flush() -> _ObjC_HeartbeatsPayload {
  39. let heartbeatsPayload = heartbeatController.flush()
  40. return _ObjC_HeartbeatsPayload(heartbeatsPayload)
  41. }
  42. /// Asynchronously flushes heartbeats from storage into a heartbeats payload.
  43. ///
  44. /// - Note: This API is thread-safe.
  45. /// - Returns: A heartbeats payload for the flushed heartbeat(s).
  46. public func flushAsync(completionHandler: @escaping @Sendable (_ObjC_HeartbeatsPayload) -> Void) {
  47. // TODO: When minimum version moves to iOS 13.0, restore the async version
  48. // removed in #13952.
  49. heartbeatController.flushAsync { heartbeatsPayload in
  50. completionHandler(_ObjC_HeartbeatsPayload(heartbeatsPayload))
  51. }
  52. }
  53. /// Synchronously flushes the heartbeat for today.
  54. ///
  55. /// If no heartbeat was logged today, the returned payload is empty.
  56. ///
  57. /// - Note: This API is thread-safe.
  58. /// - Returns: A heartbeats payload for the flushed heartbeat.
  59. public func flushHeartbeatFromToday() -> _ObjC_HeartbeatsPayload {
  60. let heartbeatsPayload = heartbeatController.flushHeartbeatFromToday()
  61. return _ObjC_HeartbeatsPayload(heartbeatsPayload)
  62. }
  63. }