123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- import { DataCache } from 'meteor-reactive-cache';
- import { Jsons } from './jsons';
- // Server isn't reactive, so search for the data always.
- ReactiveCacheServer = {
- getBoard(id) {
- const ret = Boards.findOne(id);
- return ret;
- },
- getList(id) {
- const ret = Lists.findOne(id);
- return ret;
- },
- getSwimlane(id) {
- const ret = Swimlanes.findOne(id);
- return ret;
- },
- getChecklist(id) {
- const ret = Checklists.findOne(id);
- return ret;
- },
- getCard(id) {
- const ret = Cards.findOne(id);
- return ret;
- },
- getCustomField(id) {
- const ret = CustomFields.findOne(id);
- return ret;
- },
- getCustomFields(selector) {
- const ret = CustomFields.find(selector).fetch();
- return ret;
- },
- getUser(id) {
- const ret = Users.findOne(id);
- return ret;
- },
- getCurrentSetting() {
- const ret = Settings.findOne();
- return ret;
- },
- getCurrentUser() {
- const ret = Meteor.user();
- return ret;
- },
- }
- // only the Client is reactive
- // saving the result has a big advantage if the query is big and often searched for the same data again and again
- // if the data is changed in the client, the data is saved to the server and depending code is reactive called again
- ReactiveCacheClient = {
- getBoard(id) {
- if (!this.__board) {
- this.__board = new DataCache(boardId => {
- const _ret = Boards.findOne(boardId);
- return _ret;
- });
- }
- const ret = this.__board.get(id);
- return ret;
- },
- getList(id) {
- if (!this.__list) {
- this.__list = new DataCache(listId => {
- const _ret = Lists.findOne(listId);
- return _ret;
- });
- }
- const ret = this.__list.get(id);
- return ret;
- },
- getSwimlane(id) {
- if (!this.__swimlane) {
- this.__swimlane = new DataCache(swimlaneId => {
- const _ret = Swimlanes.findOne(swimlaneId);
- return _ret;
- });
- }
- const ret = this.__swimlane.get(id);
- return ret;
- },
- getChecklist(id) {
- if (!this.__checklist) {
- this.__checklist = new DataCache(checklistId => {
- const _ret = Checklists.findOne(checklistId);
- return _ret;
- });
- }
- const ret = this.__checklist.get(id);
- return ret;
- },
- getCard(id) {
- if (!this.__card) {
- this.__card = new DataCache(cardId => {
- const _ret = Cards.findOne(cardId);
- return _ret;
- });
- }
- const ret = this.__card.get(id);
- return ret;
- },
- getCustomField(id) {
- if (!this.__customField) {
- this.__customField = new DataCache(customFieldId => {
- const _ret = CustomFields.findOne(customFieldId);
- return _ret;
- });
- }
- const ret = this.__customField.get(id);
- return ret;
- },
- getCustomFields(selector) {
- if (!this.__customFields) {
- this.__customFields = new DataCache(sel => {
- const _ret = CustomFields.find(Jsons.parse(sel)).fetch();
- return _ret;
- });
- }
- const ret = this.__customFields.get(Jsons.stringify(selector));
- return ret;
- },
- getUser(id) {
- if (!this.__user) {
- this.__user = new DataCache(userId => {
- const _ret = Users.findOne(userId);
- return _ret;
- });
- }
- const ret = this.__user.get(id);
- return ret;
- },
- getCurrentSetting() {
- if (!this.__currentSetting || !this.__currentSetting.get()) {
- this.__currentSetting = new DataCache(() => {
- const _ret = Settings.findOne();
- return _ret;
- });
- }
- const ret = this.__currentSetting.get();
- return ret;
- },
- getCurrentUser() {
- if (!this.__currentUser || !this.__currentUser.get()) {
- this.__currentUser = new DataCache(() => {
- const _ret = Meteor.user();
- return _ret;
- });
- }
- const ret = this.__currentUser.get();
- return ret;
- }
- }
- // global Reactive Cache class to avoid big overhead while searching for the same data often again
- // This class calls 2 implementation, for server and client code
- //
- // having this class here has several advantages:
- // - The Programmer hasn't to care about in which context he call's this class
- // - having all queries together in 1 class to make it possible to see which queries in Wekan happens, e.g. with console.log
- ReactiveCache = {
- getBoard(id) {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getBoard(id);
- } else {
- ret = ReactiveCacheClient.getBoard(id);
- }
- return ret;
- },
- getList(id) {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getList(id);
- } else {
- ret = ReactiveCacheClient.getList(id);
- }
- return ret;
- },
- getSwimlane(id) {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getSwimlane(id);
- } else {
- ret = ReactiveCacheClient.getSwimlane(id);
- }
- return ret;
- },
- getChecklist(id) {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getChecklist(id);
- } else {
- ret = ReactiveCacheClient.getChecklist(id);
- }
- return ret;
- },
- getCard(id) {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getCard(id);
- } else {
- ret = ReactiveCacheClient.getCard(id);
- }
- return ret;
- },
- getCustomField(id) {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getCustomField(id);
- } else {
- ret = ReactiveCacheClient.getCustomField(id);
- }
- return ret;
- },
- getCustomFields(selector) {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getCustomFields(selector);
- } else {
- ret = ReactiveCacheClient.getCustomFields(selector);
- }
- return ret;
- },
- getUser(id) {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getUser(id);
- } else {
- ret = ReactiveCacheClient.getUser(id);
- }
- return ret;
- },
- getCurrentSetting() {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getCurrentSetting();
- } else {
- ret = ReactiveCacheClient.getCurrentSetting();
- }
- return ret;
- },
- getCurrentUser() {
- let ret;
- if (Meteor.isServer) {
- ret = ReactiveCacheServer.getCurrentUser();
- } else {
- ret = ReactiveCacheClient.getCurrentUser();
- }
- return ret;
- },
- }
- export { ReactiveCache };
|