FSTOnlineStateTracker.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "Firestore/Source/Core/FSTTypes.h"
  18. @class FSTDispatchQueue;
  19. @protocol FSTOnlineStateDelegate;
  20. NS_ASSUME_NONNULL_BEGIN
  21. /**
  22. * A component used by the FSTRemoteStore to track the FSTOnlineState (that is, whether or not the
  23. * client as a whole should be considered to be online or offline), implementing the appropriate
  24. * heuristics.
  25. *
  26. * In particular, when the client is trying to connect to the backend, we allow up to
  27. * kMaxWatchStreamFailures within kOnlineStateTimeout for a connection to succeed. If we have too
  28. * many failures or the timeout elapses, then we set the FSTOnlineState to Offline, and
  29. * the client will behave as if it is offline (getDocument() calls will return cached data, etc.).
  30. */
  31. @interface FSTOnlineStateTracker : NSObject
  32. - (instancetype)initWithWorkerDispatchQueue:(FSTDispatchQueue *)queue;
  33. - (instancetype)init NS_UNAVAILABLE;
  34. /** A delegate to be notified on FSTOnlineState changes. */
  35. @property(nonatomic, weak) id<FSTOnlineStateDelegate> onlineStateDelegate;
  36. /**
  37. * Called by FSTRemoteStore when a watch stream is started (including on each backoff attempt).
  38. *
  39. * If this is the first attempt, it sets the FSTOnlineState to Unknown and starts the
  40. * onlineStateTimer.
  41. */
  42. - (void)handleWatchStreamStart;
  43. /**
  44. * Called by FSTRemoteStore when a watch stream fails.
  45. *
  46. * Updates our FSTOnlineState as appropriate. The first failure moves us to FSTOnlineStateUnknown.
  47. * We then may allow multiple failures (based on kMaxWatchStreamFailures) before we actually
  48. * transition to FSTOnlineStateOffline.
  49. */
  50. - (void)handleWatchStreamFailure;
  51. /**
  52. * Explicitly sets the FSTOnlineState to the specified state.
  53. *
  54. * Note that this resets the timers / failure counters, etc. used by our Offline heuristics, so
  55. * it must not be used in place of handleWatchStreamStart and handleWatchStreamFailure.
  56. */
  57. - (void)updateState:(FSTOnlineState)newState;
  58. @end
  59. NS_ASSUME_NONNULL_END