blob: 66a17cb296cc8353406f964ee126b594d8fbe982 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import { computed, observable } from 'mobx';
import { remove } from 'lodash';
import Store from './lib/Store';
import CachedRequest from './lib/CachedRequest';
import Request from './lib/Request';
import { CHECK_INTERVAL } from '../config';
export default class NewsStore extends Store {
@observable latestNewsRequest = new CachedRequest(this.api.news, 'latest');
@observable hideNewsRequest = new Request(this.api.news, 'hide');
constructor(...args) {
super(...args);
// Register action handlers
this.actions.news.hide.listen(this._hide.bind(this));
this.actions.user.logout.listen(this._resetNewsRequest.bind(this));
}
setup() {
// Check for news updates every couple of hours
setInterval(() => {
if (this.latestNewsRequest.wasExecuted && this.stores.user.isLoggedIn) {
this.latestNewsRequest.invalidate({ immediately: true });
}
}, CHECK_INTERVAL);
}
@computed get latest() {
return this.latestNewsRequest.execute().result || [];
}
// Actions
_hide({ newsId }) {
this.hideNewsRequest.execute(newsId);
this.latestNewsRequest.invalidate().patch((result) => {
// TODO: check if we can use mobx.array remove
remove(result, (n) => n.id === newsId);
});
}
/**
* Reset the news request when current user logs out so that when another user
* logs in again without an app restart, the request will be fetched again and
* the news will be shown to the user.
*
* @private
*/
_resetNewsRequest() {
this.latestNewsRequest.reset();
}
}
|