Extracted member activity fetching into new service

refs https://github.com/TryGhost/Team/issues/1277

- pulled timeline fetching from `members-stats` service to `members-activity` service ready for further refactoring to make fetching/processing more generic
This commit is contained in:
Kevin Ansfield 2022-01-18 13:55:42 +00:00
parent 6579d8de5c
commit 93b4262db5
3 changed files with 35 additions and 26 deletions

View File

@ -7,6 +7,7 @@ import {tracked} from '@glimmer/tracking';
export default class DashboardController extends Controller {
@service feature;
@service session;
@service membersActivity;
@service membersStats;
@service store;
@service settings;
@ -162,7 +163,7 @@ export default class DashboardController extends Controller {
loadEvents() {
this.eventsLoading = true;
this.membersStats.fetchTimeline({limit: 5}).then(({events}) => {
this.membersActivity.fetchTimeline({limit: 5}).then(({events}) => {
this.eventsData = events;
this.eventsLoading = false;
}, (error) => {

View File

@ -1,5 +1,38 @@
import Service from '@ember/service';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency-decorators';
const ONE_MINUTE = 1 * 60 * 1000;
export default class MembersActivityService extends Service {
@service ajax;
@service ghostPaths;
_lastFetchedTimeline = null;
_lastFetchedTimelineLimit = null;
async fetchTimeline(options = {}) {
let staleData = this._lastFetchedTimeline && (new Date() - this._lastFetchedTimeline) > ONE_MINUTE;
let differentLimit = this._lastFetchedTimelineLimit && this._lastFetchedTimelineLimit !== options.limit;
if (this._fetchTimelineTask.isRunning) {
return this._fetchTimelineTask.last;
}
if (this.events && !staleData && !differentLimit) {
return this.events;
}
return this._fetchTimelineTask.perform(options.limit);
}
@task
*_fetchTimelineTask(limit) {
this._lastFetchedTimeline = new Date();
this._lastFetchedTimelineLimit = limit;
let eventsUrl = this.ghostPaths.url.api('members/events');
let events = yield this.ajax.request(eventsUrl, {data: {limit}});
this.events = events;
return events;
}
}

View File

@ -35,21 +35,6 @@ export default class MembersStatsService extends Service {
return this._fetchTask.perform();
}
fetchTimeline(options = {}) {
let staleData = this._lastFetchedTimeline && (new Date() - this._lastFetchedTimeline) > ONE_MINUTE;
let differentLimit = this._lastFetchedTimelineLimit && this._lastFetchedTimelineLimit !== options.limit;
if (this._fetchTimelineTask.isRunning) {
return this._fetchTask.last;
}
if (this.events && !this._forceRefresh && !staleData && !differentLimit) {
return Promise.resolve(this.events);
}
return this._fetchTimelineTask.perform(options.limit);
}
fetchCounts() {
let staleData = this._lastFetchedCounts && (new Date() - this._lastFetchedCounts) > ONE_MINUTE;
@ -235,14 +220,4 @@ export default class MembersStatsService extends Service {
this.stats = stats;
return stats;
}
@task
*_fetchTimelineTask(limit) {
this._lastFetchedTimeline = new Date();
this._lastFetchedTimelineLimit = limit;
let eventsUrl = this.ghostPaths.url.api('members/events');
let events = yield this.ajax.request(eventsUrl, {data: {limit}});
this.events = events;
return events;
}
}