Skip to content

Commit

Permalink
initial source commit
Browse files Browse the repository at this point in the history
  • Loading branch information
frontainer committed Jun 1, 2020
1 parent f1642b9 commit 7902120
Show file tree
Hide file tree
Showing 40 changed files with 26,182 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.cache
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
example
src
70 changes: 69 additions & 1 deletion README.md
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>
)
}
```
1 change: 1 addition & 0 deletions __test__/__mocks__/reactMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
75 changes: 75 additions & 0 deletions __test__/index.test.ts
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'
})
})
33 changes: 33 additions & 0 deletions dist/AsyncActionCreator.d.ts
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>>);
}
12 changes: 12 additions & 0 deletions dist/FluentDispatcher.d.ts
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;
}
16 changes: 16 additions & 0 deletions dist/FluentReducer.d.ts
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>;
}
2 changes: 2 additions & 0 deletions dist/UseFluentReducer.d.ts
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[];
2 changes: 2 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './FluentReducer';
export * from './UseFluentReducer';
1 change: 1 addition & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
23 changes: 23 additions & 0 deletions example/.gitignore
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*
44 changes: 44 additions & 0 deletions example/README.md
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/).
Loading

0 comments on commit 7902120

Please sign in to comment.