_ObjC_HeartbeatController.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. @available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
  47. public func flushAsync() async -> _ObjC_HeartbeatsPayload {
  48. let heartbeatsPayload = await heartbeatController.flushAsync()
  49. return _ObjC_HeartbeatsPayload(heartbeatsPayload)
  50. }
  51. /// Synchronously flushes the heartbeat for today.
  52. ///
  53. /// If no heartbeat was logged today, the returned payload is empty.
  54. ///
  55. /// - Note: This API is thread-safe.
  56. /// - Returns: A heartbeats payload for the flushed heartbeat.
  57. public func flushHeartbeatFromToday() -> _ObjC_HeartbeatsPayload {
  58. let heartbeatsPayload = heartbeatController.flushHeartbeatFromToday()
  59. return _ObjC_HeartbeatsPayload(heartbeatsPayload)
  60. }
  61. }