-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Issue/#45 fix stop fetching
- Loading branch information
Showing
7 changed files
with
138 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { defineStore } from "pinia"; | ||
import { EventId, ServerEvent, TEventGroup, TEventType } from "~/config/types"; | ||
import { ALL_EVENTS, EVENT_TYPES, LOCAL_STORAGE_KEYS } from "~/config/constants"; | ||
import { useEventStore } from "~/stores/events"; | ||
|
||
|
||
type TCachedEventsEmptyMap = Record<TEventType, EventId[]>; | ||
|
||
const initialcachedIds: TCachedEventsEmptyMap = { | ||
[EVENT_TYPES.SENTRY]: [] as EventId[], | ||
[EVENT_TYPES.INSPECTOR]: [] as EventId[], | ||
[EVENT_TYPES.PROFILER]: [] as EventId[], | ||
[EVENT_TYPES.SMTP]: [] as EventId[], | ||
[EVENT_TYPES.RAY_DUMP]: [] as EventId[], | ||
[EVENT_TYPES.VAR_DUMP]: [] as EventId[], | ||
[EVENT_TYPES.HTTP_DUMP]: [] as EventId[], | ||
[ALL_EVENTS]: [] as EventId[], | ||
}; | ||
|
||
const { localStorage } = window; | ||
const getCachedIds = (): TCachedEventsEmptyMap => { | ||
const storageValue = localStorage?.getItem(LOCAL_STORAGE_KEYS.CACHED_EVENTS); | ||
|
||
if (storageValue) { | ||
return JSON.parse(storageValue) as TCachedEventsEmptyMap; | ||
} | ||
|
||
return initialcachedIds; | ||
}; | ||
|
||
const syncLocalStorage = (cachedEventMap: TCachedEventsEmptyMap) => { | ||
localStorage?.setItem(LOCAL_STORAGE_KEYS.CACHED_EVENTS, JSON.stringify(cachedEventMap)); | ||
} | ||
|
||
export const useCachedIdsStore = defineStore("useCachedIdsStore", { | ||
state: () => ({ | ||
cachedIds: getCachedIds(), | ||
}), | ||
getters: { | ||
cachedTypesList(state) { | ||
return Object.entries(state.cachedIds).filter(([_, value]) => value.length > 0).map(([key]) => key) | ||
} | ||
}, | ||
actions: { | ||
setByType(cachedType: TEventGroup) { | ||
const { events } = useEventStore(); | ||
|
||
events | ||
.filter(({ type }) => | ||
type === cachedType || cachedType === ALL_EVENTS | ||
) | ||
.forEach((event) => { | ||
this.cachedIds[cachedType].push(event.uuid); | ||
}); | ||
|
||
syncLocalStorage(this.cachedIds); | ||
}, | ||
removeByType(type: TEventGroup) { | ||
this.cachedIds[type].length = 0; | ||
syncLocalStorage(this.cachedIds); | ||
}, | ||
removeById(eventUuid: EventId) { | ||
this.cachedTypesList.forEach((type) => { | ||
this.cachedIds[type] = this.cachedIds[type].filter( | ||
(uuid: EventId) => uuid !== eventUuid | ||
); | ||
}); | ||
|
||
syncLocalStorage(this.cachedIds); | ||
}, | ||
removeAll() { | ||
this.cachedIds = initialcachedIds; | ||
syncLocalStorage(this.cachedIds); | ||
}, | ||
syncWithActive(activeIds: EventId[]) { | ||
if (!activeIds.length) { | ||
this.removeAll(); | ||
|
||
return; | ||
} | ||
|
||
this.cachedTypesList.forEach((type) => { | ||
this.cachedIds[type] = this.cachedIds[type].filter( | ||
(uuid: EventId) => activeIds.includes(uuid) | ||
); | ||
}); | ||
|
||
syncLocalStorage(this.cachedIds); | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,38 @@ | ||
import { defineStore } from "pinia"; | ||
import { | ||
EventId, | ||
OneOfValues, | ||
ServerEvent, | ||
} from "~/config/types"; | ||
import { ALL_EVENTS, EVENT_TYPES, LOCAL_STORAGE_KEYS } from "~/config/constants"; | ||
import { EventId, ServerEvent, TEventType } from "~/config/types"; | ||
|
||
type TCachedEventsEmptyMap = Record<OneOfValues<typeof EVENT_TYPES>, EventId[]>; | ||
|
||
const initialCachedEventsIdsMap: TCachedEventsEmptyMap = { | ||
[EVENT_TYPES.SENTRY]: [] as EventId[], | ||
[EVENT_TYPES.INSPECTOR]: [] as EventId[], | ||
[EVENT_TYPES.PROFILER]: [] as EventId[], | ||
[EVENT_TYPES.SMTP]: [] as EventId[], | ||
[EVENT_TYPES.RAY_DUMP]: [] as EventId[], | ||
[EVENT_TYPES.VAR_DUMP]: [] as EventId[], | ||
[EVENT_TYPES.HTTP_DUMP]: [] as EventId[], | ||
[ALL_EVENTS]: [] as EventId[], | ||
}; | ||
|
||
const { localStorage } = window; | ||
const getCachedEventsIdsMap = (): TCachedEventsEmptyMap => { | ||
const storageValue = localStorage?.getItem(LOCAL_STORAGE_KEYS.CACHED_EVENTS); | ||
|
||
if (storageValue) { | ||
return JSON.parse(storageValue) as TCachedEventsEmptyMap; | ||
} | ||
|
||
return initialCachedEventsIdsMap; | ||
}; | ||
|
||
export const useEventStore = defineStore("useEventStore", { | ||
state: () => ({ | ||
events: [] as ServerEvent<unknown>[], | ||
cachedEventsIdsMap: getCachedEventsIdsMap(), | ||
}), | ||
actions: { | ||
addEvents(events: ServerEvent<unknown>[]) { | ||
addList(events: ServerEvent<unknown>[]) { | ||
events.forEach((event) => { | ||
const isExistedEvent = this.events.some((el) => el.uuid === event.uuid); | ||
if (!isExistedEvent) { | ||
this.events.unshift(event); | ||
} else { | ||
this.events = this.events.map((el) => { | ||
if (el.uuid === event.uuid) { | ||
return event; | ||
if (el.uuid !== event.uuid) { | ||
return el; // add new | ||
} | ||
return el; | ||
|
||
return event; // replace existed | ||
}); | ||
} | ||
}); | ||
}, | ||
removeEvents() { | ||
removeAll() { | ||
this.events.length = 0; | ||
|
||
this.cachedEventsIdsMap = initialCachedEventsIdsMap; | ||
}, | ||
removeEventById(eventUuid: EventId) { | ||
const eventType = this.events.find( | ||
({ uuid }) => uuid === eventUuid | ||
)?.type; | ||
|
||
if (eventType) { | ||
this.cachedEventsIdsMap[eventType] = this.cachedEventsIdsMap[ | ||
eventType | ||
].filter((uuid: EventId) => uuid !== eventUuid); | ||
} | ||
|
||
if (this.cachedEventsIdsMap[ALL_EVENTS].length) { | ||
this.cachedEventsIdsMap[ALL_EVENTS] = this.cachedEventsIdsMap[ | ||
ALL_EVENTS | ||
].filter((uuid: EventId) => uuid !== eventUuid); | ||
} | ||
|
||
removeById(eventUuid: EventId) { | ||
this.events = this.events.filter(({ uuid }) => uuid !== eventUuid); | ||
}, | ||
removeEventsByType(eventType: OneOfValues<typeof EVENT_TYPES>) { | ||
this.cachedEventsIdsMap[eventType].length = 0; | ||
removeByType(eventType: TEventType) { | ||
this.events = this.events.filter(({ type }) => type !== eventType); | ||
}, | ||
|
||
setCachedEvents( | ||
eventType: OneOfValues<typeof EVENT_TYPES | typeof ALL_EVENTS> | ||
) { | ||
this.events | ||
.filter(({ type }) => | ||
eventType === ALL_EVENTS ? true : type === eventType | ||
) | ||
.forEach((event) => { | ||
this.cachedEventsIdsMap[eventType].push(event.uuid); | ||
}); | ||
|
||
localStorage?.setItem( | ||
LOCAL_STORAGE_KEYS.CACHED_EVENTS, | ||
JSON.stringify(this.cachedEventsIdsMap) | ||
); | ||
}, | ||
removeCachedEvents( | ||
eventType: OneOfValues<typeof EVENT_TYPES | typeof ALL_EVENTS> | ||
) { | ||
this.cachedEventsIdsMap[eventType].length = 0; | ||
|
||
localStorage?.setItem( | ||
LOCAL_STORAGE_KEYS.CACHED_EVENTS, | ||
JSON.stringify(this.cachedEventsIdsMap) | ||
); | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.