FirebaseObserver.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. namespace SimpleFirebaseUnity
  5. {
  6. public class FirebaseObserver {
  7. public Action<FirebaseDatabase, DataSnapshot> OnChange;
  8. protected FirebaseDatabase firebase;
  9. protected FirebaseDatabase target;
  10. protected float refreshRate;
  11. protected string getParam;
  12. protected bool active;
  13. protected bool firstTime;
  14. protected DataSnapshot lastSnapshot;
  15. protected IEnumerator routine;
  16. #region CONSTRUCTORS
  17. /// <summary>
  18. /// Creates an Observer that calls GetValue request at the given refresh rate (in seconds) and checks whether the value has changed.
  19. /// </summary>
  20. /// <param name="firebase">Firebase.</param>
  21. /// <param name="refreshRate">Refresh rate (in seconds).</param>
  22. /// <param name="getParam">Parameter value for the Get request that will be called periodically.</param>
  23. public FirebaseObserver(FirebaseDatabase _firebase, float _refreshRate, string _getParam = "")
  24. {
  25. active = false;
  26. lastSnapshot = null;
  27. firebase = _firebase;
  28. refreshRate = _refreshRate;
  29. getParam = _getParam;
  30. target = _firebase.Copy ();
  31. routine = null;
  32. }
  33. /// <summary>
  34. /// Creates an Observer that calls GetValue request at the given refresh rate (in seconds) and checks whether the value has changed.
  35. /// </summary>
  36. /// <param name="firebase">Firebase.</param>
  37. /// <param name="refreshRate">Refresh rate (in seconds).</param>
  38. /// <param name="getParam">Parameter value for the Get request that will be called periodically.</param>
  39. public FirebaseObserver(FirebaseDatabase _firebase, float _refreshRate, FirebaseParam _getParam)
  40. {
  41. active = false;
  42. lastSnapshot = null;
  43. firebase = _firebase;
  44. refreshRate = _refreshRate;
  45. getParam = _getParam.Parameter;
  46. target = _firebase.Copy ();
  47. }
  48. #endregion
  49. #region OBSERVER FUNCTIONS
  50. /// <summary>
  51. /// Start the observer.
  52. /// </summary>
  53. public void Start()
  54. {
  55. if (routine != null)
  56. Stop ();
  57. active = true;
  58. firstTime = true;
  59. target.OnGetSuccess += CompareSnapshot;
  60. routine = RefreshCoroutine ();
  61. target.root.StartCoroutine (routine);
  62. }
  63. /// <summary>
  64. /// Stop the observer.
  65. /// </summary>
  66. public void Stop()
  67. {
  68. active = false;
  69. target.OnGetSuccess -= CompareSnapshot;
  70. lastSnapshot = null;
  71. if (routine != null) {
  72. target.root.StopCoroutine (routine);
  73. routine = null;
  74. }
  75. }
  76. IEnumerator RefreshCoroutine()
  77. {
  78. while (active) {
  79. target.GetValue ();
  80. yield return new WaitForSeconds (refreshRate);
  81. }
  82. }
  83. void CompareSnapshot(FirebaseDatabase target, DataSnapshot snapshot)
  84. {
  85. if (firstTime) {
  86. firstTime = false;
  87. lastSnapshot = snapshot;
  88. return;
  89. }
  90. if (lastSnapshot != null)
  91. {
  92. if (!snapshot.RawJson.Equals (lastSnapshot.RawJson)) {
  93. if (OnChange != null)
  94. OnChange (firebase, snapshot);
  95. }
  96. }
  97. lastSnapshot = snapshot;
  98. }
  99. #endregion
  100. }
  101. }