IssueListCacheStore.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import isEqual from 'lodash/isEqual';
  2. import {createStore} from 'reflux';
  3. import type {Group} from 'sentry/types/group';
  4. import type {StrictStoreDefinition} from './types';
  5. /**
  6. * The type here doesn't really matter it just needs to be compared via isEqual
  7. */
  8. type LooseParamsType = Record<string, any>;
  9. interface IssueListCache {
  10. groups: Group[];
  11. pageLinks: string;
  12. queryCount: number;
  13. queryMaxCount: number;
  14. }
  15. interface IssueListCacheState {
  16. /**
  17. * The data that was cached
  18. */
  19. cache: IssueListCache;
  20. /**
  21. * Do not use this directly, use `getFromCache` instead
  22. */
  23. expiration: number;
  24. /**
  25. * The params that were used to generate the cache
  26. * eg - {query: 'Some query'}
  27. */
  28. params: LooseParamsType;
  29. }
  30. // 30 seconds
  31. const CACHE_EXPIRATION = 30 * 1000;
  32. interface IssueListCacheStoreDefinition
  33. extends StrictStoreDefinition<IssueListCacheState | null> {
  34. getFromCache(params: LooseParamsType): IssueListCache | null;
  35. reset(): void;
  36. save(params: LooseParamsType, data: IssueListCache): void;
  37. }
  38. const storeConfig: IssueListCacheStoreDefinition = {
  39. state: null,
  40. init() {},
  41. reset() {
  42. this.state = null;
  43. },
  44. save(params: LooseParamsType, data: IssueListCache) {
  45. this.state = {
  46. params,
  47. cache: data,
  48. expiration: Date.now() + CACHE_EXPIRATION,
  49. };
  50. },
  51. getFromCache(params: LooseParamsType) {
  52. if (
  53. this.state &&
  54. this.state.expiration > Date.now() &&
  55. isEqual(this.state?.params, params)
  56. ) {
  57. return this.state.cache;
  58. }
  59. return null;
  60. },
  61. getState() {
  62. return this.state;
  63. },
  64. };
  65. const IssueListCacheStore = createStore(storeConfig);
  66. export default IssueListCacheStore;