Skip to content

Commit

Permalink
testing typescript store
Browse files Browse the repository at this point in the history
  • Loading branch information
YeonV committed Oct 1, 2021
1 parent 9118c34 commit 381b398
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
58 changes: 58 additions & 0 deletions storeTs/settingsTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import create, {GetState, SetState, StateCreator, StoreApi} from 'zustand';
// import { MyState } from "./storeTs";

type colorType = {
r:number,
g:number,
b:number
}

export interface SettingsSlice {
device: string;
setDevice: (newDevice:string) => void;
devices: [];
setDevices: (newDevices:any) => void;
iframe: string;
setIframe: (ip:string) => void;
audioDevice: string;
setAudioDevice: (newDevice:string) => void;
audioDevices: [];
setAudioDevices: (newDevices:any) => void;
color: colorType;
setColor: (newColor:colorType) => void;
}

const storeSettings: StateCreator<SettingsSlice> | StoreApi<SettingsSlice> = (set, get) => ({

device: '',
setDevice: (newDevice:string) => set(() => ({
device: newDevice
})),

devices: [],
setDevices: (newDevices:any) => set(() => ({
devices: newDevices
})),

iframe: '',
setIframe: (ip:string) => set(() => ({
iframe: ip
})),

audioDevice: 'default',
setAudioDevice: (newDevice:string) => set(() => ({
audioDevice: newDevice
})),

audioDevices: [],
setAudioDevices: (newDevices:any) => set(() => ({
audioDevices: newDevices
})),

color: { r: 50, g: 100, b: 150 },
setColor: (newColor:colorType) => set(() => ({
color: newColor
})),

})
export default storeSettings
40 changes: 40 additions & 0 deletions storeTs/storeTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import create, {GetState, SetState, StateCreator, StoreApi} from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import storeSettings, {SettingsSlice} from './settingsTs';
import storeUI, {UiSlice} from './uiTs';

// export type MyState = UiSlice & SettingsSlice;

// type StateFromFunctions<T extends [...any]> = T extends [infer F, ...(infer R)]
// ? F extends (...args: any) => object
// ? StateFromFunctions<R> & ReturnType<F>
// : unknown
// : unknown;


// export type State = StateFromFunctions<[
// typeof storeUI,
// typeof storeSettings
// ]>;

// const useStore = create<State>((set, get) => ({
// ...storeUI(set, get),
// ...storeSettings(set, get)
// }))

interface IStore extends UiSlice, SettingsSlice {}

export const useStore = create<IStore>((set, get, api) => ({
...storeUI(
set as unknown as SetState<UiSlice>,
get as GetState<UiSlice>,
api as unknown as StoreApi<UiSlice>,
),
...storeSettings(
set as unknown as SetState<SettingsSlice>,
get as GetState<SettingsSlice>,
api as unknown as StoreApi<SettingsSlice>,
),
}));

export default useStore
25 changes: 25 additions & 0 deletions storeTs/uiTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import create, {GetState, SetState, StateCreator, StoreApi} from 'zustand';
// import { MyState } from "./storeTs";

export interface UiSlice {
leftBarOpen: Boolean;
setLeftBarOpen: (data:Boolean) => void;
bottomBarOpen: Boolean;
setBottomBarOpen: (data:Boolean) => void;
}

const storeUI: StateCreator<UiSlice> | StoreApi<UiSlice> = (set, get) => ({

leftBarOpen: true,
setLeftBarOpen: (data:Boolean) => set(() => ({
leftBarOpen: data
})),

bottomBarOpen: false,
setBottomBarOpen: (data:Boolean) => set(() => ({
bottomBarOpen: data
})),


})
export default storeUI

0 comments on commit 381b398

Please sign in to comment.