Skip to content

Commit

Permalink
refactor: standardise apps
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler committed Dec 8, 2024
1 parent 2aa4c44 commit 7afdece
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 58 deletions.
14 changes: 3 additions & 11 deletions apps/example-reducers/src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@ import type { Reducer } from 'zutron';

import { counterReducer } from './counter/index.js';

export const rootReducer: Reducer<State> = (state, action) => ({
export type AppState = { counter: number };

export const rootReducer: Reducer<AppState> = (state, action) => ({
counter: counterReducer(state.counter, action),
});

export type Subscribe = (listener: (state: State, prevState: State) => void) => () => void;
export type Handlers = Record<string, () => void>;
export type State = { counter: number };
export type Store = {
getState: () => State;
getInitialState: () => State;
setState: (stateSetter: (state: State) => State) => void;
subscribe: Subscribe;
};
17 changes: 14 additions & 3 deletions apps/example-reducers/src/main/store.ts
Original file line number Diff line number Diff line change
@@ -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<State>()(() => ({
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<AppState>()(() => initialState);
8 changes: 5 additions & 3 deletions apps/example-reducers/src/main/tray/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,7 +16,7 @@ class SystemTray {
private electronTray?: Tray;
private window?: BrowserWindow;

private update = (state: State) => {
private update = (state: AppState) => {
if (!this.dispatch) {
return;
}
Expand Down Expand Up @@ -57,7 +59,7 @@ class SystemTray {

public init = (store: Store, window: BrowserWindow) => {
this.window = window;
this.dispatch = createDispatch<State, Store>(store, { reducer: rootReducer });
this.dispatch = createDispatch<AppState, Store>(store, { reducer: rootReducer });
this.update(store.getState());
store.subscribe(() => this.update(store.getState()));
};
Expand Down
6 changes: 3 additions & 3 deletions apps/example-reducers/src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<State>();
export const { handlers } = preloadZustandBridge<AppState>();

contextBridge.exposeInMainWorld('zutron', handlers);

declare global {
interface Window {
zutron: Handlers<State>;
zutron: Handlers<AppState>;
}
}
4 changes: 2 additions & 2 deletions apps/example-reducers/src/renderer/App.tsx
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
5 changes: 3 additions & 2 deletions apps/example-reducers/src/renderer/hooks/useStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createUseStore } from 'zutron';
import type { State } from '../../features/index.js';

export const useStore = createUseStore<State>(window.zutron);
import type { AppState } from '../../features/index.js';

export const useStore = createUseStore<AppState>(window.zutron);
Original file line number Diff line number Diff line change
@@ -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 })),
Expand Down
16 changes: 5 additions & 11 deletions apps/example-separate-handlers/src/features/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, () => 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<string, () => void>;
export type State = { counter: number };
export type Store = {
getState: () => State;
getInitialState: () => State;
setState: (stateSetter: (state: State) => State) => void;
subscribe: Subscribe;
};
15 changes: 12 additions & 3 deletions apps/example-separate-handlers/src/main/store.ts
Original file line number Diff line number Diff line change
@@ -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<State>()(() => initialState);
export const store = createStore<AppState>()(() => initialState);
8 changes: 5 additions & 3 deletions apps/example-separate-handlers/src/main/tray/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,7 +16,7 @@ class SystemTray {
private electronTray?: Tray;
private window?: BrowserWindow;

private update = (state: State) => {
private update = (state: AppState) => {
if (!this.dispatch) {
return;
}
Expand Down Expand Up @@ -57,7 +59,7 @@ class SystemTray {

public init = (store: Store, window: BrowserWindow, actionHandlers: Handlers) => {
this.window = window;
this.dispatch = createDispatch<State, Store>(store, { handlers: actionHandlers });
this.dispatch = createDispatch<AppState, Store>(store, { handlers: actionHandlers });
this.update(store.getState());
store.subscribe(() => this.update(store.getState()));
};
Expand Down
6 changes: 3 additions & 3 deletions apps/example-separate-handlers/src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<State>();
export const { handlers } = preloadZustandBridge<AppState>();

contextBridge.exposeInMainWorld('zutron', handlers);

declare global {
interface Window {
zutron: Handlers<State>;
zutron: Handlers<AppState>;
}
}
4 changes: 2 additions & 2 deletions apps/example-separate-handlers/src/renderer/App.tsx
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
5 changes: 3 additions & 2 deletions apps/example-separate-handlers/src/renderer/hooks/useStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createUseStore } from 'zutron';
import type { State } from '../../features/index.js';

export const useStore = createUseStore<State>(window.zutron);
import type { AppState } from '../../features/index.js';

export const useStore = createUseStore<AppState>(window.zutron);
2 changes: 1 addition & 1 deletion apps/example-store-handlers/src/features/counter/index.ts
Original file line number Diff line number Diff line change
@@ -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 })),
Expand Down
9 changes: 1 addition & 8 deletions apps/example-store-handlers/src/features/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, () => 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),
Expand Down

0 comments on commit 7afdece

Please sign in to comment.