Clean up discussion list retaining mechanism

This commit is contained in:
Toby Zerner 2015-05-07 10:27:29 +09:30
parent 6b8dfdfb1c
commit 16c6bd4f23
5 changed files with 14 additions and 18 deletions

View File

@ -17,24 +17,15 @@ export default class DiscussionList extends Component {
this.loading = m.prop(true); this.loading = m.prop(true);
this.moreResults = m.prop(false); this.moreResults = m.prop(false);
this.discussions = m.prop([]); this.discussions = m.prop([]);
this.subtrees = [];
this.willRedraw();
this.refresh(); this.refresh();
app.session.on('loggedIn', this.loggedInHandler = this.refresh.bind(this)) app.session.on('loggedIn', this.loggedInHandler = this.refresh.bind(this))
} }
willRedraw() {
this.subtrees = [];
this.addSubtrees(this.discussions());
}
addSubtrees(discussions) { addSubtrees(discussions) {
discussions.forEach(discussion => {
this.subtrees[discussion.id()] = new SubtreeRetainer(
() => discussion.freshness
)
});
} }
params() { params() {
@ -90,7 +81,9 @@ export default class DiscussionList extends Component {
parseResults(results) { parseResults(results) {
m.startComputation(); m.startComputation();
this.loading(false); this.loading(false);
this.addSubtrees(results);
results.forEach(discussion => this.subtrees[discussion.id()] = new SubtreeRetainer(() => discussion.freshness));
[].push.apply(this.discussions(), results); [].push.apply(this.discussions(), results);
this.moreResults(!!results.payload.links.next); this.moreResults(!!results.payload.links.next);
m.endComputation(); m.endComputation();

View File

@ -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)); app.store.find('discussions', m.route.param('id'), this.params()).then(this.setupDiscussion.bind(this));
if (app.cache.discussionList) { if (app.cache.discussionList) {
if (!(app.current instanceof DiscussionPage)) {
app.cache.discussionList.subtrees.map(subtree => subtree.invalidate());
}
app.pane.enable(); app.pane.enable();
app.pane.hide(); app.pane.hide();
m.redraw.strategy('diff'); // otherwise pane redraws and mouseenter even is triggered so it doesn't hide m.redraw.strategy('diff'); // otherwise pane redraws and mouseenter even is triggered so it doesn't hide

View File

@ -22,7 +22,7 @@ export default class IndexPage extends Component {
var params = this.params(); var params = this.params();
if (app.cache.discussionList) { if (app.cache.discussionList) {
app.cache.discussionList.willRedraw(); app.cache.discussionList.subtrees.map(subtree => subtree.invalidate());
Object.keys(params).some(key => { Object.keys(params).some(key => {
if (app.cache.discussionList.props.params[key] !== params[key]) { if (app.cache.discussionList.props.params[key] !== params[key]) {
app.cache.discussionList = null; app.cache.discussionList = null;

View File

@ -17,7 +17,7 @@ export default class PostComment extends Post {
super(props); super(props);
this.postHeaderUser = new PostHeaderUser({post: this.props.post}); this.postHeaderUser = new PostHeaderUser({post: this.props.post});
this.subtree.add(this.postHeaderUser.showCard); this.subtree.check(this.postHeaderUser.showCard);
} }
view() { view() {

View File

@ -4,14 +4,14 @@
() => this.props.post.freshness, () => this.props.post.freshness,
() => this.showing () => this.showing
); );
this.subtree.add(() => this.props.user.freshness); this.subtree.check(() => this.props.user.freshness);
// view // view
this.subtree.retain() || 'expensive expression' this.subtree.retain() || 'expensive expression'
*/ */
export default class SubtreeRetainer { export default class SubtreeRetainer {
constructor() { constructor() {
this.clear(); this.invalidate();
this.callbacks = [].slice.call(arguments); this.callbacks = [].slice.call(arguments);
} }
@ -27,11 +27,11 @@ export default class SubtreeRetainer {
return needsRebuild ? false : {subtree: 'retain'}; return needsRebuild ? false : {subtree: 'retain'};
} }
add() { check() {
this.callbacks = this.callbacks.concat([].slice.call(arguments)); this.callbacks = this.callbacks.concat([].slice.call(arguments));
} }
clear() { invalidate() {
this.old = []; this.old = [];
} }
} }