From 7afdeced89d8450f1858046c17102b135e7f159a Mon Sep 17 00:00:00 2001 From: goosewobbler Date: Sun, 8 Dec 2024 18:18:27 +0000 Subject: [PATCH] refactor: standardise apps --- apps/example-reducers/src/features/index.ts | 14 +++----------- apps/example-reducers/src/main/store.ts | 17 ++++++++++++++--- apps/example-reducers/src/main/tray/index.ts | 8 +++++--- apps/example-reducers/src/preload/index.ts | 6 +++--- apps/example-reducers/src/renderer/App.tsx | 4 ++-- .../src/renderer/hooks/useStore.ts | 5 +++-- .../src/features/counter/index.ts | 2 +- .../src/features/index.ts | 16 +++++----------- .../example-separate-handlers/src/main/store.ts | 15 ++++++++++++--- .../src/main/tray/index.ts | 8 +++++--- .../src/preload/index.ts | 6 +++--- .../src/renderer/App.tsx | 4 ++-- .../src/renderer/hooks/useStore.ts | 5 +++-- .../src/features/counter/index.ts | 2 +- .../src/features/index.ts | 9 +-------- 15 files changed, 63 insertions(+), 58 deletions(-) diff --git a/apps/example-reducers/src/features/index.ts b/apps/example-reducers/src/features/index.ts index c2a2459..41b7411 100644 --- a/apps/example-reducers/src/features/index.ts +++ b/apps/example-reducers/src/features/index.ts @@ -2,16 +2,8 @@ import type { Reducer } from 'zutron'; import { counterReducer } from './counter/index.js'; -export const rootReducer: Reducer = (state, action) => ({ +export type AppState = { counter: number }; + +export const rootReducer: Reducer = (state, action) => ({ counter: counterReducer(state.counter, action), }); - -export type Subscribe = (listener: (state: State, prevState: State) => void) => () => void; -export type Handlers = Record void>; -export type State = { counter: number }; -export type Store = { - getState: () => State; - getInitialState: () => State; - setState: (stateSetter: (state: State) => State) => void; - subscribe: Subscribe; -}; diff --git a/apps/example-reducers/src/main/store.ts b/apps/example-reducers/src/main/store.ts index f7c8bc1..a4ee97e 100644 --- a/apps/example-reducers/src/main/store.ts +++ b/apps/example-reducers/src/main/store.ts @@ -1,7 +1,18 @@ import { createStore } from 'zustand/vanilla'; -import type { State } from '../features/index.js'; +import type { AppState } from '../features/index.js'; -export const store = createStore()(() => ({ +type Subscribe = (listener: (state: AppState, prevState: AppState) => void) => () => void; + +export type Store = { + getState: () => AppState; + getInitialState: () => AppState; + setState: (stateSetter: (state: AppState) => AppState) => void; + subscribe: Subscribe; +}; + +const initialState: AppState = { counter: 0, -})); +}; + +export const store = createStore()(() => initialState); diff --git a/apps/example-reducers/src/main/tray/index.ts b/apps/example-reducers/src/main/tray/index.ts index c310c6b..bbdc44c 100644 --- a/apps/example-reducers/src/main/tray/index.ts +++ b/apps/example-reducers/src/main/tray/index.ts @@ -1,8 +1,10 @@ import { type BrowserWindow, Menu, Tray, app, nativeImage } from 'electron'; import { createDispatch } from 'zutron/main'; + import trayIconFile from '../../../../../resources/trayIcon.png'; -import { rootReducer, type State, type Store } from '../../features/index.js'; +import type { Store } from '../store.js'; +import { rootReducer, type AppState } from '../../features/index.js'; const trayIcon = nativeImage.createFromDataURL(trayIconFile).resize({ width: 18, @@ -14,7 +16,7 @@ class SystemTray { private electronTray?: Tray; private window?: BrowserWindow; - private update = (state: State) => { + private update = (state: AppState) => { if (!this.dispatch) { return; } @@ -57,7 +59,7 @@ class SystemTray { public init = (store: Store, window: BrowserWindow) => { this.window = window; - this.dispatch = createDispatch(store, { reducer: rootReducer }); + this.dispatch = createDispatch(store, { reducer: rootReducer }); this.update(store.getState()); store.subscribe(() => this.update(store.getState())); }; diff --git a/apps/example-reducers/src/preload/index.ts b/apps/example-reducers/src/preload/index.ts index 32cb33d..52a1b75 100644 --- a/apps/example-reducers/src/preload/index.ts +++ b/apps/example-reducers/src/preload/index.ts @@ -4,14 +4,14 @@ import { preloadZustandBridge } from 'zutron/preload'; import 'wdio-electron-service/preload'; import type { Handlers } from 'zutron'; -import type { State } from '../features/index.js'; +import type { AppState } from '../features/index.js'; -export const { handlers } = preloadZustandBridge(); +export const { handlers } = preloadZustandBridge(); contextBridge.exposeInMainWorld('zutron', handlers); declare global { interface Window { - zutron: Handlers; + zutron: Handlers; } } diff --git a/apps/example-reducers/src/renderer/App.tsx b/apps/example-reducers/src/renderer/App.tsx index c20ae60..1540bf8 100644 --- a/apps/example-reducers/src/renderer/App.tsx +++ b/apps/example-reducers/src/renderer/App.tsx @@ -1,10 +1,10 @@ import { useDispatch } from 'zutron'; import { useStore } from './hooks/useStore.js'; -import type { State } from '../features/index.js'; +import type { AppState } from '../features/index.js'; export const App = () => { - const counter = useStore((x: State) => x.counter); + const counter = useStore((x: AppState) => x.counter); const dispatch = useDispatch(window.zutron); return ( diff --git a/apps/example-reducers/src/renderer/hooks/useStore.ts b/apps/example-reducers/src/renderer/hooks/useStore.ts index bde9fb5..200c825 100644 --- a/apps/example-reducers/src/renderer/hooks/useStore.ts +++ b/apps/example-reducers/src/renderer/hooks/useStore.ts @@ -1,4 +1,5 @@ import { createUseStore } from 'zutron'; -import type { State } from '../../features/index.js'; -export const useStore = createUseStore(window.zutron); +import type { AppState } from '../../features/index.js'; + +export const useStore = createUseStore(window.zutron); diff --git a/apps/example-separate-handlers/src/features/counter/index.ts b/apps/example-separate-handlers/src/features/counter/index.ts index 107f4cd..9eac2c5 100644 --- a/apps/example-separate-handlers/src/features/counter/index.ts +++ b/apps/example-separate-handlers/src/features/counter/index.ts @@ -1,4 +1,4 @@ -import { type Store } from '..'; +import type { Store } from '../../main/store.js'; export const handlers = (store: Store) => ({ 'COUNTER:INCREMENT': () => store.setState((state) => ({ counter: state.counter + 1 })), diff --git a/apps/example-separate-handlers/src/features/index.ts b/apps/example-separate-handlers/src/features/index.ts index fdea9fa..003ad25 100644 --- a/apps/example-separate-handlers/src/features/index.ts +++ b/apps/example-separate-handlers/src/features/index.ts @@ -1,16 +1,10 @@ import { handlers as counterHandlers } from './counter/index.js'; +import type { Store } from '../main/store.js'; -export const actionHandlers = (store: Store, initialState: State) => ({ +export type Handlers = Record void>; +export type AppState = { counter: number }; + +export const actionHandlers = (store: Store, initialState: AppState) => ({ ...counterHandlers(store), 'STORE:RESET': () => store.setState(() => initialState), }); - -export type Subscribe = (listener: (state: State, prevState: State) => void) => () => void; -export type Handlers = Record void>; -export type State = { counter: number }; -export type Store = { - getState: () => State; - getInitialState: () => State; - setState: (stateSetter: (state: State) => State) => void; - subscribe: Subscribe; -}; diff --git a/apps/example-separate-handlers/src/main/store.ts b/apps/example-separate-handlers/src/main/store.ts index 15a8298..8db6fb0 100644 --- a/apps/example-separate-handlers/src/main/store.ts +++ b/apps/example-separate-handlers/src/main/store.ts @@ -1,9 +1,18 @@ import { createStore } from 'zustand/vanilla'; -import type { State } from '../features/index.js'; +import type { AppState } from '../features/index.js'; -export const initialState = { +type Subscribe = (listener: (state: AppState, prevState: AppState) => void) => () => void; + +export type Store = { + getState: () => AppState; + getInitialState: () => AppState; + setState: (stateSetter: (state: AppState) => AppState) => void; + subscribe: Subscribe; +}; + +export const initialState: AppState = { counter: 0, }; -export const store = createStore()(() => initialState); +export const store = createStore()(() => initialState); diff --git a/apps/example-separate-handlers/src/main/tray/index.ts b/apps/example-separate-handlers/src/main/tray/index.ts index 72588eb..28925ec 100644 --- a/apps/example-separate-handlers/src/main/tray/index.ts +++ b/apps/example-separate-handlers/src/main/tray/index.ts @@ -1,8 +1,10 @@ import { type BrowserWindow, Menu, Tray, app, nativeImage } from 'electron'; import { createDispatch } from 'zutron/main'; + import trayIconFile from '../../../../../resources/trayIcon.png'; -import type { Handlers, State, Store } from '../../features/index.js'; +import type { Store } from '../store.js'; +import type { Handlers, AppState } from '../../features/index.js'; const trayIcon = nativeImage.createFromDataURL(trayIconFile).resize({ width: 18, @@ -14,7 +16,7 @@ class SystemTray { private electronTray?: Tray; private window?: BrowserWindow; - private update = (state: State) => { + private update = (state: AppState) => { if (!this.dispatch) { return; } @@ -57,7 +59,7 @@ class SystemTray { public init = (store: Store, window: BrowserWindow, actionHandlers: Handlers) => { this.window = window; - this.dispatch = createDispatch(store, { handlers: actionHandlers }); + this.dispatch = createDispatch(store, { handlers: actionHandlers }); this.update(store.getState()); store.subscribe(() => this.update(store.getState())); }; diff --git a/apps/example-separate-handlers/src/preload/index.ts b/apps/example-separate-handlers/src/preload/index.ts index b4cb2f4..a97fe9c 100644 --- a/apps/example-separate-handlers/src/preload/index.ts +++ b/apps/example-separate-handlers/src/preload/index.ts @@ -3,14 +3,14 @@ import { preloadZustandBridge } from 'zutron/preload'; import 'wdio-electron-service/preload'; import type { Handlers } from 'zutron'; -import type { State } from '../features/index.js'; +import type { AppState } from '../features/index.js'; -export const { handlers } = preloadZustandBridge(); +export const { handlers } = preloadZustandBridge(); contextBridge.exposeInMainWorld('zutron', handlers); declare global { interface Window { - zutron: Handlers; + zutron: Handlers; } } diff --git a/apps/example-separate-handlers/src/renderer/App.tsx b/apps/example-separate-handlers/src/renderer/App.tsx index c20ae60..1540bf8 100644 --- a/apps/example-separate-handlers/src/renderer/App.tsx +++ b/apps/example-separate-handlers/src/renderer/App.tsx @@ -1,10 +1,10 @@ import { useDispatch } from 'zutron'; import { useStore } from './hooks/useStore.js'; -import type { State } from '../features/index.js'; +import type { AppState } from '../features/index.js'; export const App = () => { - const counter = useStore((x: State) => x.counter); + const counter = useStore((x: AppState) => x.counter); const dispatch = useDispatch(window.zutron); return ( diff --git a/apps/example-separate-handlers/src/renderer/hooks/useStore.ts b/apps/example-separate-handlers/src/renderer/hooks/useStore.ts index bde9fb5..200c825 100644 --- a/apps/example-separate-handlers/src/renderer/hooks/useStore.ts +++ b/apps/example-separate-handlers/src/renderer/hooks/useStore.ts @@ -1,4 +1,5 @@ import { createUseStore } from 'zutron'; -import type { State } from '../../features/index.js'; -export const useStore = createUseStore(window.zutron); +import type { AppState } from '../../features/index.js'; + +export const useStore = createUseStore(window.zutron); diff --git a/apps/example-store-handlers/src/features/counter/index.ts b/apps/example-store-handlers/src/features/counter/index.ts index aeaaa55..6c87b31 100644 --- a/apps/example-store-handlers/src/features/counter/index.ts +++ b/apps/example-store-handlers/src/features/counter/index.ts @@ -1,4 +1,4 @@ -import type { Store } from '../index.js'; +import type { Store } from '../../main/store.js'; export const handlers = (setState: Store['setState']) => ({ 'COUNTER:INCREMENT': () => setState((state) => ({ counter: state.counter + 1 })), diff --git a/apps/example-store-handlers/src/features/index.ts b/apps/example-store-handlers/src/features/index.ts index 91da977..3e4fc8a 100644 --- a/apps/example-store-handlers/src/features/index.ts +++ b/apps/example-store-handlers/src/features/index.ts @@ -1,14 +1,7 @@ import { handlers as counterHandlers } from './counter/index.js'; +import type { Store } from '../main/store.js'; -export type Subscribe = (listener: (state: AppState, prevState: AppState) => void) => () => void; -export type Handlers = Record void>; export type AppState = { counter: number }; -export type Store = { - getState: () => AppState; - getInitialState: () => AppState; - setState: (stateSetter: (state: AppState) => AppState) => void; - subscribe: Subscribe; -}; export const actionHandlers = (setState: Store['setState'], initialState: AppState) => ({ ...counterHandlers(setState),