reactiveCache.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { DataCache } from 'meteor-reactive-cache';
  2. import { Jsons } from './jsons';
  3. // Server isn't reactive, so search for the data always.
  4. ReactiveCacheServer = {
  5. getBoard(id) {
  6. const ret = Boards.findOne(id);
  7. return ret;
  8. },
  9. getList(id) {
  10. const ret = Lists.findOne(id);
  11. return ret;
  12. },
  13. getSwimlane(id) {
  14. const ret = Swimlanes.findOne(id);
  15. return ret;
  16. },
  17. getChecklist(id) {
  18. const ret = Checklists.findOne(id);
  19. return ret;
  20. },
  21. getCard(id) {
  22. const ret = Cards.findOne(id);
  23. return ret;
  24. },
  25. getCustomField(id) {
  26. const ret = CustomFields.findOne(id);
  27. return ret;
  28. },
  29. getCustomFields(selector) {
  30. const ret = CustomFields.find(selector).fetch();
  31. return ret;
  32. },
  33. getCurrentSetting() {
  34. const ret = Settings.findOne();
  35. return ret;
  36. },
  37. }
  38. // only the Client is reactive
  39. // saving the result has a big advantage if the query is big and often searched for the same data again and again
  40. // if the data is changed in the client, the data is saved to the server and depending code is reactive called again
  41. ReactiveCacheClient = {
  42. getBoard(id) {
  43. if (!this.__board) {
  44. this.__board = new DataCache(boardId => {
  45. const _ret = Boards.findOne(boardId);
  46. return _ret;
  47. });
  48. }
  49. const ret = this.__board.get(id);
  50. return ret;
  51. },
  52. getList(id) {
  53. if (!this.__list) {
  54. this.__list = new DataCache(listId => {
  55. const _ret = Lists.findOne(listId);
  56. return _ret;
  57. });
  58. }
  59. const ret = this.__list.get(id);
  60. return ret;
  61. },
  62. getSwimlane(id) {
  63. if (!this.__swimlane) {
  64. this.__swimlane = new DataCache(swimlaneId => {
  65. const _ret = Swimlanes.findOne(swimlaneId);
  66. return _ret;
  67. });
  68. }
  69. const ret = this.__swimlane.get(id);
  70. return ret;
  71. },
  72. getChecklist(id) {
  73. if (!this.__checklist) {
  74. this.__checklist = new DataCache(checklistId => {
  75. const _ret = Checklists.findOne(checklistId);
  76. return _ret;
  77. });
  78. }
  79. const ret = this.__checklist.get(id);
  80. return ret;
  81. },
  82. getCard(id) {
  83. if (!this.__card) {
  84. this.__card = new DataCache(cardId => {
  85. const _ret = Cards.findOne(cardId);
  86. return _ret;
  87. });
  88. }
  89. const ret = this.__card.get(id);
  90. return ret;
  91. },
  92. getCustomField(id) {
  93. if (!this.__customField) {
  94. this.__customField = new DataCache(customFieldId => {
  95. const _ret = CustomFields.findOne(customFieldId);
  96. return _ret;
  97. });
  98. }
  99. const ret = this.__customField.get(id);
  100. return ret;
  101. },
  102. getCustomFields(selector) {
  103. if (!this.__customFields) {
  104. this.__customFields = new DataCache(sel => {
  105. const _ret = CustomFields.find(Jsons.parse(sel)).fetch();
  106. return _ret;
  107. });
  108. }
  109. const ret = this.__customFields.get(Jsons.stringify(selector));
  110. return ret;
  111. },
  112. getCurrentSetting() {
  113. if (!this.__currentSetting || !this.__currentSetting.get()) {
  114. this.__currentSetting = new DataCache(() => {
  115. const _ret = Settings.findOne();
  116. return _ret;
  117. });
  118. }
  119. const ret = this.__currentSetting.get();
  120. return ret;
  121. }
  122. }
  123. // global Reactive Cache class to avoid big overhead while searching for the same data often again
  124. // This class calls 2 implementation, for server and client code
  125. //
  126. // having this class here has several advantages:
  127. // - The Programmer hasn't to care about in which context he call's this class
  128. // - having all queries together in 1 class to make it possible to see which queries in Wekan happens, e.g. with console.log
  129. ReactiveCache = {
  130. getBoard(id) {
  131. let ret;
  132. if (Meteor.isServer) {
  133. ret = ReactiveCacheServer.getBoard(id);
  134. } else {
  135. ret = ReactiveCacheClient.getBoard(id);
  136. }
  137. return ret;
  138. },
  139. getList(id) {
  140. let ret;
  141. if (Meteor.isServer) {
  142. ret = ReactiveCacheServer.getList(id);
  143. } else {
  144. ret = ReactiveCacheClient.getList(id);
  145. }
  146. return ret;
  147. },
  148. getSwimlane(id) {
  149. let ret;
  150. if (Meteor.isServer) {
  151. ret = ReactiveCacheServer.getSwimlane(id);
  152. } else {
  153. ret = ReactiveCacheClient.getSwimlane(id);
  154. }
  155. return ret;
  156. },
  157. getChecklist(id) {
  158. let ret;
  159. if (Meteor.isServer) {
  160. ret = ReactiveCacheServer.getChecklist(id);
  161. } else {
  162. ret = ReactiveCacheClient.getChecklist(id);
  163. }
  164. return ret;
  165. },
  166. getCard(id) {
  167. let ret;
  168. if (Meteor.isServer) {
  169. ret = ReactiveCacheServer.getCard(id);
  170. } else {
  171. ret = ReactiveCacheClient.getCard(id);
  172. }
  173. return ret;
  174. },
  175. getCustomField(id) {
  176. let ret;
  177. if (Meteor.isServer) {
  178. ret = ReactiveCacheServer.getCustomField(id);
  179. } else {
  180. ret = ReactiveCacheClient.getCustomField(id);
  181. }
  182. return ret;
  183. },
  184. getCustomFields(selector) {
  185. let ret;
  186. if (Meteor.isServer) {
  187. ret = ReactiveCacheServer.getCustomFields(selector);
  188. } else {
  189. ret = ReactiveCacheClient.getCustomFields(selector);
  190. }
  191. return ret;
  192. },
  193. getCurrentSetting() {
  194. let ret;
  195. if (Meteor.isServer) {
  196. ret = ReactiveCacheServer.getCurrentSetting();
  197. } else {
  198. ret = ReactiveCacheClient.getCurrentSetting();
  199. }
  200. return ret;
  201. },
  202. }
  203. export { ReactiveCache };