externalIssueStore.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {createStore} from 'reflux';
  2. import {CommonStoreDefinition} from 'sentry/stores/types';
  3. import {PlatformExternalIssue} from 'sentry/types';
  4. interface ExternalIssueStoreDefinition
  5. extends CommonStoreDefinition<PlatformExternalIssue[]> {
  6. add(issue: PlatformExternalIssue): void;
  7. getInitialState(): PlatformExternalIssue[];
  8. load(items: PlatformExternalIssue[]): void;
  9. }
  10. const storeConfig: ExternalIssueStoreDefinition = {
  11. init() {
  12. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  13. // listeners due to their leaky nature in tests.
  14. this.items = [];
  15. },
  16. getState() {
  17. return this.items;
  18. },
  19. getInitialState(): PlatformExternalIssue[] {
  20. return this.items;
  21. },
  22. load(items: PlatformExternalIssue[]) {
  23. this.items = items;
  24. this.trigger(items);
  25. },
  26. get(id: string) {
  27. return this.items.find((item: PlatformExternalIssue) => item.id === id);
  28. },
  29. getAll() {
  30. return this.items;
  31. },
  32. add(issue: PlatformExternalIssue) {
  33. if (!this.items.some(i => i.id === issue.id)) {
  34. this.items = this.items.concat([issue]);
  35. this.trigger(this.items);
  36. }
  37. },
  38. };
  39. const ExternalIssueStore = createStore(storeConfig);
  40. export default ExternalIssueStore;