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