-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"typescript.tsdk": "node_modules\\typescript\\lib" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { expect, test } from 'vitest' | ||
import type { Ref } from 'vue' | ||
import type { ReturnDisptch } from '../libs/types' | ||
|
||
import { useReducer } from '../libs/main' | ||
|
||
function reducer(state, action) { | ||
if (action.type === 'add') | ||
return state.value + 1 | ||
|
||
return state.value - 1 | ||
} | ||
test('useReducer', () => { | ||
const [state, dispatch] = useReducer<number>(reducer, 1) | ||
expect((state as Ref<number>).value).toBe(1); | ||
(dispatch as ReturnDisptch) ({ type: 'add' }) | ||
expect((state as Ref<number>).value).toBe(2) | ||
}) | ||
|
||
test('useReducer has init ', () => { | ||
const init = (arg: number) => { | ||
return arg + 1 | ||
} | ||
const [state, dispatch] = useReducer<number>(reducer, 1, init) | ||
expect((state as Ref<number>).value).toBe(2); | ||
(dispatch as ReturnDisptch) ({ type: 'add' }) | ||
expect((state as Ref<number>).value).toBe(3) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import useState from './useState' | ||
import useReducer from './useReducer' | ||
|
||
export { | ||
useState, | ||
useReducer, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { Ref } from 'vue' | ||
import type { Action, Init, Reducer, ReturnDisptch, SetStateSetter } from '../types/index' | ||
import { isFunc } from '../utils/index' | ||
import useState from './useState' | ||
|
||
export default function useReducer<U>(reducer: Reducer<U>, initArg: U, init?: Init<U>) { | ||
let lastInit: U = initArg | ||
if (init && isFunc(init)) | ||
lastInit = init(lastInit) | ||
|
||
const [_state, setState] = useState<U>(lastInit) | ||
|
||
const _dispatch = createDispatch<U>(reducer, _state, setState) | ||
|
||
return [_state, _dispatch] | ||
} | ||
|
||
function createDispatch<T>(reducer: Reducer<T>, _state: Ref<T>, setState: SetStateSetter<T>): ReturnDisptch { | ||
return function (action: Action) { | ||
setState (reducer (_state, action)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
import type { Ref } from 'vue' | ||
|
||
export type SetStateSetter<U> = (newState: U) => void | ||
export interface Action { | ||
type: string | ||
} | ||
|
||
export type Reducer<T> = (state: Ref<T>, action: Action) => T | ||
|
||
export type Init<T> = (init: T) => T | ||
|
||
export type ReturnDisptch = (action: Action) => void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters