FirebaseQueue.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using System;
  2. namespace SimpleFirebaseUnity
  3. {
  4. using MiniJSON;
  5. public class FirebaseQueue {
  6. #region FIREBASE COMMAND QUEUE
  7. const string SERVER_VALUE_TIMESTAMP = "{\".sv\": \"timestamp\"}";
  8. protected enum FirebaseCommand
  9. {
  10. Get = 0,
  11. Set = 1,
  12. Update = 2,
  13. Push = 3,
  14. Delete = 4
  15. }
  16. protected class CommandLinkedList {
  17. public FirebaseDatabase firebase;
  18. FirebaseCommand command;
  19. string param;
  20. object obj;
  21. public CommandLinkedList next;
  22. public CommandLinkedList(FirebaseDatabase _firebase, FirebaseCommand _command, string _param, object _obj = null){
  23. firebase = _firebase;
  24. command = _command;
  25. param = _param;
  26. obj = _obj;
  27. next = null;
  28. }
  29. public CommandLinkedList(FirebaseDatabase _firebase, FirebaseCommand _command, FirebaseParam firebaseParam, object _obj = null){
  30. firebase = _firebase;
  31. command = _command;
  32. param = firebaseParam.Parameter;
  33. obj = _obj;
  34. next = null;
  35. }
  36. public void AddNext(CommandLinkedList _next){
  37. next = _next;
  38. }
  39. public void DoCommand()
  40. {
  41. switch (command) {
  42. case FirebaseCommand.Get:
  43. firebase.GetValue (param);
  44. break;
  45. case FirebaseCommand.Set:
  46. firebase.SetValue (obj, param);
  47. break;
  48. case FirebaseCommand.Update:
  49. firebase.UpdateValue (obj, param);
  50. break;
  51. case FirebaseCommand.Push:
  52. firebase.Push (obj, param);
  53. break;
  54. case FirebaseCommand.Delete:
  55. firebase.Delete (param);
  56. break;
  57. }
  58. }
  59. }
  60. public Action OnQueueFinished;
  61. protected CommandLinkedList head;
  62. protected CommandLinkedList tail;
  63. protected bool autoStart;
  64. protected int count;
  65. protected void AddQueue(FirebaseDatabase firebase, FirebaseCommand command, string param, object obj = null)
  66. {
  67. CommandLinkedList commandNode = new CommandLinkedList (firebase, command, param, obj);
  68. if (head == null) {
  69. head = commandNode;
  70. tail = commandNode;
  71. if (autoStart)
  72. head.DoCommand();
  73. } else {
  74. tail.next = commandNode;
  75. tail = commandNode;
  76. }
  77. ++count;
  78. }
  79. protected void ClearQueueTopDown(CommandLinkedList node)
  80. {
  81. CommandLinkedList temp = node.next;
  82. node.next = null;
  83. ClearQueueTopDown (temp);
  84. }
  85. protected void StartNextCommand(){
  86. head = head.next;
  87. if (head != null)
  88. head.DoCommand ();
  89. else {
  90. tail = null;
  91. if (OnQueueFinished != null)
  92. OnQueueFinished ();
  93. }
  94. }
  95. protected void OnSuccess(FirebaseDatabase sender, DataSnapshot snapshot)
  96. {
  97. --count;
  98. StartNextCommand ();
  99. ClearCallbacks (sender);
  100. }
  101. protected void OnFailed(FirebaseDatabase sender, FirebaseError err)
  102. {
  103. --count;
  104. StartNextCommand ();
  105. ClearCallbacks (sender);
  106. }
  107. protected void ClearCallbacks(FirebaseDatabase sender)
  108. {
  109. sender.OnGetSuccess -= OnSuccess;
  110. sender.OnGetFailed -= OnFailed;
  111. sender.OnUpdateSuccess -= OnSuccess;
  112. sender.OnUpdateFailed -= OnFailed;
  113. sender.OnPushSuccess -= OnSuccess;
  114. sender.OnPushFailed -= OnFailed;
  115. sender.OnDeleteSuccess -= OnSuccess;
  116. sender.OnDeleteFailed -= OnFailed;
  117. }
  118. #endregion
  119. #region PUBLIC FUNCTIONS
  120. /// <summary>
  121. /// Initializes a new instance of the <see cref="SimpleFirebaseUnity.FirebaseQueueManager"/> class.
  122. /// </summary>
  123. /// <param name="_autoStart">If set to <c>true</c> auto start when a queue added.</param>
  124. public FirebaseQueue(bool _autoStart = true)
  125. {
  126. autoStart = _autoStart;
  127. count = 0;
  128. }
  129. /// <summary>
  130. /// Gets the number of request in queue.
  131. /// </summary>
  132. /// <value>The count.</value>
  133. public int Count
  134. {
  135. get {
  136. return count;
  137. }
  138. }
  139. /// <summary>
  140. /// Determines whether the queue is empty.
  141. /// </summary>
  142. /// <returns><c>true</c> if this instance is empty; otherwise, <c>false</c>.</returns>
  143. public bool IsEmpty()
  144. {
  145. return (count == 0);
  146. }
  147. /// <summary>
  148. /// Adds Firebase Get request to queue.
  149. /// </summary>
  150. /// <param name="firebase">Firebase.</param>
  151. /// <param name="param">Parameter.</param>
  152. public void AddQueueGet(FirebaseDatabase firebase, string param = "")
  153. {
  154. FirebaseDatabase temp = firebase.Copy (true);
  155. temp.OnGetSuccess += OnSuccess;
  156. temp.OnGetFailed += OnFailed;
  157. AddQueue (temp, FirebaseCommand.Get, param);
  158. }
  159. /// <summary>
  160. /// Adds Firebase Get request to queue.
  161. /// </summary>
  162. /// <param name="firebase">Firebase.</param>
  163. /// <param name="param">Parameter.</param>
  164. public void AddQueueGet(FirebaseDatabase firebase, FirebaseParam param)
  165. {
  166. FirebaseDatabase temp = firebase.Copy (true);
  167. temp.OnGetSuccess += OnSuccess;
  168. temp.OnGetFailed += OnFailed;
  169. AddQueue (temp, FirebaseCommand.Get, param.Parameter);
  170. }
  171. /// <summary>
  172. /// Adds Firebase Set request to queue.
  173. /// </summary>
  174. /// <param name="firebase">Firebase.</param>
  175. /// <param name="val">Value.</param>
  176. /// <param name="param">Parameter.</param>
  177. public void AddQueueSet(FirebaseDatabase firebase, object val, string param = "")
  178. {
  179. FirebaseDatabase temp = firebase.Copy (true);
  180. temp.OnSetSuccess += OnSuccess;
  181. temp.OnSetFailed += OnFailed;
  182. AddQueue (temp, FirebaseCommand.Set, param, val);
  183. }
  184. /// <summary>
  185. /// Adds Firebase Set request to queue.
  186. /// </summary>
  187. /// <param name="firebase">Firebase.</param>
  188. /// <param name="val">Value.</param>
  189. /// <param name="param">Parameter.</param>
  190. public void AddQueueSet(FirebaseDatabase firebase, object val, FirebaseParam param)
  191. {
  192. FirebaseDatabase temp = firebase.Copy (true);
  193. temp.OnSetSuccess += OnSuccess;
  194. temp.OnSetFailed += OnFailed;
  195. AddQueue (temp, FirebaseCommand.Set, param.Parameter, val);
  196. }
  197. /// <summary>
  198. /// Adds Firebase Set request to queue.
  199. /// </summary>
  200. /// <param name="firebase">Firebase.</param>
  201. /// <param name="json">Json.</param>
  202. /// <param name="isJson">If set to <c>true</c> is json.</param>
  203. /// <param name="param">Parameter.</param>
  204. public void AddQueueSet(FirebaseDatabase firebase, string json, bool isJson, string param = "")
  205. {
  206. FirebaseDatabase temp = firebase.Copy (true);
  207. temp.OnSetSuccess += OnSuccess;
  208. temp.OnSetFailed += OnFailed;
  209. if (!isJson)
  210. AddQueue (temp, FirebaseCommand.Set, param, json);
  211. else
  212. AddQueue (temp, FirebaseCommand.Set, param, Json.Deserialize(json));
  213. }
  214. /// <summary>
  215. /// Adds Firebase Set request to queue.
  216. /// </summary>
  217. /// <param name="firebase">Firebase.</param>
  218. /// <param name="json">Json.</param>
  219. /// <param name="isJson">If set to <c>true</c> is json.</param>
  220. /// <param name="param">Parameter.</param>
  221. public void AddQueueSet(FirebaseDatabase firebase, string json, bool isJson, FirebaseParam param)
  222. {
  223. FirebaseDatabase temp = firebase.Copy (true);
  224. temp.OnSetSuccess += OnSuccess;
  225. temp.OnSetFailed += OnFailed;
  226. if (!isJson)
  227. AddQueue (temp, FirebaseCommand.Set, param.Parameter, json);
  228. else
  229. AddQueue (temp, FirebaseCommand.Set, param.Parameter, Json.Deserialize(json));
  230. }
  231. /// <summary>
  232. /// Adds Firebase Update request to queue.
  233. /// </summary>
  234. /// <param name="firebase">Firebase.</param>
  235. /// <param name="val">Value.</param>
  236. /// <param name="param">Parameter.</param>
  237. public void AddQueueUpdate(FirebaseDatabase firebase, object val, string param = "")
  238. {
  239. firebase.OnUpdateSuccess += OnSuccess;
  240. firebase.OnUpdateFailed += OnFailed;
  241. AddQueue (firebase, FirebaseCommand.Update, param, val);
  242. }
  243. /// <summary>
  244. /// Adds Firebase Update request to queue.
  245. /// </summary>
  246. /// <param name="firebase">Firebase.</param>
  247. /// <param name="val">Value.</param>
  248. /// <param name="param">Parameter.</param>
  249. public void AddQueueUpdate(FirebaseDatabase firebase, object val, FirebaseParam param)
  250. {
  251. FirebaseDatabase temp = firebase.Copy (true);
  252. temp.OnUpdateSuccess += OnSuccess;
  253. temp.OnUpdateFailed += OnFailed;
  254. AddQueue (temp, FirebaseCommand.Update, param.Parameter, val);
  255. }
  256. /// <summary>
  257. /// Adds Firebase Update request to queue.
  258. /// </summary>
  259. /// <param name="firebase">Firebase.</param>
  260. /// <param name="json">Json.</param>
  261. /// <param name="isJson">If set to <c>true</c> is json.</param>
  262. /// <param name="param">Parameter.</param>
  263. public void AddQueueUpdate(FirebaseDatabase firebase, string json, bool isJson, string param = "")
  264. {
  265. FirebaseDatabase temp = firebase.Copy (true);
  266. temp.OnUpdateSuccess += OnSuccess;
  267. temp.OnUpdateFailed += OnFailed;
  268. if (!isJson)
  269. AddQueue (temp, FirebaseCommand.Update, param, json);
  270. else
  271. AddQueue (temp, FirebaseCommand.Update, param, Json.Deserialize(json));
  272. }
  273. /// <summary>
  274. /// Adds Firebase Update request to queue.
  275. /// </summary>
  276. /// <param name="firebase">Firebase.</param>
  277. /// <param name="json">Json.</param>
  278. /// <param name="isJson">If set to <c>true</c> is json.</param>
  279. /// <param name="param">Parameter.</param>
  280. public void AddQueueUpdate(FirebaseDatabase firebase, string json, bool isJson, FirebaseParam param)
  281. {
  282. FirebaseDatabase temp = firebase.Copy (true);
  283. temp.OnUpdateSuccess += OnSuccess;
  284. temp.OnUpdateFailed += OnFailed;
  285. if (!isJson)
  286. AddQueue (temp, FirebaseCommand.Update, param.Parameter, json);
  287. else
  288. AddQueue (temp, FirebaseCommand.Update, param.Parameter, Json.Deserialize(json));
  289. }
  290. /// <summary>
  291. /// Adds Firebase Push request to queue.
  292. /// </summary>
  293. /// <param name="firebase">Firebase.</param>
  294. /// <param name="val">Value.</param>
  295. /// <param name="param">Parameter.</param>
  296. public void AddQueuePush(FirebaseDatabase firebase, object val, string param = "")
  297. {
  298. FirebaseDatabase temp = firebase.Copy (true);
  299. temp.OnPushSuccess += OnSuccess;
  300. temp.OnPushFailed += OnFailed;
  301. AddQueue (temp, FirebaseCommand.Push, param, val);
  302. }
  303. /// <summary>
  304. /// Adds Firebase Push request to queue.
  305. /// </summary>
  306. /// <param name="firebase">Firebase.</param>
  307. /// <param name="val">Value.</param>
  308. /// <param name="param">Parameter.</param>
  309. public void AddQueuePush(FirebaseDatabase firebase, object val, FirebaseParam param)
  310. {
  311. FirebaseDatabase temp = firebase.Copy (true);
  312. temp.OnPushSuccess += OnSuccess;
  313. temp.OnPushFailed += OnFailed;
  314. AddQueue (temp, FirebaseCommand.Push, param.Parameter, val);
  315. }
  316. /// <summary>
  317. /// Adds Firebase Push request to queue.
  318. /// </summary>
  319. /// <param name="firebase">Firebase.</param>
  320. /// <param name="json">Json.</param>
  321. /// <param name="isJson">If set to <c>true</c> is json.</param>
  322. /// <param name="param">Parameter.</param>
  323. public void AddQueuePush(FirebaseDatabase firebase, string json, bool isJson, string param = "")
  324. {
  325. FirebaseDatabase temp = firebase.Copy (true);
  326. temp.OnPushSuccess += OnSuccess;
  327. temp.OnPushFailed += OnFailed;
  328. if (!isJson)
  329. AddQueue (temp, FirebaseCommand.Push, param, json);
  330. else
  331. AddQueue (temp, FirebaseCommand.Push, param, Json.Deserialize(json));
  332. }
  333. /// <summary>
  334. /// Adds Firebase Push request to queue.
  335. /// </summary>
  336. /// <param name="firebase">Firebase.</param>
  337. /// <param name="json">Json.</param>
  338. /// <param name="isJson">If set to <c>true</c> is json.</param>
  339. /// <param name="param">Parameter.</param>
  340. public void AddQueuePush(FirebaseDatabase firebase, string json, bool isJson, FirebaseParam param)
  341. {
  342. FirebaseDatabase temp = firebase.Copy (true);
  343. temp.OnPushSuccess += OnSuccess;
  344. temp.OnPushFailed += OnFailed;
  345. if (!isJson)
  346. AddQueue (temp, FirebaseCommand.Push, param.Parameter, json);
  347. else
  348. AddQueue (temp, FirebaseCommand.Push, param.Parameter, Json.Deserialize(json));
  349. }
  350. /// <summary>
  351. /// Adds Firebase Delete request to queue.
  352. /// </summary>
  353. /// <param name="firebase">Firebase.</param>
  354. /// <param name="param">Parameter.</param>
  355. public void AddQueueDelete(FirebaseDatabase firebase, string param = "")
  356. {
  357. FirebaseDatabase temp = firebase.Copy (true);
  358. temp.OnDeleteSuccess += OnSuccess;
  359. temp.OnDeleteFailed += OnFailed;
  360. AddQueue (temp, FirebaseCommand.Delete, param);
  361. }
  362. /// <summary>
  363. /// Adds Firebase Delete request to queue.
  364. /// </summary>
  365. /// <param name="firebase">Firebase.</param>
  366. /// <param name="param">Parameter.</param>
  367. public void AddQueueDelete(FirebaseDatabase firebase, FirebaseParam param)
  368. {
  369. FirebaseDatabase temp = firebase.Copy (true);
  370. temp.OnDeleteSuccess += OnSuccess;
  371. temp.OnDeleteFailed += OnFailed;
  372. AddQueue (temp, FirebaseCommand.Delete, param.Parameter);
  373. }
  374. /// <summary>
  375. /// Adds Firebase Set Time Stamp request to queue.
  376. /// </summary>
  377. /// <param name="firebase">Firebase.</param>
  378. /// <param name="keyName">Time stamp key name.</param>
  379. public void AddQueueSetTimeStamp(FirebaseDatabase firebase, string keyName)
  380. {
  381. FirebaseDatabase temp = firebase.Child (keyName, false);
  382. AddQueueSet (temp, SERVER_VALUE_TIMESTAMP, true, "print=silent");
  383. }
  384. /// <summary>
  385. /// Adds Firebase Set Time Stamp request with callback to queue.
  386. /// </summary>
  387. /// <param name="firebase">Firebase.</param>
  388. /// <param name="keyName">Key name.</param>
  389. /// <param name="_OnSuccess">On success callback.</param>
  390. /// <param name="_OnFailed">On fail callback.</param>
  391. public void AddQueueSetTimeStamp(FirebaseDatabase firebase, string keyName, Action<FirebaseDatabase, DataSnapshot> _OnSuccess, Action<FirebaseDatabase, FirebaseError> _OnFailed)
  392. {
  393. FirebaseDatabase temp = firebase.Child (keyName);
  394. temp.OnSetSuccess += _OnSuccess;
  395. temp.OnSetFailed += _OnFailed;
  396. AddQueueSet (temp, SERVER_VALUE_TIMESTAMP, true);
  397. }
  398. /// <summary>
  399. /// Force stop the command queue chain and clears the queue.
  400. /// </summary>
  401. public void ForceClearQueue()
  402. {
  403. ClearQueueTopDown (head);
  404. }
  405. #endregion
  406. }
  407. }