Skip to content

Commit

Permalink
feat(createGlobalState): 新增 setStatePartial
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jan 19, 2021
1 parent f5dbb04 commit 9c76e5a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/react/createGlobalState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,32 @@ describe('createGlobalState', () => {
})
})
})

describe('对象', () => {
let useGlobalObj: CreateGlobalStateResult<{ x: number; y: string }>

beforeEach(() => {
useGlobalObj = createGlobalState({ x: 1, y: '2' })
})

test('setState', () => {
expect(useGlobalObj.getState()).toEqual({ x: 1, y: '2' })
useGlobalObj.setState({ x: 2, y: '3' })
expect(useGlobalObj.getState()).toEqual({ x: 2, y: '3' })
})

test('setStatePartial', () => {
expect(useGlobalObj.getState()).toEqual({ x: 1, y: '2' })
useGlobalObj.setStatePartial({})
expect(useGlobalObj.getState()).toEqual({ x: 1, y: '2' })
useGlobalObj.setStatePartial({ y: '6' })
expect(useGlobalObj.getState()).toEqual({ x: 1, y: '6' })
useGlobalObj.setStatePartial({ x: 33 })
expect(useGlobalObj.getState()).toEqual({ x: 33, y: '6' })
useGlobalObj.setStatePartial({ x: 2, y: '3' })
expect(useGlobalObj.getState()).toEqual({ x: 2, y: '3' })
useGlobalObj.setStatePartial(_ => ({ x: _.x + 10 }))
expect(useGlobalObj.getState()).toEqual({ x: 12, y: '3' })
})
})
})
16 changes: 16 additions & 0 deletions src/react/createGlobalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface CreateGlobalStateResult<
(): CreateGlobalStateResultResult<S, R>
getState(): S
setState(nextState: SetStateAction<S>): void
setStatePartial(nextState: Partial<S> | ((prevState: S) => Partial<S>)): void
watchState(callback: (nextState: S, prevState: S) => any): () => void
watchStateImmediate(callback: (nextState: S, prevState: S) => any): () => void
}
Expand Down Expand Up @@ -73,6 +74,20 @@ export function createGlobalState<S extends CreateGlobalStateState, R = never>(
bus.emit('setGlobalState', nextGlobalState as any, prevGlobalState)
}
}
const setGlobalStatePartial: Dispatch<
SetStateAction<Partial<S> | undefined>
> = nextGlobalState => {
if (typeof nextGlobalState === 'function') {
nextGlobalState = (nextGlobalState as any)(currentGlobalState)
}
nextGlobalState = {
...(currentGlobalState as any),
...nextGlobalState,
}
const prevGlobalState = currentGlobalState
currentGlobalState = nextGlobalState as any
bus.emit('setGlobalState', nextGlobalState as any, prevGlobalState)
}
const watchGlobalState: (
callback: (
nextGlobalState: S | undefined,
Expand Down Expand Up @@ -103,6 +118,7 @@ export function createGlobalState<S extends CreateGlobalStateState, R = never>(
}) as any
useGlobalState.getState = getGlobalState
useGlobalState.setState = setGlobalState
useGlobalState.setStatePartial = setGlobalStatePartial
useGlobalState.watchState = watchGlobalState
useGlobalState.watchStateImmediate = watchGlobalStateImmediate
return useGlobalState
Expand Down

0 comments on commit 9c76e5a

Please sign in to comment.