Skip to content

Commit

Permalink
chore: cleanup store
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jul 6, 2024
1 parent a71d111 commit 58b55e3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 55 deletions.
8 changes: 2 additions & 6 deletions src/main/menu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
MenuItem,
MenuItemConstructorOptions,
} from "electron"
import type { MenuItem, MenuItemConstructorOptions } from "electron"
import { Menu } from "electron"

import { createSettingWindow, createWindow } from "./window"
Expand All @@ -16,8 +13,7 @@ export const registerAppMenu = () => {
{
label: "Settings...",
accelerator: "CmdOrCtrl+,",
click: createSettingWindow
,
click: () => createSettingWindow(),
},
{ type: "separator" },
{ role: "services" },
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { hydrateDatabaseToStore, setHydrated } from "./store/utils/hydrate"

const cleanup = subscribeShouldUseIndexedDB((value) => {
if (!value) {
browserDB.delete()
browserDB.tables.forEach((table) => {
table.clear()
})
setHydrated(false)
return
}
Expand All @@ -31,7 +33,7 @@ export const initializeApp = async () => {
const now = Date.now()

registerGlobalContext({
showSetting: window.router.showSettings,
showSetting: () => window.router.showSettings(),
toast,
})

Expand Down
4 changes: 1 addition & 3 deletions src/renderer/src/store/entry/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const createState = (): EntryState => ({

export const useEntryStore = createZustandStore<EntryState & EntryActions>(
"entry",
{
version: 1,
},

)((set, get) => ({
...createState(),

Expand Down
4 changes: 1 addition & 3 deletions src/renderer/src/store/feed/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import type { FeedActions, FeedState } from "./types"

export const useFeedStore = createZustandStore<FeedState & FeedActions>(
"feed",
{
version: 1,
},

)((set) => ({
feeds: {},
clear() {
Expand Down
4 changes: 1 addition & 3 deletions src/renderer/src/store/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ const emptyDataIdByView: Record<FeedViewType, FeedId[]> = {

export const useSubscriptionStore = createZustandStore<
SubscriptionState & SubscriptionActions
>("subscription", {
version: 1,
})((set, get) => ({
>("subscription")((set, get) => ({
data: {},
dataIdByView: { ...emptyDataIdByView },

Expand Down
4 changes: 1 addition & 3 deletions src/renderer/src/store/unread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ interface FeedUnreadActions {
*/
export const useFeedUnreadStore = createZustandStore<
UnreadState & FeedUnreadActions
>("unread", {
version: 1,
})((set, get) => ({
>("unread")((set, get) => ({
data: {},

internal_reset() {
Expand Down
56 changes: 21 additions & 35 deletions src/renderer/src/store/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { StateCreator, StoreApi } from "zustand"
import type {
PersistOptions,
PersistStorage,
} from "zustand/middleware"
import { persist } from "zustand/middleware"
import type { StateCreator } from "zustand"
import type { PersistStorage } from "zustand/middleware"
import { shallow } from "zustand/shallow"
import type { UseBoundStoreWithEqualityFn } from "zustand/traditional"
import { createWithEqualityFn } from "zustand/traditional"
Expand All @@ -27,41 +23,31 @@ export const localStorage: PersistStorage<any> = {
},
}

const storeMap = {} as Record<string, UseBoundStoreWithEqualityFn<any>>

export const createZustandStore =
<
S,
T extends StateCreator<
S,
[["zustand/persist", unknown]],
[]
> = StateCreator<S, [["zustand/persist", unknown]], []>,
>(
<S, T extends StateCreator<S, [], []> = StateCreator<S, [], []>>(
name: string,
options?: Partial<PersistOptions<S> & {
persist?: boolean
}>,
) =>
(store: T) => {
const newStore = options?.persist ?
createWithEqualityFn(
persist<S>(store, {
name,
storage: localStorage,
...options,
}),
shallow,
) :
createWithEqualityFn(store as any, shallow)
const newStore = createWithEqualityFn(store, shallow)

window.store = window.store || {}
Object.assign(window.store, {
[name]() {
return newStore.getState()
storeMap[name] = newStore
window.store =
window.store ||
new Proxy(
{},
{
get(_, prop) {
if (prop in storeMap) {
return storeMap[prop as string].getState()
}
return
},
},
})
return newStore as unknown as UseBoundStoreWithEqualityFn<StoreApi<
S
>>
)

return newStore
}
type FunctionKeys<T> = {
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
Expand Down

0 comments on commit 58b55e3

Please sign in to comment.