Skip to content

Commit

Permalink
refactor: stricter uesModuleStore
Browse files Browse the repository at this point in the history
  • Loading branch information
Linkontoask committed Dec 25, 2020
1 parent 0c8d434 commit 2fe8b0f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 24 deletions.
13 changes: 8 additions & 5 deletions src/hooks/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ export const useDrag = (
}
}

export function uesModuleStore<S, G = Record<string, string>>(
NAMESPACED: string
) {
export function uesModuleStore<
S,
G = Record<string, string>,
A = string,
M = string
>(NAMESPACED: string) {
const store = useStore()

const useState = (): S => {
Expand All @@ -123,10 +126,10 @@ export function uesModuleStore<S, G = Record<string, string>>(
const useGetter = <key extends keyof G>(value: key): G[key] => {
return store.getters[NAMESPACED + '/' + value]
}
const useActions = (type: string, payload?: unknown) => {
const useActions = (type: A, payload?: unknown) => {
return store.dispatch(NAMESPACED + '/' + type, payload)
}
const useMutations = (type: string, payload?: unknown): void => {
const useMutations = (type: M, payload?: unknown): void => {
store.commit(NAMESPACED + '/' + type, payload)
}

Expand Down
4 changes: 2 additions & 2 deletions src/layout/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { state } from './state'
import { mutations } from './sage'
import { uesModuleStore } from '@/hooks/index'
import { LayoutState } from '@/interface'
import { LayoutState, LayoutActions } from '@/interface'

export const LayoutNameSpaced = 'Layout'

export const useLayoutModule = () =>
uesModuleStore<LayoutState>(LayoutNameSpaced)
uesModuleStore<LayoutState, {}, LayoutActions>(LayoutNameSpaced)

export default {
namespaced: true,
Expand Down
6 changes: 4 additions & 2 deletions src/pages/artist/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { actions, mutations } from './sage'
import { state } from './state'
import { uesModuleStore } from '@/hooks/index'
import { ArtistState } from '@/interface'
import { ArtistState, ArtistActions, ArtistMutations } from '@/interface'

export const ArtistNameSpaced = 'Artist'

export const useArtistModule = () => {
return uesModuleStore<ArtistState>(ArtistNameSpaced)
return uesModuleStore<ArtistState, {}, ArtistActions, ArtistMutations>(
ArtistNameSpaced
)
}

export default {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/download/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { actions, mutations } from './sage'
import { state } from './state'
import { uesModuleStore } from '@/hooks/index'
import { DownloadState } from '@/interface'
import { DownloadState, DownloadActions, DownloadMutations } from '@/interface'

export const DownloadNameSpaced = 'Download'

export const useDownloadModule = () => {
return uesModuleStore<DownloadState>(DownloadNameSpaced)
return uesModuleStore<DownloadState, {}, DownloadActions, DownloadMutations>(
DownloadNameSpaced
)
}

export default {
Expand Down
14 changes: 12 additions & 2 deletions src/pages/footer/module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { actions, mutations, getters } from './sage'
import { state } from './state'
import { uesModuleStore } from '@/hooks/index'
import { FooterState, FooterGetter } from '@/interface'
import {
FooterState,
FooterGetter,
FooterActions,
FooterMutations
} from '@/interface'

export { findMusicIndex } from './sage'

export const FooterNameSpaced = 'Footer'

export const useFooterModule = () => {
return uesModuleStore<FooterState, FooterGetter>(FooterNameSpaced)
return uesModuleStore<
FooterState,
FooterGetter,
FooterActions,
FooterMutations
>(FooterNameSpaced)
}

export default {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/header/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { actions, mutations } from './sage'
import { state } from './state'
import { uesModuleStore } from '@/hooks/index'
import { HeaderState } from '@/interface'
import { HeaderState, HeaderActions, HeaderMutations } from '@/interface'

export const HeaderNameSpaced = 'Header'

export const useHeaderModule = () => {
return uesModuleStore<HeaderState>(HeaderNameSpaced)
return uesModuleStore<HeaderState, {}, HeaderActions, HeaderMutations>(
HeaderNameSpaced
)
}

export default {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/list/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { actions, mutations } from './sage'
import { state } from './state'
import { uesModuleStore } from '@/hooks/index'
import { SongState } from '@/interface'
import { SongState, SongActions, SongMutations } from '@/interface'

export const SongNameSpaced = 'Song'

export const useSongModule = () => {
return uesModuleStore<SongState>(SongNameSpaced)
return uesModuleStore<SongState, {}, SongActions, SongMutations>(
SongNameSpaced
)
}

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/main/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { mutations } from './sage'
import { state } from './state'
import { uesModuleStore } from '@/hooks/index'
import { MainState } from '@/interface'
import { MainState, MainMutations } from '@/interface'

export const MainNameSpaced = 'Main'

export const useMainModule = () => {
return uesModuleStore<MainState>(MainNameSpaced)
return uesModuleStore<MainState, {}, {}, MainMutations>(MainNameSpaced)
}

export default {
Expand Down
29 changes: 24 additions & 5 deletions src/pages/news/children/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,44 @@ import Artists, { NAMESPACED as ArtistsNameSpaced } from './artists/module'
import { uesModuleStore } from '@/hooks/index'
import {
RecommendState,
RecommendActions,
RecommendMutations,
ArtistsState,
ArtistsActions,
ArtistsMutations,
SongListState,
TopListState
SongListActions,
SongListMutations,
TopListState,
TopListActions,
TopListMutations
} from '@/interface'

export const useRecommendModule = () => {
return uesModuleStore<RecommendState>(RecommendNameSpaced)
return uesModuleStore<
RecommendState,
{},
RecommendActions,
RecommendMutations
>(RecommendNameSpaced)
}

export const useArtistSearchModule = () => {
return uesModuleStore<ArtistsState>(ArtistsNameSpaced)
return uesModuleStore<ArtistsState, {}, ArtistsActions, ArtistsMutations>(
ArtistsNameSpaced
)
}

export const useSongListModule = () => {
return uesModuleStore<SongListState>(SongListNameSpaced)
return uesModuleStore<SongListState, {}, SongListActions, SongListMutations>(
SongListNameSpaced
)
}

export const useTopListModule = () => {
return uesModuleStore<TopListState>(TopListNameSpaced)
return uesModuleStore<TopListState, {}, TopListActions, TopListMutations>(
TopListNameSpaced
)
}

export {
Expand Down

0 comments on commit 2fe8b0f

Please sign in to comment.