From 741ca5ab92cf08e439644f9badc0640bb6713bd7 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 7 May 2015 10:27:29 +0930 Subject: [PATCH] Clean up discussion list retaining mechanism --- .../js/forum/src/components/discussion-list.js | 17 +++++------------ .../js/forum/src/components/discussion-page.js | 3 +++ .../core/js/forum/src/components/index-page.js | 2 +- .../js/forum/src/components/post-comment.js | 2 +- framework/core/js/lib/utils/subtree-retainer.js | 8 ++++---- 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/framework/core/js/forum/src/components/discussion-list.js b/framework/core/js/forum/src/components/discussion-list.js index 2973b8d1c..d00d59bd6 100644 --- a/framework/core/js/forum/src/components/discussion-list.js +++ b/framework/core/js/forum/src/components/discussion-list.js @@ -17,24 +17,15 @@ export default class DiscussionList extends Component { this.loading = m.prop(true); this.moreResults = m.prop(false); this.discussions = m.prop([]); + this.subtrees = []; - this.willRedraw(); this.refresh(); app.session.on('loggedIn', this.loggedInHandler = this.refresh.bind(this)) } - willRedraw() { - this.subtrees = []; - this.addSubtrees(this.discussions()); - } - addSubtrees(discussions) { - discussions.forEach(discussion => { - this.subtrees[discussion.id()] = new SubtreeRetainer( - () => discussion.freshness - ) - }); + } params() { @@ -90,7 +81,9 @@ export default class DiscussionList extends Component { parseResults(results) { m.startComputation(); this.loading(false); - this.addSubtrees(results); + + results.forEach(discussion => this.subtrees[discussion.id()] = new SubtreeRetainer(() => discussion.freshness)); + [].push.apply(this.discussions(), results); this.moreResults(!!results.payload.links.next); m.endComputation(); diff --git a/framework/core/js/forum/src/components/discussion-page.js b/framework/core/js/forum/src/components/discussion-page.js index a4eac7f7d..68d8aeb6f 100644 --- a/framework/core/js/forum/src/components/discussion-page.js +++ b/framework/core/js/forum/src/components/discussion-page.js @@ -36,6 +36,9 @@ export default class DiscussionPage extends Component { app.store.find('discussions', m.route.param('id'), this.params()).then(this.setupDiscussion.bind(this)); if (app.cache.discussionList) { + if (!(app.current instanceof DiscussionPage)) { + app.cache.discussionList.subtrees.map(subtree => subtree.invalidate()); + } app.pane.enable(); app.pane.hide(); m.redraw.strategy('diff'); // otherwise pane redraws and mouseenter even is triggered so it doesn't hide diff --git a/framework/core/js/forum/src/components/index-page.js b/framework/core/js/forum/src/components/index-page.js index 73c18974b..ec3e955ea 100644 --- a/framework/core/js/forum/src/components/index-page.js +++ b/framework/core/js/forum/src/components/index-page.js @@ -22,7 +22,7 @@ export default class IndexPage extends Component { var params = this.params(); if (app.cache.discussionList) { - app.cache.discussionList.willRedraw(); + app.cache.discussionList.subtrees.map(subtree => subtree.invalidate()); Object.keys(params).some(key => { if (app.cache.discussionList.props.params[key] !== params[key]) { app.cache.discussionList = null; diff --git a/framework/core/js/forum/src/components/post-comment.js b/framework/core/js/forum/src/components/post-comment.js index 8910b7a12..be57fb541 100644 --- a/framework/core/js/forum/src/components/post-comment.js +++ b/framework/core/js/forum/src/components/post-comment.js @@ -17,7 +17,7 @@ export default class PostComment extends Post { super(props); this.postHeaderUser = new PostHeaderUser({post: this.props.post}); - this.subtree.add(this.postHeaderUser.showCard); + this.subtree.check(this.postHeaderUser.showCard); } view() { diff --git a/framework/core/js/lib/utils/subtree-retainer.js b/framework/core/js/lib/utils/subtree-retainer.js index b6e99e318..b5430788f 100644 --- a/framework/core/js/lib/utils/subtree-retainer.js +++ b/framework/core/js/lib/utils/subtree-retainer.js @@ -4,14 +4,14 @@ () => this.props.post.freshness, () => this.showing ); - this.subtree.add(() => this.props.user.freshness); + this.subtree.check(() => this.props.user.freshness); // view this.subtree.retain() || 'expensive expression' */ export default class SubtreeRetainer { constructor() { - this.clear(); + this.invalidate(); this.callbacks = [].slice.call(arguments); } @@ -27,11 +27,11 @@ export default class SubtreeRetainer { return needsRebuild ? false : {subtree: 'retain'}; } - add() { + check() { this.callbacks = this.callbacks.concat([].slice.call(arguments)); } - clear() { + invalidate() { this.old = []; } }