_ObjC_HeartbeatController.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 heartbeat with.
  30. public func log(_ agent: String) {
  31. heartbeatController.log(agent)
  32. }
  33. /// Synchronously flushes heartbeats from storage into a heartbeats payload.
  34. ///
  35. /// - Note: This API is thread-safe.
  36. /// - Returns: A heartbeats payload for the flushed heartbeat(s).
  37. public func flush() -> _ObjC_HeartbeatsPayload {
  38. let heartbeatsPayload = heartbeatController.flush()
  39. return _ObjC_HeartbeatsPayload(heartbeatsPayload)
  40. }
  41. /// Synchronously flushes the heartbeat for today.
  42. ///
  43. /// If no heartbeat was logged today, the returned payload is empty.
  44. ///
  45. /// - Note: This API is thread-safe.
  46. /// - Returns: A heartbeats payload for the flushed heartbeat.
  47. public func flushHeartbeatFromToday() -> _ObjC_HeartbeatsPayload {
  48. let heartbeatsPayload = heartbeatController.flushHeartbeatFromToday()
  49. return _ObjC_HeartbeatsPayload(heartbeatsPayload)
  50. }
  51. }