reactiveCache.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. getUser(id) {
  34. const ret = Users.findOne(id);
  35. return ret;
  36. },
  37. getCurrentSetting() {
  38. const ret = Settings.findOne();
  39. return ret;
  40. },
  41. getCurrentUser() {
  42. const ret = Meteor.user();
  43. return ret;
  44. },
  45. }
  46. // only the Client is reactive
  47. // saving the result has a big advantage if the query is big and often searched for the same data again and again
  48. // if the data is changed in the client, the data is saved to the server and depending code is reactive called again
  49. ReactiveCacheClient = {
  50. getBoard(id) {
  51. if (!this.__board) {
  52. this.__board = new DataCache(boardId => {
  53. const _ret = Boards.findOne(boardId);
  54. return _ret;
  55. });
  56. }
  57. const ret = this.__board.get(id);
  58. return ret;
  59. },
  60. getList(id) {
  61. if (!this.__list) {
  62. this.__list = new DataCache(listId => {
  63. const _ret = Lists.findOne(listId);
  64. return _ret;
  65. });
  66. }
  67. const ret = this.__list.get(id);
  68. return ret;
  69. },
  70. getSwimlane(id) {
  71. if (!this.__swimlane) {
  72. this.__swimlane = new DataCache(swimlaneId => {
  73. const _ret = Swimlanes.findOne(swimlaneId);
  74. return _ret;
  75. });
  76. }
  77. const ret = this.__swimlane.get(id);
  78. return ret;
  79. },
  80. getChecklist(id) {
  81. if (!this.__checklist) {
  82. this.__checklist = new DataCache(checklistId => {
  83. const _ret = Checklists.findOne(checklistId);
  84. return _ret;
  85. });
  86. }
  87. const ret = this.__checklist.get(id);
  88. return ret;
  89. },
  90. getCard(id) {
  91. if (!this.__card) {
  92. this.__card = new DataCache(cardId => {
  93. const _ret = Cards.findOne(cardId);
  94. return _ret;
  95. });
  96. }
  97. const ret = this.__card.get(id);
  98. return ret;
  99. },
  100. getCustomField(id) {
  101. if (!this.__customField) {
  102. this.__customField = new DataCache(customFieldId => {
  103. const _ret = CustomFields.findOne(customFieldId);
  104. return _ret;
  105. });
  106. }
  107. const ret = this.__customField.get(id);
  108. return ret;
  109. },
  110. getCustomFields(selector) {
  111. if (!this.__customFields) {
  112. this.__customFields = new DataCache(sel => {
  113. const _ret = CustomFields.find(Jsons.parse(sel)).fetch();
  114. return _ret;
  115. });
  116. }
  117. const ret = this.__customFields.get(Jsons.stringify(selector));
  118. return ret;
  119. },
  120. getUser(id) {
  121. if (!this.__user) {
  122. this.__user = new DataCache(userId => {
  123. const _ret = Users.findOne(userId);
  124. return _ret;
  125. });
  126. }
  127. const ret = this.__user.get(id);
  128. return ret;
  129. },
  130. getCurrentSetting() {
  131. if (!this.__currentSetting || !this.__currentSetting.get()) {
  132. this.__currentSetting = new DataCache(() => {
  133. const _ret = Settings.findOne();
  134. return _ret;
  135. });
  136. }
  137. const ret = this.__currentSetting.get();
  138. return ret;
  139. },
  140. getCurrentUser() {
  141. if (!this.__currentUser || !this.__currentUser.get()) {
  142. this.__currentUser = new DataCache(() => {
  143. const _ret = Meteor.user();
  144. return _ret;
  145. });
  146. }
  147. const ret = this.__currentUser.get();
  148. return ret;
  149. }
  150. }
  151. // global Reactive Cache class to avoid big overhead while searching for the same data often again
  152. // This class calls 2 implementation, for server and client code
  153. //
  154. // having this class here has several advantages:
  155. // - The Programmer hasn't to care about in which context he call's this class
  156. // - having all queries together in 1 class to make it possible to see which queries in Wekan happens, e.g. with console.log
  157. ReactiveCache = {
  158. getBoard(id) {
  159. let ret;
  160. if (Meteor.isServer) {
  161. ret = ReactiveCacheServer.getBoard(id);
  162. } else {
  163. ret = ReactiveCacheClient.getBoard(id);
  164. }
  165. return ret;
  166. },
  167. getList(id) {
  168. let ret;
  169. if (Meteor.isServer) {
  170. ret = ReactiveCacheServer.getList(id);
  171. } else {
  172. ret = ReactiveCacheClient.getList(id);
  173. }
  174. return ret;
  175. },
  176. getSwimlane(id) {
  177. let ret;
  178. if (Meteor.isServer) {
  179. ret = ReactiveCacheServer.getSwimlane(id);
  180. } else {
  181. ret = ReactiveCacheClient.getSwimlane(id);
  182. }
  183. return ret;
  184. },
  185. getChecklist(id) {
  186. let ret;
  187. if (Meteor.isServer) {
  188. ret = ReactiveCacheServer.getChecklist(id);
  189. } else {
  190. ret = ReactiveCacheClient.getChecklist(id);
  191. }
  192. return ret;
  193. },
  194. getCard(id) {
  195. let ret;
  196. if (Meteor.isServer) {
  197. ret = ReactiveCacheServer.getCard(id);
  198. } else {
  199. ret = ReactiveCacheClient.getCard(id);
  200. }
  201. return ret;
  202. },
  203. getCustomField(id) {
  204. let ret;
  205. if (Meteor.isServer) {
  206. ret = ReactiveCacheServer.getCustomField(id);
  207. } else {
  208. ret = ReactiveCacheClient.getCustomField(id);
  209. }
  210. return ret;
  211. },
  212. getCustomFields(selector) {
  213. let ret;
  214. if (Meteor.isServer) {
  215. ret = ReactiveCacheServer.getCustomFields(selector);
  216. } else {
  217. ret = ReactiveCacheClient.getCustomFields(selector);
  218. }
  219. return ret;
  220. },
  221. getUser(id) {
  222. let ret;
  223. if (Meteor.isServer) {
  224. ret = ReactiveCacheServer.getUser(id);
  225. } else {
  226. ret = ReactiveCacheClient.getUser(id);
  227. }
  228. return ret;
  229. },
  230. getCurrentSetting() {
  231. let ret;
  232. if (Meteor.isServer) {
  233. ret = ReactiveCacheServer.getCurrentSetting();
  234. } else {
  235. ret = ReactiveCacheClient.getCurrentSetting();
  236. }
  237. return ret;
  238. },
  239. getCurrentUser() {
  240. let ret;
  241. if (Meteor.isServer) {
  242. ret = ReactiveCacheServer.getCurrentUser();
  243. } else {
  244. ret = ReactiveCacheClient.getCurrentUser();
  245. }
  246. return ret;
  247. },
  248. }
  249. export { ReactiveCache };