FSTOnlineStateTracker.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.
  38. *
  39. * It sets the FSTOnlineState to Unknown and starts the onlineStateTimer if necessary.
  40. */
  41. - (void)handleWatchStreamStart;
  42. /**
  43. * Called by FSTRemoteStore when a watch stream fails.
  44. *
  45. * Updates our FSTOnlineState as appropriate. The first failure moves us to FSTOnlineStateUnknown.
  46. * We then may allow multiple failures (based on kMaxWatchStreamFailures) before we actually
  47. * transition to FSTOnlineStateOffline.
  48. */
  49. - (void)handleWatchStreamFailure;
  50. /**
  51. * Explicitly sets the FSTOnlineState to the specified state.
  52. *
  53. * Note that this resets the timers / failure counters, etc. used by our Offline heuristics, so
  54. * it must not be used in place of handleWatchStreamStart and handleWatchStreamFailure.
  55. */
  56. - (void)updateState:(FSTOnlineState)newState;
  57. @end
  58. NS_ASSUME_NONNULL_END