@@ -13,12 +13,9 @@ import {
1313} from "~/config/types" ;
1414import { ALL_EVENTS , EVENT_TYPES } from "~/config/constants" ;
1515
16- type TCachedEventsEmptyMap = Record <
17- OneOfValues < typeof EVENT_TYPES > ,
18- EventId [ ]
19- > ;
16+ type TCachedEventsEmptyMap = Record < OneOfValues < typeof EVENT_TYPES > , EventId [ ] > ;
2017
21- const initialcachedEventsIdsMap : TCachedEventsEmptyMap = {
18+ const initialCachedEventsIdsMap : TCachedEventsEmptyMap = {
2219 [ EVENT_TYPES . SENTRY ] : [ ] as EventId [ ] ,
2320 [ EVENT_TYPES . INSPECTOR ] : [ ] as EventId [ ] ,
2421 [ EVENT_TYPES . PROFILER ] : [ ] as EventId [ ] ,
@@ -29,12 +26,43 @@ const initialcachedEventsIdsMap: TCachedEventsEmptyMap = {
2926 [ ALL_EVENTS ] : [ ] as EventId [ ] ,
3027} ;
3128
29+ const { localStorage } = window ;
30+ const getCachedEventsIdsMap = ( ) : TCachedEventsEmptyMap => {
31+ const storageValue = localStorage ?. getItem ( "cached_events" ) ;
32+
33+ if ( storageValue ) {
34+ return JSON . parse ( storageValue ) as TCachedEventsEmptyMap ;
35+ }
36+
37+ return initialCachedEventsIdsMap ;
38+ } ;
39+
3240export const useEventStore = defineStore ( "useEventStore" , {
3341 state : ( ) => ( {
3442 events : [ ] as ServerEvent < unknown > [ ] ,
35- cachedEventsIdsMap : initialcachedEventsIdsMap ,
43+ cachedEventsIdsMap : getCachedEventsIdsMap ( ) ,
3644 } ) ,
3745 actions : {
46+ addEvents ( events : ServerEvent < unknown > [ ] ) {
47+ events . forEach ( ( event ) => {
48+ const isExistedEvent = this . events . some ( ( el ) => el . uuid === event . uuid ) ;
49+ if ( ! isExistedEvent ) {
50+ this . events . unshift ( event ) ;
51+ } else {
52+ this . events = this . events . map ( ( el ) => {
53+ if ( el . uuid === event . uuid ) {
54+ return event ;
55+ }
56+ return el ;
57+ } ) ;
58+ }
59+ } ) ;
60+ } ,
61+ removeEvents ( ) {
62+ this . events . length = 0 ;
63+
64+ this . cachedEventsIdsMap = initialCachedEventsIdsMap ;
65+ } ,
3866 removeEventById ( eventUuid : EventId ) {
3967 const eventType = this . events . find (
4068 ( { uuid } ) => uuid === eventUuid
@@ -43,52 +71,47 @@ export const useEventStore = defineStore("useEventStore", {
4371 if ( eventType ) {
4472 this . cachedEventsIdsMap [ eventType ] = this . cachedEventsIdsMap [
4573 eventType
46- ] . filter ( ( uuid ) => uuid !== eventUuid ) ;
74+ ] . filter ( ( uuid : EventId ) => uuid !== eventUuid ) ;
4775 }
4876
4977 if ( this . cachedEventsIdsMap [ ALL_EVENTS ] . length ) {
5078 this . cachedEventsIdsMap [ ALL_EVENTS ] = this . cachedEventsIdsMap [
5179 ALL_EVENTS
52- ] . filter ( ( uuid ) => uuid !== eventUuid ) ;
80+ ] . filter ( ( uuid : EventId ) => uuid !== eventUuid ) ;
5381 }
5482
5583 this . events = this . events . filter ( ( { uuid } ) => uuid !== eventUuid ) ;
5684 } ,
57- removeEvents ( ) {
58- this . events . length = 0 ;
59-
60- this . cachedEventsIdsMap = initialcachedEventsIdsMap ;
61- } ,
6285 removeEventsByType ( eventType : OneOfValues < typeof EVENT_TYPES > ) {
6386 this . cachedEventsIdsMap [ eventType ] . length = 0 ;
6487 this . events = this . events . filter ( ( { type } ) => type !== eventType ) ;
6588 } ,
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 > ) {
89+
90+ setCachedEvents (
91+ eventType : OneOfValues < typeof EVENT_TYPES | typeof ALL_EVENTS >
92+ ) {
8293 this . events
8394 . filter ( ( { type } ) =>
8495 eventType === ALL_EVENTS ? true : type === eventType
8596 )
8697 . forEach ( ( event ) => {
8798 this . cachedEventsIdsMap [ eventType ] . push ( event . uuid ) ;
8899 } ) ;
100+
101+ localStorage ?. setItem (
102+ "cached_events" ,
103+ JSON . stringify ( this . cachedEventsIdsMap )
104+ ) ;
89105 } ,
90- removeCachedEvents ( eventType : OneOfValues < typeof EVENT_TYPES | typeof ALL_EVENTS > ) {
106+ removeCachedEvents (
107+ eventType : OneOfValues < typeof EVENT_TYPES | typeof ALL_EVENTS >
108+ ) {
91109 this . cachedEventsIdsMap [ eventType ] . length = 0 ;
110+
111+ localStorage ?. setItem (
112+ "cached_events" ,
113+ JSON . stringify ( this . cachedEventsIdsMap )
114+ ) ;
92115 } ,
93116 } ,
94117} ) ;
0 commit comments