Skip to content

Commit

Permalink
创建Store后只能获取 storeApi / state / actions
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-hxl committed Nov 27, 2022
1 parent 50d4b2d commit c2b5b52
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import {
IProxyInstanceRes
} from './types'

export default function xlStore<
S extends IState,
A extends IActions<IProxyInstanceRes<S, A>>
>(
function xlStore<S extends IState, A extends IActions<IProxyInstanceRes<S, A>>>(
storeArgs: IStoreArgs<S, A>,
options?: IStoreOptionsArg
): IProxyInstanceRes<S, A> {
Expand All @@ -25,3 +22,5 @@ export default function xlStore<

return proxyInstanceRes
}

module.exports = xlStore
12 changes: 6 additions & 6 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ export function proxyInstance<
const { state, actions, storeApi } = instance

return new Proxy<IProxyInstanceRes<S, A>>(instance as any, {
// storeApi => state => actions

get(_, prop: string) {
if (prop in instance) {
return instance[prop as ObjectKey<IInstance<S, A>>]
} else if (prop in storeApi) {
if (prop in storeApi) {
return storeApi[prop as ObjectKey<IStoreApi<S>>]
} else if (prop in state) {
return state[prop]
} else if (prop in actions) {
return actions[prop]
} else {
throw new Error(`不能获取 ${prop}`)
return undefined
}
},
set(_, prop: string, value: any) {
if (prop in instance || prop in storeApi) {
if (prop in storeApi) {
throw new Error(`${prop} 是 Store 自带的不允许被修改`)
} else if (prop in state) {
state[prop as ObjectKey<S | IState>] = value
return true
} else if (prop in actions) {
throw new Error(`${prop} 是 actions , 不允许被修改`)
throw new Error(`${prop} 是 actions , 不允许在此被修改`)
} else {
throw new Error(`${prop} 不允许被修改或添加`)
}
Expand Down
7 changes: 1 addition & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,4 @@ export interface IStoreOptionsArg {
export type IProxyInstanceRes<
S extends IState,
A extends IActions<IProxyInstanceRes<S, A>>
> = S &
A &
IStoreApi<S> & {
id: number
trackStore: ITrackStore<S>
}
> = S & A & IStoreApi<S>

0 comments on commit c2b5b52

Please sign in to comment.