From dbb45e7253e67fa9538525815a8cc1fe707fbf02 Mon Sep 17 00:00:00 2001 From: Akash K <57758277+amk-dev@users.noreply.github.com> Date: Thu, 30 Mar 2023 15:13:25 +0530 Subject: [PATCH] chore: add removeDuplicateEntry dispatcher to history store (#2955) --- .../hoppscotch-common/src/newstore/history.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/packages/hoppscotch-common/src/newstore/history.ts b/packages/hoppscotch-common/src/newstore/history.ts index 6943651c1..e4e5d53d9 100644 --- a/packages/hoppscotch-common/src/newstore/history.ts +++ b/packages/hoppscotch-common/src/newstore/history.ts @@ -163,6 +163,20 @@ const RESTHistoryDispatchers = defineDispatchers({ }), } }, + // only used for history.sync.ts to prevent double insertion of history entries from storeSync and Subscriptions + removeDuplicateEntry(currentVal: RESTHistoryType, { id }: { id: string }) { + const entries = currentVal.state.filter((e) => e.id === id) + + if (entries.length == 2) { + const indexToRemove = currentVal.state.findIndex((e) => e.id === id) + + currentVal.state.splice(indexToRemove, 1) + } + + return { + state: currentVal.state, + } + }, }) const GQLHistoryDispatchers = defineDispatchers({ @@ -212,6 +226,20 @@ const GQLHistoryDispatchers = defineDispatchers({ }), } }, + // only used for history.sync.ts to prevent double insertion of history entries from storeSync and Subscriptions + removeDuplicateEntry(currentVal: GraphqlHistoryType, { id }: { id: string }) { + const entries = currentVal.state.filter((e) => e.id === id) + + if (entries.length == 2) { + const indexToRemove = currentVal.state.findIndex((e) => e.id === id) + + currentVal.state.splice(indexToRemove, 1) + } + + return { + state: currentVal.state, + } + }, }) export const restHistoryStore = new DispatchingStore( @@ -297,6 +325,20 @@ export function toggleGraphqlHistoryEntryStar(entry: GQLHistoryEntry) { }) } +export function removeDuplicateRestHistoryEntry(id: string) { + restHistoryStore.dispatch({ + dispatcher: "removeDuplicateEntry", + payload: { id }, + }) +} + +export function removeDuplicateGraphqlHistoryEntry(id: string) { + graphqlHistoryStore.dispatch({ + dispatcher: "removeDuplicateEntry", + payload: { id }, + }) +} + // Listen to completed responses to add to history completedRESTResponse$.subscribe((res) => { if (res !== null) {