Browse Source

Handle subscriptions better
* use onReady and onStop callbacks when subscribing
* show an server error message when the server returns an error
* call stop() on subscriptions

John Supplee 4 years ago
parent
commit
58020863a8

+ 1 - 1
client/components/main/dueCards.js

@@ -57,7 +57,7 @@ class DueCardsComponent extends CardSearchPagedComponent {
       queryParams.users = [Meteor.user().username];
       queryParams.users = [Meteor.user().username];
     }
     }
 
 
-    this.autorunGlobalSearch(queryParams);
+    this.runGlobalSearch(queryParams);
   }
   }
 
 
   dueCardsView() {
   dueCardsView() {

+ 6 - 0
client/components/main/globalSearch.jade

@@ -50,6 +50,12 @@ template(name="globalSearch")
                   = msg
                   = msg
           else
           else
             +resultsPaged(this)
             +resultsPaged(this)
+      else if serverError.get
+        .global-search-page
+          .global-search-help
+            h1 {{_ 'server-error' }}
+            +viewer
+              | {{_ 'server-error-troubleshooting' }}
       else
       else
         .global-search-page
         .global-search-page
           .global-search-help
           .global-search-help

+ 1 - 1
client/components/main/globalSearch.js

@@ -431,7 +431,7 @@ class GlobalSearchComponent extends CardSearchPagedComponent {
       return;
       return;
     }
     }
 
 
-    this.autorunGlobalSearch(params);
+    this.runGlobalSearch(params);
   }
   }
 
 
   searchInstructions() {
   searchInstructions() {

+ 1 - 1
client/components/main/myCards.js

@@ -53,7 +53,7 @@ class MyCardsComponent extends CardSearchPagedComponent {
       sort: { name: 'dueAt', order: 'des' },
       sort: { name: 'dueAt', order: 'des' },
     };
     };
 
 
-    this.autorunGlobalSearch(queryParams);
+    this.runGlobalSearch(queryParams);
     Meteor.subscribe('setting');
     Meteor.subscribe('setting');
   }
   }
 
 

+ 54 - 49
client/lib/cardSearch.js

