reactiveCache.js 7.2 KB

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