123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using UnityEngine;
- using System.Collections;
- using System;
- namespace SimpleFirebaseUnity
- {
- public class FirebaseObserver {
- public Action<FirebaseDatabase, DataSnapshot> OnChange;
- protected FirebaseDatabase firebase;
- protected FirebaseDatabase target;
- protected float refreshRate;
- protected string getParam;
- protected bool active;
- protected bool firstTime;
- protected DataSnapshot lastSnapshot;
- protected IEnumerator routine;
- #region CONSTRUCTORS
- /// <summary>
- /// Creates an Observer that calls GetValue request at the given refresh rate (in seconds) and checks whether the value has changed.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="refreshRate">Refresh rate (in seconds).</param>
- /// <param name="getParam">Parameter value for the Get request that will be called periodically.</param>
- public FirebaseObserver(FirebaseDatabase _firebase, float _refreshRate, string _getParam = "")
- {
- active = false;
- lastSnapshot = null;
- firebase = _firebase;
- refreshRate = _refreshRate;
- getParam = _getParam;
- target = _firebase.Copy ();
- routine = null;
- }
- /// <summary>
- /// Creates an Observer that calls GetValue request at the given refresh rate (in seconds) and checks whether the value has changed.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="refreshRate">Refresh rate (in seconds).</param>
- /// <param name="getParam">Parameter value for the Get request that will be called periodically.</param>
- public FirebaseObserver(FirebaseDatabase _firebase, float _refreshRate, FirebaseParam _getParam)
- {
- active = false;
- lastSnapshot = null;
- firebase = _firebase;
- refreshRate = _refreshRate;
- getParam = _getParam.Parameter;
- target = _firebase.Copy ();
- }
- #endregion
- #region OBSERVER FUNCTIONS
- /// <summary>
- /// Start the observer.
- /// </summary>
- public void Start()
- {
- if (routine != null)
- Stop ();
-
- active = true;
- firstTime = true;
- target.OnGetSuccess += CompareSnapshot;
- routine = RefreshCoroutine ();
- target.root.StartCoroutine (routine);
- }
- /// <summary>
- /// Stop the observer.
- /// </summary>
- public void Stop()
- {
- active = false;
- target.OnGetSuccess -= CompareSnapshot;
- lastSnapshot = null;
- if (routine != null) {
- target.root.StopCoroutine (routine);
- routine = null;
- }
-
- }
- IEnumerator RefreshCoroutine()
- {
- while (active) {
- target.GetValue ();
- yield return new WaitForSeconds (refreshRate);
- }
- }
- void CompareSnapshot(FirebaseDatabase target, DataSnapshot snapshot)
- {
- if (firstTime) {
- firstTime = false;
- lastSnapshot = snapshot;
- return;
- }
- if (lastSnapshot != null)
- {
- if (!snapshot.RawJson.Equals (lastSnapshot.RawJson)) {
- if (OnChange != null)
- OnChange (firebase, snapshot);
- }
- }
- lastSnapshot = snapshot;
- }
- #endregion
- }
- }
|