Skip to content

Commit

Permalink
Merge pull request #16 from kadirahq/comments-cache
Browse files Browse the repository at this point in the history
Implement the comments cache.
  • Loading branch information
arunoda authored Oct 8, 2016
2 parents c4ce8d0 + 6c5a16b commit b0d45c9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react-dom": "^0.14.7 || ^15.0.0"
},
"dependencies": {
"deep-equal": "^1.0.1",
"moment": "^2.15.1"
}
}
41 changes: 35 additions & 6 deletions src/manager/containers/CommentsPanel/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react';
import addons from '@kadira/storybook-addons';
import CommentsPanel from '../../components/CommentsPanel/';
import deepEquals from 'deep-equal';

export default class Container extends Component {
constructor(props, ...args) {
Expand All @@ -12,14 +13,32 @@ export default class Container extends Component {
loading: true,
};
// bind functions so it can be passed later
this.addComment = this.addComment.bind(this);
this.syncDatabase = this.syncDatabase.bind(this);
this.addComment = this.addComment.bind(this)

// keep loaded comments here. So, we could use them when switching
// stories.
this.cache = {};
}

addToCache(selection, comments) {
const key = JSON.stringify(selection);
this.cache[key] = comments;
}

getFromCache(selection) {
const key = JSON.stringify(selection);
return this.cache[key] || [];
}

componentDidMount() {
// Clear the current notes on every story change.
this.stopListeningOnStory = this.props.api.onStory((kind, story) => {
// set the current selection
this.selection = { sbKind: kind, sbStory: story };

// set comments fetch from the cache.
this.setState({ comments: this.getFromCache(this.selection) });

Promise.resolve(null)
.then(() => this.syncDatabase())
.then(() => this.getCurrentUser())
Expand Down Expand Up @@ -57,10 +76,20 @@ export default class Container extends Component {

loadCommentsCollection() {
const db = addons.getDatabase();
const query = { ...this.selection };

const selection = { ...this.selection };
const query = selection;
const options = {limit: 1e6};
return db.getCollection('comments').get(query, options)
.then(comments => this.setState({ comments }));
return db.getCollection('comments')
.get(query, options)
.then(comments => {
// add to cache
this.addToCache(selection, comments);
// set comments only if we are on the relavant story
if (deepEquals(selection, this.selection)) {
this.setState({ comments });
}
});
}

addPendingComment(_comment) {
Expand Down Expand Up @@ -106,7 +135,7 @@ export default class Container extends Component {
comments: this.state.comments,
loading: this.state.loading,
loggedIn: this.state.loggedIn,
addComment: this.addComment,
addComment: c => this.addComment(c),
};
return <CommentsPanel {...props} />;
}
Expand Down

0 comments on commit b0d45c9

Please sign in to comment.