123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- using System;
- namespace SimpleFirebaseUnity
- {
- using MiniJSON;
- public class FirebaseQueue {
- #region FIREBASE COMMAND QUEUE
- const string SERVER_VALUE_TIMESTAMP = "{\".sv\": \"timestamp\"}";
- protected enum FirebaseCommand
- {
- Get = 0,
- Set = 1,
- Update = 2,
- Push = 3,
- Delete = 4
- }
- protected class CommandLinkedList {
- public FirebaseDatabase firebase;
- FirebaseCommand command;
- string param;
- object obj;
- public CommandLinkedList next;
- public CommandLinkedList(FirebaseDatabase _firebase, FirebaseCommand _command, string _param, object _obj = null){
- firebase = _firebase;
- command = _command;
- param = _param;
- obj = _obj;
- next = null;
- }
- public CommandLinkedList(FirebaseDatabase _firebase, FirebaseCommand _command, FirebaseParam firebaseParam, object _obj = null){
- firebase = _firebase;
- command = _command;
- param = firebaseParam.Parameter;
- obj = _obj;
- next = null;
- }
- public void AddNext(CommandLinkedList _next){
- next = _next;
- }
- public void DoCommand()
- {
- switch (command) {
- case FirebaseCommand.Get:
- firebase.GetValue (param);
- break;
- case FirebaseCommand.Set:
- firebase.SetValue (obj, param);
- break;
- case FirebaseCommand.Update:
- firebase.UpdateValue (obj, param);
- break;
- case FirebaseCommand.Push:
- firebase.Push (obj, param);
- break;
- case FirebaseCommand.Delete:
- firebase.Delete (param);
- break;
- }
- }
- }
- public Action OnQueueFinished;
- protected CommandLinkedList head;
- protected CommandLinkedList tail;
- protected bool autoStart;
- protected int count;
- protected void AddQueue(FirebaseDatabase firebase, FirebaseCommand command, string param, object obj = null)
- {
- CommandLinkedList commandNode = new CommandLinkedList (firebase, command, param, obj);
- if (head == null) {
- head = commandNode;
- tail = commandNode;
- if (autoStart)
- head.DoCommand();
- } else {
- tail.next = commandNode;
- tail = commandNode;
- }
- ++count;
- }
- protected void ClearQueueTopDown(CommandLinkedList node)
- {
- CommandLinkedList temp = node.next;
- node.next = null;
- ClearQueueTopDown (temp);
- }
- protected void StartNextCommand(){
- head = head.next;
- if (head != null)
- head.DoCommand ();
- else {
- tail = null;
- if (OnQueueFinished != null)
- OnQueueFinished ();
- }
- }
- protected void OnSuccess(FirebaseDatabase sender, DataSnapshot snapshot)
- {
- --count;
- StartNextCommand ();
- ClearCallbacks (sender);
- }
- protected void OnFailed(FirebaseDatabase sender, FirebaseError err)
- {
- --count;
- StartNextCommand ();
- ClearCallbacks (sender);
- }
- protected void ClearCallbacks(FirebaseDatabase sender)
- {
- sender.OnGetSuccess -= OnSuccess;
- sender.OnGetFailed -= OnFailed;
- sender.OnUpdateSuccess -= OnSuccess;
- sender.OnUpdateFailed -= OnFailed;
- sender.OnPushSuccess -= OnSuccess;
- sender.OnPushFailed -= OnFailed;
- sender.OnDeleteSuccess -= OnSuccess;
- sender.OnDeleteFailed -= OnFailed;
- }
- #endregion
- #region PUBLIC FUNCTIONS
- /// <summary>
- /// Initializes a new instance of the <see cref="SimpleFirebaseUnity.FirebaseQueueManager"/> class.
- /// </summary>
- /// <param name="_autoStart">If set to <c>true</c> auto start when a queue added.</param>
- public FirebaseQueue(bool _autoStart = true)
- {
- autoStart = _autoStart;
- count = 0;
- }
- /// <summary>
- /// Gets the number of request in queue.
- /// </summary>
- /// <value>The count.</value>
- public int Count
- {
- get {
- return count;
- }
- }
- /// <summary>
- /// Determines whether the queue is empty.
- /// </summary>
- /// <returns><c>true</c> if this instance is empty; otherwise, <c>false</c>.</returns>
- public bool IsEmpty()
- {
- return (count == 0);
- }
- /// <summary>
- /// Adds Firebase Get request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueGet(FirebaseDatabase firebase, string param = "")
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnGetSuccess += OnSuccess;
- temp.OnGetFailed += OnFailed;
- AddQueue (temp, FirebaseCommand.Get, param);
- }
- /// <summary>
- /// Adds Firebase Get request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueGet(FirebaseDatabase firebase, FirebaseParam param)
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnGetSuccess += OnSuccess;
- temp.OnGetFailed += OnFailed;
- AddQueue (temp, FirebaseCommand.Get, param.Parameter);
- }
- /// <summary>
- /// Adds Firebase Set request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="val">Value.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueSet(FirebaseDatabase firebase, object val, string param = "")
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnSetSuccess += OnSuccess;
- temp.OnSetFailed += OnFailed;
- AddQueue (temp, FirebaseCommand.Set, param, val);
- }
- /// <summary>
- /// Adds Firebase Set request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="val">Value.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueSet(FirebaseDatabase firebase, object val, FirebaseParam param)
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnSetSuccess += OnSuccess;
- temp.OnSetFailed += OnFailed;
- AddQueue (temp, FirebaseCommand.Set, param.Parameter, val);
- }
- /// <summary>
- /// Adds Firebase Set request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="json">Json.</param>
- /// <param name="isJson">If set to <c>true</c> is json.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueSet(FirebaseDatabase firebase, string json, bool isJson, string param = "")
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnSetSuccess += OnSuccess;
- temp.OnSetFailed += OnFailed;
- if (!isJson)
- AddQueue (temp, FirebaseCommand.Set, param, json);
- else
- AddQueue (temp, FirebaseCommand.Set, param, Json.Deserialize(json));
- }
- /// <summary>
- /// Adds Firebase Set request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="json">Json.</param>
- /// <param name="isJson">If set to <c>true</c> is json.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueSet(FirebaseDatabase firebase, string json, bool isJson, FirebaseParam param)
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnSetSuccess += OnSuccess;
- temp.OnSetFailed += OnFailed;
- if (!isJson)
- AddQueue (temp, FirebaseCommand.Set, param.Parameter, json);
- else
- AddQueue (temp, FirebaseCommand.Set, param.Parameter, Json.Deserialize(json));
- }
- /// <summary>
- /// Adds Firebase Update request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="val">Value.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueUpdate(FirebaseDatabase firebase, object val, string param = "")
- {
- firebase.OnUpdateSuccess += OnSuccess;
- firebase.OnUpdateFailed += OnFailed;
- AddQueue (firebase, FirebaseCommand.Update, param, val);
- }
- /// <summary>
- /// Adds Firebase Update request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="val">Value.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueUpdate(FirebaseDatabase firebase, object val, FirebaseParam param)
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnUpdateSuccess += OnSuccess;
- temp.OnUpdateFailed += OnFailed;
- AddQueue (temp, FirebaseCommand.Update, param.Parameter, val);
- }
- /// <summary>
- /// Adds Firebase Update request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="json">Json.</param>
- /// <param name="isJson">If set to <c>true</c> is json.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueUpdate(FirebaseDatabase firebase, string json, bool isJson, string param = "")
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnUpdateSuccess += OnSuccess;
- temp.OnUpdateFailed += OnFailed;
- if (!isJson)
- AddQueue (temp, FirebaseCommand.Update, param, json);
- else
- AddQueue (temp, FirebaseCommand.Update, param, Json.Deserialize(json));
- }
- /// <summary>
- /// Adds Firebase Update request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="json">Json.</param>
- /// <param name="isJson">If set to <c>true</c> is json.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueUpdate(FirebaseDatabase firebase, string json, bool isJson, FirebaseParam param)
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnUpdateSuccess += OnSuccess;
- temp.OnUpdateFailed += OnFailed;
- if (!isJson)
- AddQueue (temp, FirebaseCommand.Update, param.Parameter, json);
- else
- AddQueue (temp, FirebaseCommand.Update, param.Parameter, Json.Deserialize(json));
- }
- /// <summary>
- /// Adds Firebase Push request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="val">Value.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueuePush(FirebaseDatabase firebase, object val, string param = "")
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnPushSuccess += OnSuccess;
- temp.OnPushFailed += OnFailed;
- AddQueue (temp, FirebaseCommand.Push, param, val);
- }
- /// <summary>
- /// Adds Firebase Push request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="val">Value.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueuePush(FirebaseDatabase firebase, object val, FirebaseParam param)
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnPushSuccess += OnSuccess;
- temp.OnPushFailed += OnFailed;
- AddQueue (temp, FirebaseCommand.Push, param.Parameter, val);
- }
- /// <summary>
- /// Adds Firebase Push request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="json">Json.</param>
- /// <param name="isJson">If set to <c>true</c> is json.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueuePush(FirebaseDatabase firebase, string json, bool isJson, string param = "")
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnPushSuccess += OnSuccess;
- temp.OnPushFailed += OnFailed;
- if (!isJson)
- AddQueue (temp, FirebaseCommand.Push, param, json);
- else
- AddQueue (temp, FirebaseCommand.Push, param, Json.Deserialize(json));
- }
- /// <summary>
- /// Adds Firebase Push request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="json">Json.</param>
- /// <param name="isJson">If set to <c>true</c> is json.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueuePush(FirebaseDatabase firebase, string json, bool isJson, FirebaseParam param)
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnPushSuccess += OnSuccess;
- temp.OnPushFailed += OnFailed;
- if (!isJson)
- AddQueue (temp, FirebaseCommand.Push, param.Parameter, json);
- else
- AddQueue (temp, FirebaseCommand.Push, param.Parameter, Json.Deserialize(json));
- }
- /// <summary>
- /// Adds Firebase Delete request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueDelete(FirebaseDatabase firebase, string param = "")
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnDeleteSuccess += OnSuccess;
- temp.OnDeleteFailed += OnFailed;
- AddQueue (temp, FirebaseCommand.Delete, param);
- }
- /// <summary>
- /// Adds Firebase Delete request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="param">Parameter.</param>
- public void AddQueueDelete(FirebaseDatabase firebase, FirebaseParam param)
- {
- FirebaseDatabase temp = firebase.Copy (true);
- temp.OnDeleteSuccess += OnSuccess;
- temp.OnDeleteFailed += OnFailed;
- AddQueue (temp, FirebaseCommand.Delete, param.Parameter);
- }
- /// <summary>
- /// Adds Firebase Set Time Stamp request to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="keyName">Time stamp key name.</param>
- public void AddQueueSetTimeStamp(FirebaseDatabase firebase, string keyName)
- {
- FirebaseDatabase temp = firebase.Child (keyName, false);
- AddQueueSet (temp, SERVER_VALUE_TIMESTAMP, true, "print=silent");
- }
- /// <summary>
- /// Adds Firebase Set Time Stamp request with callback to queue.
- /// </summary>
- /// <param name="firebase">Firebase.</param>
- /// <param name="keyName">Key name.</param>
- /// <param name="_OnSuccess">On success callback.</param>
- /// <param name="_OnFailed">On fail callback.</param>
- public void AddQueueSetTimeStamp(FirebaseDatabase firebase, string keyName, Action<FirebaseDatabase, DataSnapshot> _OnSuccess, Action<FirebaseDatabase, FirebaseError> _OnFailed)
- {
- FirebaseDatabase temp = firebase.Child (keyName);
- temp.OnSetSuccess += _OnSuccess;
- temp.OnSetFailed += _OnFailed;
- AddQueueSet (temp, SERVER_VALUE_TIMESTAMP, true);
- }
- /// <summary>
- /// Force stop the command queue chain and clears the queue.
- /// </summary>
- public void ForceClearQueue()
- {
- ClearQueueTopDown (head);
- }
- #endregion
- }
- }
|