reactiveCache.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. getAttachment(id) {
  42. const ret = Attachments.findOne(id);
  43. return ret;
  44. },
  45. getUser(id) {
  46. const ret = Users.findOne(id);
  47. return ret;
  48. },
  49. getOrg(id) {
  50. const ret = Org.findOne(id);
  51. return ret;
  52. },
  53. getTeam(id) {
  54. const ret = Team.findOne(id);
  55. return ret;
  56. },
  57. getActivity(id) {
  58. const ret = Activities.findOne(id);
  59. return ret;
  60. },
  61. getCurrentSetting() {
  62. const ret = Settings.findOne();
  63. return ret;
  64. },
  65. getCurrentUser() {
  66. const ret = Meteor.user();
  67. return ret;
  68. },
  69. }
  70. // only the Client is reactive
  71. // saving the result has a big advantage if the query is big and often searched for the same data again and again
  72. // if the data is changed in the client, the data is saved to the server and depending code is reactive called again
  73. ReactiveCacheClient = {
  74. getBoard(id) {
  75. if (!this.__board) {
  76. this.__board = new DataCache(boardId => {
  77. const _ret = Boards.findOne(boardId);
  78. return _ret;
  79. });
  80. }
  81. const ret = this.__board.get(id);
  82. return ret;
  83. },
  84. getList(id) {
  85. if (!this.__list) {
  86. this.__list = new DataCache(listId => {
  87. const _ret = Lists.findOne(listId);
  88. return _ret;
  89. });
  90. }
  91. const ret = this.__list.get(id);
  92. return ret;
  93. },
  94. getSwimlane(id) {
  95. if (!this.__swimlane) {
  96. this.__swimlane = new DataCache(swimlaneId => {
  97. const _ret = Swimlanes.findOne(swimlaneId);
  98. return _ret;
  99. });
  100. }
  101. const ret = this.__swimlane.get(id);
  102. return ret;
  103. },
  104. getChecklist(id) {
  105. if (!this.__checklist) {
  106. this.__checklist = new DataCache(checklistId => {
  107. const _ret = Checklists.findOne(checklistId);
  108. return _ret;
  109. });
  110. }
  111. const ret = this.__checklist.get(id);
  112. return ret;
  113. },
  114. getChecklistItem(id) {
  115. if (!this.__checklistItem) {
  116. this.__checklistItem = new DataCache(_id => {
  117. const _ret = ChecklistItems.findOne(_id);
  118. return _ret;
  119. });
  120. }
  121. const ret = this.__checklistItem.get(id);
  122. return ret;
  123. },
  124. getCard(id) {
  125. if (!this.__card) {
  126. this.__card = new DataCache(cardId => {
  127. const _ret = Cards.findOne(cardId);
  128. return _ret;
  129. });
  130. }
  131. const ret = this.__card.get(id);
  132. return ret;
  133. },
  134. getCardComment(id) {
  135. if (!this.__cardComment) {
  136. this.__cardComment = new DataCache(_id => {
  137. const _ret = CardComments.findOne(_id);
  138. return _ret;
  139. });
  140. }
  141. const ret = this.__cardComment.get(id);
  142. return ret;
  143. },
  144. getCustomField(id) {
  145. if (!this.__customField) {
  146. this.__customField = new DataCache(customFieldId => {
  147. const _ret = CustomFields.findOne(customFieldId);
  148. return _ret;
  149. });
  150. }
  151. const ret = this.__customField.get(id);
  152. return ret;
  153. },
  154. getCustomFields(selector) {
  155. if (!this.__customFields) {
  156. this.__customFields = new DataCache(sel => {
  157. const _ret = CustomFields.find(Jsons.parse(sel)).fetch();
  158. return _ret;
  159. });
  160. }
  161. const ret = this.__customFields.get(Jsons.stringify(selector));
  162. return ret;
  163. },
  164. getAttachment(id) {
  165. if (!this.__attachment) {
  166. this.__attachment = new DataCache(_id => {
  167. const _ret = Attachments.findOne(_id);
  168. return _ret;
  169. });
  170. }
  171. const ret = this.__attachment.get(id);
  172. return ret;
  173. },
  174. getUser(id) {
  175. if (!this.__user) {
  176. this.__user = new DataCache(userId => {
  177. const _ret = Users.findOne(userId);
  178. return _ret;
  179. });
  180. }
  181. const ret = this.__user.get(id);
  182. return ret;
  183. },
  184. getOrg(id) {
  185. if (!this.__org) {
  186. this.__org = new DataCache(_id => {
  187. const _ret = Org.findOne(_id);
  188. return _ret;
  189. });
  190. }
  191. const ret = this.__org.get(id);
  192. return ret;
  193. },
  194. getTeam(id) {
  195. if (!this.__team) {
  196. this.__team = new DataCache(_id => {
  197. const _ret = Team.findOne(_id);
  198. return _ret;
  199. });
  200. }
  201. const ret = this.__team.get(id);
  202. return ret;
  203. },
  204. getActivity(id) {
  205. if (!this.__activity) {
  206. this.__activity = new DataCache(_id => {
  207. const _ret = Activities.findOne(_id);
  208. return _ret;
  209. });
  210. }
  211. const ret = this.__activity.get(id);
  212. return ret;
  213. },
  214. getCurrentSetting() {
  215. if (!this.__currentSetting || !this.__currentSetting.get()) {
  216. this.__currentSetting = new DataCache(() => {
  217. const _ret = Settings.findOne();
  218. return _ret;
  219. });
  220. }
  221. const ret = this.__currentSetting.get();
  222. return ret;
  223. },
  224. getCurrentUser() {
  225. if (!this.__currentUser || !this.__currentUser.get()) {
  226. this.__currentUser = new DataCache(() => {
  227. const _ret = Meteor.user();
  228. return _ret;
  229. });
  230. }
  231. const ret = this.__currentUser.get();
  232. return ret;
  233. }
  234. }
  235. // global Reactive Cache class to avoid big overhead while searching for the same data often again
  236. // This class calls 2 implementation, for server and client code
  237. //
  238. // having this class here has several advantages:
  239. // - The Programmer hasn't to care about in which context he call's this class
  240. // - having all queries together in 1 class to make it possible to see which queries in Wekan happens, e.g. with console.log
  241. ReactiveCache = {
  242. getBoard(id) {
  243. let ret;
  244. if (Meteor.isServer) {
  245. ret = ReactiveCacheServer.getBoard(id);
  246. } else {
  247. ret = ReactiveCacheClient.getBoard(id);
  248. }
  249. return ret;
  250. },
  251. getList(id) {
  252. let ret;
  253. if (Meteor.isServer) {
  254. ret = ReactiveCacheServer.getList(id);
  255. } else {
  256. ret = ReactiveCacheClient.getList(id);
  257. }
  258. return ret;
  259. },
  260. getSwimlane(id) {
  261. let ret;
  262. if (Meteor.isServer) {
  263. ret = ReactiveCacheServer.getSwimlane(id);
  264. } else {
  265. ret = ReactiveCacheClient.getSwimlane(id);
  266. }
  267. return ret;
  268. },
  269. getChecklist(id) {
  270. let ret;
  271. if (Meteor.isServer) {
  272. ret = ReactiveCacheServer.getChecklist(id);
  273. } else {
  274. ret = ReactiveCacheClient.getChecklist(id);
  275. }
  276. return ret;
  277. },
  278. getChecklistItem(id) {
  279. let ret;
  280. if (Meteor.isServer) {
  281. ret = ReactiveCacheServer.getChecklistItem(id);
  282. } else {
  283. ret = ReactiveCacheClient.getChecklistItem(id);
  284. }
  285. return ret;
  286. },
  287. getCard(id) {
  288. let ret;
  289. if (Meteor.isServer) {
  290. ret = ReactiveCacheServer.getCard(id);
  291. } else {
  292. ret = ReactiveCacheClient.getCard(id);
  293. }
  294. return ret;
  295. },
  296. getCardComment(id) {
  297. let ret;
  298. if (Meteor.isServer) {
  299. ret = ReactiveCacheServer.getCardComment(id);
  300. } else {
  301. ret = ReactiveCacheClient.getCardComment(id);
  302. }
  303. return ret;
  304. },
  305. getCustomField(id) {
  306. let ret;
  307. if (Meteor.isServer) {
  308. ret = ReactiveCacheServer.getCustomField(id);
  309. } else {
  310. ret = ReactiveCacheClient.getCustomField(id);
  311. }
  312. return ret;
  313. },
  314. getCustomFields(selector) {
  315. let ret;
  316. if (Meteor.isServer) {
  317. ret = ReactiveCacheServer.getCustomFields(selector);
  318. } else {
  319. ret = ReactiveCacheClient.getCustomFields(selector);
  320. }
  321. return ret;
  322. },
  323. getAttachment(id) {
  324. let ret;
  325. if (Meteor.isServer) {
  326. ret = ReactiveCacheServer.getAttachment(id);
  327. } else {
  328. ret = ReactiveCacheClient.getAttachment(id);
  329. }
  330. return ret;
  331. },
  332. getUser(id) {
  333. let ret;
  334. if (Meteor.isServer) {
  335. ret = ReactiveCacheServer.getUser(id);
  336. } else {
  337. ret = ReactiveCacheClient.getUser(id);
  338. }
  339. return ret;
  340. },
  341. getOrg(id) {
  342. let ret;
  343. if (Meteor.isServer) {
  344. ret = ReactiveCacheServer.getOrg(id);
  345. } else {
  346. ret = ReactiveCacheClient.getOrg(id);
  347. }
  348. return ret;
  349. },
  350. getTeam(id) {
  351. let ret;
  352. if (Meteor.isServer) {
  353. ret = ReactiveCacheServer.getTeam(id);
  354. } else {
  355. ret = ReactiveCacheClient.getTeam(id);
  356. }
  357. return ret;
  358. },
  359. getActivity(id) {
  360. let ret;
  361. if (Meteor.isServer) {
  362. ret = ReactiveCacheServer.getActivity(id);
  363. } else {
  364. ret = ReactiveCacheClient.getActivity(id);
  365. }
  366. return ret;
  367. },
  368. getCurrentSetting() {
  369. let ret;
  370. if (Meteor.isServer) {
  371. ret = ReactiveCacheServer.getCurrentSetting();
  372. } else {
  373. ret = ReactiveCacheClient.getCurrentSetting();
  374. }
  375. return ret;
  376. },
  377. getCurrentUser() {
  378. let ret;
  379. if (Meteor.isServer) {
  380. ret = ReactiveCacheServer.getCurrentUser();
  381. } else {
  382. ret = ReactiveCacheClient.getCurrentUser();
  383. }
  384. return ret;
  385. },
  386. }
  387. export { ReactiveCache };