|
1 | 1 | import { defineStore } from "pinia"; |
2 | | -import { |
3 | | - EventId, |
4 | | - OneOfValues, |
5 | | - ServerEvent, |
6 | | -} from "~/config/types"; |
7 | | -import { ALL_EVENTS, EVENT_TYPES, LOCAL_STORAGE_KEYS } from "~/config/constants"; |
| 2 | +import { EventId, ServerEvent, TEventType } from "~/config/types"; |
8 | 3 |
|
9 | | -type TCachedEventsEmptyMap = Record<OneOfValues<typeof EVENT_TYPES>, EventId[]>; |
10 | 4 |
|
11 | | -const initialCachedEventsIdsMap: TCachedEventsEmptyMap = { |
12 | | - [EVENT_TYPES.SENTRY]: [] as EventId[], |
13 | | - [EVENT_TYPES.INSPECTOR]: [] as EventId[], |
14 | | - [EVENT_TYPES.PROFILER]: [] as EventId[], |
15 | | - [EVENT_TYPES.SMTP]: [] as EventId[], |
16 | | - [EVENT_TYPES.RAY_DUMP]: [] as EventId[], |
17 | | - [EVENT_TYPES.VAR_DUMP]: [] as EventId[], |
18 | | - [EVENT_TYPES.HTTP_DUMP]: [] as EventId[], |
19 | | - [ALL_EVENTS]: [] as EventId[], |
20 | | -}; |
21 | | - |
22 | | -const { localStorage } = window; |
23 | | -const getCachedEventsIdsMap = (): TCachedEventsEmptyMap => { |
24 | | - const storageValue = localStorage?.getItem(LOCAL_STORAGE_KEYS.CACHED_EVENTS); |
25 | | - |
26 | | - if (storageValue) { |
27 | | - return JSON.parse(storageValue) as TCachedEventsEmptyMap; |
28 | | - } |
29 | | - |
30 | | - return initialCachedEventsIdsMap; |
31 | | -}; |
32 | 5 |
|
33 | 6 | export const useEventStore = defineStore("useEventStore", { |
34 | 7 | state: () => ({ |
35 | 8 | events: [] as ServerEvent<unknown>[], |
36 | | - cachedEventsIdsMap: getCachedEventsIdsMap(), |
37 | 9 | }), |
38 | 10 | actions: { |
39 | | - addEvents(events: ServerEvent<unknown>[]) { |
| 11 | + addList(events: ServerEvent<unknown>[]) { |
40 | 12 | events.forEach((event) => { |
41 | 13 | const isExistedEvent = this.events.some((el) => el.uuid === event.uuid); |
42 | 14 | if (!isExistedEvent) { |
43 | 15 | this.events.unshift(event); |
44 | 16 | } else { |
45 | 17 | this.events = this.events.map((el) => { |
46 | | - if (el.uuid === event.uuid) { |
47 | | - return event; |
| 18 | + if (el.uuid !== event.uuid) { |
| 19 | + return el; // add new |
48 | 20 | } |
49 | | - return el; |
| 21 | + |
| 22 | + return event; // replace existed |
50 | 23 | }); |
51 | 24 | } |
52 | 25 | }); |
53 | 26 | }, |
54 | | - removeEvents() { |
| 27 | + removeAll() { |
55 | 28 | this.events.length = 0; |
56 | | - |
57 | | - this.cachedEventsIdsMap = initialCachedEventsIdsMap; |
58 | 29 | }, |
59 | | - removeEventById(eventUuid: EventId) { |
60 | | - const eventType = this.events.find( |
61 | | - ({ uuid }) => uuid === eventUuid |
62 | | - )?.type; |
63 | | - |
64 | | - if (eventType) { |
65 | | - this.cachedEventsIdsMap[eventType] = this.cachedEventsIdsMap[ |
66 | | - eventType |
67 | | - ].filter((uuid: EventId) => uuid !== eventUuid); |
68 | | - } |
69 | | - |
70 | | - if (this.cachedEventsIdsMap[ALL_EVENTS].length) { |
71 | | - this.cachedEventsIdsMap[ALL_EVENTS] = this.cachedEventsIdsMap[ |
72 | | - ALL_EVENTS |
73 | | - ].filter((uuid: EventId) => uuid !== eventUuid); |
74 | | - } |
75 | | - |
| 30 | + removeById(eventUuid: EventId) { |
76 | 31 | this.events = this.events.filter(({ uuid }) => uuid !== eventUuid); |
77 | 32 | }, |
78 | | - removeEventsByType(eventType: OneOfValues<typeof EVENT_TYPES>) { |
79 | | - this.cachedEventsIdsMap[eventType].length = 0; |
| 33 | + removeByType(eventType: TEventType) { |
80 | 34 | this.events = this.events.filter(({ type }) => type !== eventType); |
81 | 35 | }, |
82 | 36 |
|
83 | | - setCachedEvents( |
84 | | - eventType: OneOfValues<typeof EVENT_TYPES | typeof ALL_EVENTS> |
85 | | - ) { |
86 | | - this.events |
87 | | - .filter(({ type }) => |
88 | | - eventType === ALL_EVENTS ? true : type === eventType |
89 | | - ) |
90 | | - .forEach((event) => { |
91 | | - this.cachedEventsIdsMap[eventType].push(event.uuid); |
92 | | - }); |
93 | | - |
94 | | - localStorage?.setItem( |
95 | | - LOCAL_STORAGE_KEYS.CACHED_EVENTS, |
96 | | - JSON.stringify(this.cachedEventsIdsMap) |
97 | | - ); |
98 | | - }, |
99 | | - removeCachedEvents( |
100 | | - eventType: OneOfValues<typeof EVENT_TYPES | typeof ALL_EVENTS> |
101 | | - ) { |
102 | | - this.cachedEventsIdsMap[eventType].length = 0; |
103 | | - |
104 | | - localStorage?.setItem( |
105 | | - LOCAL_STORAGE_KEYS.CACHED_EVENTS, |
106 | | - JSON.stringify(this.cachedEventsIdsMap) |
107 | | - ); |
108 | | - }, |
109 | 37 | }, |
110 | 38 | }); |
0 commit comments