-
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
1 parent
f1642b9
commit 7902120
Showing
40 changed files
with
26,182 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,8 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env", | ||
"@babel/preset-react" | ||
], | ||
"plugins": [ | ||
] | ||
} |
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,2 @@ | ||
node_modules | ||
.cache |
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,2 @@ | ||
example | ||
src |
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,69 @@ | ||
# fluent-reducer | ||
# fluent-reducer | ||
|
||
TypeSafe & Immutable useReducer | ||
|
||
## how to use | ||
|
||
``` | ||
npm i -S fluent-reducer immer react | ||
``` | ||
|
||
### 1. Define State Object | ||
``` | ||
interface ROOT_STATE { | ||
name: string | ||
} | ||
const DEFAULT_STATE: ROOT_STATE = { | ||
name: 'sable' | ||
} | ||
``` | ||
|
||
### 2. Create FluentReducer | ||
``` | ||
const reducer = new FluentReducer<ROOT_STATE>() | ||
``` | ||
|
||
### 3. Sync Action | ||
|
||
``` | ||
const changeState = reducer.sync<string>('CHANGE_NAME', (state, payload) => { | ||
state.name = payload | ||
}) | ||
``` | ||
|
||
### 4. Async Action | ||
``` | ||
const asyncChangeState = reducer.async<string, string, Error>('ASYNC_CHANGE_NAME', (name, dispatch, getState) => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(name) | ||
}, 3000) | ||
}) | ||
}, { | ||
started(state, params) { | ||
console.log('started', params) | ||
state.state = 'started' | ||
}, | ||
failed(state, { error }) { | ||
console.error(error) | ||
}, | ||
done(state, { result }) { | ||
state.name = result | ||
console.log('done') | ||
} | ||
}) | ||
``` | ||
|
||
### 5. hooks | ||
|
||
``` | ||
export const MyExample: React.FC = () => { | ||
const [state, dispatch] = useFluentReducer<ROOT_STATE>(reducer, DEFAULT_STATE) | ||
useEffect(() => { | ||
dispatch(asyncChangeState('done')) | ||
}, [dispatch]) | ||
return ( | ||
<div>{state.name}</div> | ||
) | ||
} | ||
``` |
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 @@ | ||
module.exports = {} |
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,75 @@ | ||
import 'jest' | ||
import { FluentReducer } from '../src' | ||
interface IRootState { | ||
hoge: 'test' | ||
} | ||
test('basic', () => { | ||
const reducer = new FluentReducer<IRootState>() | ||
expect(reducer).toBeDefined() | ||
}) | ||
test('sync', () => { | ||
const reducer = new FluentReducer<IRootState>() | ||
const stringAction = reducer.sync<string>('TEST', (state) => {}) | ||
expect(stringAction('test')).toEqual({ | ||
type: 'TEST', | ||
payload: 'test' | ||
}) | ||
const numberAction = reducer.sync<number>('TEST', (state) => {}) | ||
expect(numberAction(2)).toEqual({ | ||
type: 'TEST', | ||
payload: 2 | ||
}) | ||
}) | ||
test('async', () => { | ||
const reducer = new FluentReducer<IRootState>() | ||
const handler = () => { return 0 } | ||
const NAME = 'ASYNC_TEST' | ||
const PARAM = 'hoge' | ||
const asyncAction = reducer.async<string, number, any>(NAME, handler) | ||
expect(asyncAction).toBeDefined() | ||
const action = asyncAction(PARAM) | ||
expect(action.type).toBe(NAME) | ||
expect(action.handler).toBe(handler) | ||
expect(action.param).toBe(PARAM) | ||
expect(action.started(PARAM)).toEqual({ | ||
type: `${NAME}__STARTED`, | ||
payload: PARAM | ||
}) | ||
const err = new Error() | ||
expect(action.failed({ | ||
params: PARAM, | ||
error: err | ||
})).toEqual({ | ||
type: `${NAME}__FAILED`, | ||
payload: { | ||
params: PARAM, | ||
error: err | ||
} | ||
}) | ||
expect(action.done({ | ||
params: PARAM, | ||
result: 0 | ||
})).toEqual({ | ||
type: `${NAME}__DONE`, | ||
payload: { | ||
params: PARAM, | ||
result: 0 | ||
} | ||
}) | ||
}) | ||
|
||
test('multiple', () => { | ||
const reducer = new FluentReducer<IRootState>() | ||
const reducer2 = new FluentReducer<IRootState>() | ||
|
||
const stringAction = reducer.sync<string>('TEST', (state) => {}) | ||
expect(stringAction('test')).toEqual({ | ||
type: 'TEST', | ||
payload: 'test' | ||
}) | ||
const stringAction2 = reducer.sync<string>('TEST', (state) => {}) | ||
expect(stringAction2('test')).toEqual({ | ||
type: 'TEST', | ||
payload: 'test' | ||
}) | ||
}) |
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,33 @@ | ||
import { FluentDispatch } from './FluentDispatcher'; | ||
export interface IAction<P = any> { | ||
type: string; | ||
payload: P; | ||
} | ||
export interface IAsyncFailed<P, E> { | ||
params: P; | ||
error: E; | ||
} | ||
export interface IAsyncSucceeded<P, R> { | ||
params: P; | ||
result: R; | ||
} | ||
export interface IAsyncHandlers<S, P, R, E> { | ||
started: (state: S, params: P) => void; | ||
failed: (state: S, result: IAsyncFailed<P, E>) => void; | ||
done: (state: S, result: IAsyncSucceeded<P, R>) => void; | ||
} | ||
export interface IActionCreator<P> { | ||
(payload: P): IAction<P>; | ||
} | ||
export interface IAsyncHandler<InS, P, R> { | ||
(params: P, dispatch: FluentDispatch<InS, P>, getState: () => Readonly<InS>): Promise<R> | R; | ||
} | ||
export declare class AsyncActionCreator<S, P, R, E> { | ||
type: string; | ||
param: P; | ||
handler: IAsyncHandler<S, P, R>; | ||
started: IActionCreator<P>; | ||
failed: IActionCreator<IAsyncFailed<P, E>>; | ||
done: IActionCreator<IAsyncSucceeded<P, R>>; | ||
constructor(type: string, param: P, handler: IAsyncHandler<S, P, R>, started: IActionCreator<P>, failed: IActionCreator<IAsyncFailed<P, E>>, done: IActionCreator<IAsyncSucceeded<P, R>>); | ||
} |
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,12 @@ | ||
import { Dispatch } from 'react'; | ||
import { AsyncActionCreator, IAction } from './AsyncActionCreator'; | ||
export interface FluentDispatch<InS, P> { | ||
(action: IAction | AsyncActionCreator<InS, any, P, any>): Promise<P> | void; | ||
} | ||
export declare class FluentDispatcher<InS> { | ||
private _state; | ||
private _dispatcher; | ||
update(state: InS, dispatcher: Dispatch<IAction>): void; | ||
getState(): Readonly<InS>; | ||
dispatch<P = any>(action: IAction | AsyncActionCreator<InS, any, P, any>): Promise<P> | 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { AsyncActionCreator, IAction, IActionCreator, IAsyncHandler, IAsyncHandlers } from './AsyncActionCreator'; | ||
export declare type IHandler<InS, P> = (state: InS, payload: P) => void; | ||
export interface IFluentReducerOption<InS> { | ||
defaultHandler: IHandler<InS, IAction> | undefined; | ||
verbose: boolean; | ||
} | ||
export declare class FluentReducer<InS> { | ||
private _handle; | ||
private _exec; | ||
private _option; | ||
constructor(op?: Partial<IFluentReducerOption<InS>>); | ||
reducer: (state: InS, action: any) => any; | ||
private _caseWithAction; | ||
sync<P = any>(type: string, handler: IHandler<InS, P>): IActionCreator<P>; | ||
async<Param, Result, Err>(type: string, handler: IAsyncHandler<InS, Param, Result>, handlers?: Partial<IAsyncHandlers<InS, Param, Result, Err>>): (param: Param) => AsyncActionCreator<InS, Param, Result, Err>; | ||
} |
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,2 @@ | ||
import { FluentReducer } from './index'; | ||
export declare function useFluentReducer<InS>(reducer: FluentReducer<InS>, initialState: InS, initializer?: undefined): any[]; |
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,2 @@ | ||
export * from './FluentReducer'; | ||
export * from './UseFluentReducer'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
SKIP_PREFLIGHT_CHECK=true |
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,23 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
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,44 @@ | ||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). | ||
|
||
## Available Scripts | ||
|
||
In the project directory, you can run: | ||
|
||
### `npm start` | ||
|
||
Runs the app in the development mode.<br /> | ||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser. | ||
|
||
The page will reload if you make edits.<br /> | ||
You will also see any lint errors in the console. | ||
|
||
### `npm test` | ||
|
||
Launches the test runner in the interactive watch mode.<br /> | ||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. | ||
|
||
### `npm run build` | ||
|
||
Builds the app for production to the `build` folder.<br /> | ||
It correctly bundles React in production mode and optimizes the build for the best performance. | ||
|
||
The build is minified and the filenames include the hashes.<br /> | ||
Your app is ready to be deployed! | ||
|
||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. | ||
|
||
### `npm run eject` | ||
|
||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!** | ||
|
||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. | ||
|
||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. | ||
|
||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. | ||
|
||
## Learn More | ||
|
||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). | ||
|
||
To learn React, check out the [React documentation](https://reactjs.org/). |
Oops, something went wrong.