@@ -13,6 +13,28 @@ export class CardSearchPagedComponent extends BlazeComponent {
     this.totalHits = 0;
     this.totalHits = 0;
     this.queryErrors = null;
     this.queryErrors = null;
     this.resultsPerPage = 25;
     this.resultsPerPage = 25;
+    this.sessionId = SessionData.getSessionId();
+    this.subscriptionHandle = null;
+    this.serverError = new ReactiveVar(false);
+
+    const that = this;
+    this.subscriptionCallbacks = {
+      onReady() {
+        that.getResults();
+        that.searching.set(false);
+        that.hasResults.set(true);
+        that.serverError.set(false);
+      },
+      onError(error) {
+        that.searching.set(false);
+        that.hasResults.set(false);
+        that.serverError.set(true);
+        console.log('Error.reason:', error.reason);
+        console.log('Error.message:', error.message);
+        console.log('Error.stack:', error.stack);
+      }
+    };
+
   }
   }
 
 
   resetSearch() {
   resetSearch() {
@@ -21,15 +43,15 @@ export class CardSearchPagedComponent extends BlazeComponent {
     this.hasResults.set(false);
     this.hasResults.set(false);
     this.hasQueryErrors.set(false);
     this.hasQueryErrors.set(false);
     this.resultsHeading.set('');
     this.resultsHeading.set('');
+    this.serverError.set(false);
     this.resultsCount = 0;
     this.resultsCount = 0;
     this.totalHits = 0;
     this.totalHits = 0;
     this.queryErrors = null;
     this.queryErrors = null;
   }
   }
 
 
-  getSessionData() {
+  getSessionData(sessionId) {
     return SessionData.findOne({
     return SessionData.findOne({
-      userId: Meteor.userId(),
-      sessionId: SessionData.getSessionId(),
+      sessionId: sessionId ? sessionId : SessionData.getSessionId(),
     });
     });
   }
   }
 
 
@@ -45,6 +67,7 @@ export class CardSearchPagedComponent extends BlazeComponent {
     const cards = Cards.find({ _id: { $in: sessionData.cards } }, projection);
     const cards = Cards.find({ _id: { $in: sessionData.cards } }, projection);
     this.queryErrors = sessionData.errors;
     this.queryErrors = sessionData.errors;
     if (this.queryErrors.length) {
     if (this.queryErrors.length) {
+      // console.log('queryErrors:', this.queryErrorMessages());
       this.hasQueryErrors.set(true);
       this.hasQueryErrors.set(true);
       return null;
       return null;
     }
     }
@@ -67,25 +90,21 @@ export class CardSearchPagedComponent extends BlazeComponent {
     return null;
     return null;
   }
   }
 
 
-  autorunGlobalSearch(params) {
-    this.searching.set(true);
+  stopSubscription() {
+    if (this.subscriptionHandle) {
+      this.subscriptionHandle.stop();
+    }
+  }
 
 
-    this.autorun(() => {
-      const handle = Meteor.subscribe(
-        'globalSearch',
-        SessionData.getSessionId(),
-        params,
-      );
-      Tracker.nonreactive(() => {
-        Tracker.autorun(() => {
-          if (handle.ready()) {
-            this.getResults();
-            this.searching.set(false);
-            this.hasResults.set(true);
-          }
-        });
-      });
-    });
+  runGlobalSearch(params) {
+    this.searching.set(true);
+    this.stopSubscription();
+    this.subscriptionHandle = Meteor.subscribe(
+      'globalSearch',
+      this.sessionId,
+      params,
+      this.subscriptionCallbacks,
+    );
   }
   }
 
 
   queryErrorMessages() {
   queryErrorMessages() {
@@ -103,37 +122,23 @@ export class CardSearchPagedComponent extends BlazeComponent {
   }
   }
 
 
   nextPage() {
   nextPage() {
-    const sessionData = this.getSessionData();
-
-    this.autorun(() => {
-      const handle = Meteor.subscribe('nextPage', sessionData.sessionId);
-      Tracker.nonreactive(() => {
-        Tracker.autorun(() => {
-          if (handle.ready()) {
-            this.getResults();
-            this.searching.set(false);
-            this.hasResults.set(true);
-          }
-        });
-      });
-    });
+    this.searching.set(true);
+    this.stopSubscription();
+    this.subscriptionHandle = Meteor.subscribe(
+      'nextPage',
+      this.sessionId,
+      this.subscriptionCallbacks,
+    );
   }
   }
 
 
   previousPage() {
   previousPage() {
-    const sessionData = this.getSessionData();
-
-    this.autorun(() => {
-      const handle = Meteor.subscribe('previousPage', sessionData.sessionId);
-      Tracker.nonreactive(() => {
-        Tracker.autorun(() => {
-          if (handle.ready()) {
-            this.getResults();
-            this.searching.set(false);
-            this.hasResults.set(true);
-          }
-        });
-      });
-    });
+    this.searching.set(true);
+    this.stopSubscription();
+    this.subscriptionHandle = Meteor.subscribe(
+      'previousPage',
+      this.sessionId,
+      this.subscriptionCallbacks,
+    );
   }
   }
 
 
   getResultsHeading() {
   getResultsHeading() {

+ 2 - 0
i18n/en.i18n.json

@@ -978,6 +978,8 @@
   "sort-cards": "Sort Cards",
   "sort-cards": "Sort Cards",
   "cardsSortPopup-title": "Sort Cards",
   "cardsSortPopup-title": "Sort Cards",
   "due-date": "Due Date",
   "due-date": "Due Date",
+  "server-error": "Server Error",
+  "server-error-troubleshooting": "Please submit the error generated by the server.\nFor a snap installation on Linux, run: `sudo journalctl -u 'snap.wekan.*'`",
   "title-alphabetically": "Title (Alphabetically)",
   "title-alphabetically": "Title (Alphabetically)",
   "created-at-newest-first": "Created At (Newest First)",
   "created-at-newest-first": "Created At (Newest First)",
   "created-at-oldest-first": "Created At (Oldest First)"
   "created-at-oldest-first": "Created At (Oldest First)"