0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Switched members-count-fetcher to use members-count-cache

no issue

In the new publish flow we use `{{members-count-fetcher}}` to show member counts as needed in the template without needing to worry about manually fetching counts in the backing classes. However, when switching between states in the flow the count would be re-requested resulting in some glitchy looking async count rendering. By changing `{{members-count-fetcher}}` to use our `members-count-cache` service internally we reduce the likelihood of an async count being triggered when switching between publish flow states.

- updated `members-count-cache` service methods to work with filter strings or full query objects
- switched `members-count-fetcher` resource to use `members-count-cache` in place of directly querying the store
This commit is contained in:
Kevin Ansfield 2022-05-04 22:51:34 +01:00
parent 1fc436f3e2
commit 7c4674507e
2 changed files with 23 additions and 10 deletions

View file

@ -4,7 +4,7 @@ import {task} from 'ember-concurrency';
import {tracked} from '@glimmer/tracking';
export default class MembersCount extends Resource {
@service store;
@service membersCountCache;
@tracked count = null;
@ -33,7 +33,7 @@ export default class MembersCount extends Resource {
@task
*fetchMembersTask({query} = {}) {
const result = yield this.store.query('member', {...query, limit: 1});
this.count = result.meta.pagination.total;
const count = yield this.membersCountCache.count(query);
this.count = count;
}
}

View file

@ -1,5 +1,6 @@
import Service, {inject as service} from '@ember/service';
import moment from 'moment';
import {action} from '@ember/object';
import {ghPluralize} from 'ghost-admin/helpers/gh-pluralize';
import {task} from 'ember-concurrency';
@ -9,20 +10,27 @@ export default class MembersCountCacheService extends Service {
cache = {};
async count(filter) {
const cachedValue = this.cache[filter];
@action
async count(query) {
if (typeof query === 'string') {
query = {filter: query};
}
const cacheKey = JSON.stringify(query);
const cachedValue = this.cache[cacheKey];
if (cachedValue && moment().diff(cachedValue.time, 'seconds') <= 60) {
return cachedValue.count;
}
const count = this._countMembersTask.perform(filter);
const count = this._countMembersTask.perform(query);
this.cache[filter] = {count, time: moment()};
this.cache[cacheKey] = {count, time: moment()};
return count;
}
@action
async countString(filter = '', {knownCount} = {}) {
const user = this.session.user;
@ -61,14 +69,19 @@ export default class MembersCountCacheService extends Service {
return ghPluralize(recipientCount, 'member');
}
@action
clear() {
this.cache = {};
}
@task
*_countMembersTask(filter) {
if (!filter) {
*_countMembersTask(query) {
if (!query) {
return 0;
}
try {
const result = yield this.store.query('member', {filter, limit: 1, page: 1});
const result = yield this.store.query('member', {...query, limit: 1, page: 1});
return result.meta.pagination.total;
} catch (e) {
console.error(e); // eslint-disable-line