Skip to content

Commit 8989095

Browse files
authored
Provides a stable send method. Closes #12 (#18)
* Provides a stable send method * Fix linter & lint
1 parent 9e40f1a commit 8989095

File tree

6 files changed

+181
-135
lines changed

6 files changed

+181
-135
lines changed

.eslintrc.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module.exports = {
2-
plugins: ['react-hooks'],
3-
rules: {
4-
'react-hooks/exhaustive-deps': 'warn',
2+
extends: ['react-app', 'prettier/@typescript-eslint', 'plugin:prettier/recommended'],
3+
settings: {
4+
react: {
5+
version: 'detect',
6+
},
57
},
68
};

package-lock.json

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@size-limit/preset-small-lib": "^4.10.1",
3131
"@testing-library/react-hooks": "^5.1.0",
3232
"@types/react": "^17.0.3",
33-
"eslint-plugin-react-hooks": "^4.1.0",
33+
"eslint-plugin-react-hooks": "^4.2.0",
3434
"husky": "^5.1.3",
3535
"react": ">=16.8.0",
3636
"react-test-renderer": "^17.0.1",

src/index.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,29 @@ function useStateMachineImpl<Context>(context: Context): UseStateMachineWithCont
117117

118118
const reducer = useConstant(() => getReducer<Context, S, T>(config));
119119

120-
const [machine, send] = useReducer(reducer, initialState);
120+
const [machine, dispatch] = useReducer(reducer, initialState);
121121
// The updater function sends an internal event to the reducer to trigger the actual update
122-
const sendUpdater: Dispatch<ContextUpdate<Context>> = updater => send({ type: 'Update', updater });
123-
// The public updater function exposed to the user
124-
const sendNext: Dispatch<T> = next => send({ type: 'Transition', next });
122+
const update: Dispatch<ContextUpdate<Context>> = updater =>
123+
dispatch({
124+
type: 'Update',
125+
updater,
126+
});
127+
// The public dispatch/send function exposed to the user
128+
const send: Dispatch<T> = useConstant(() => next =>
129+
dispatch({
130+
type: 'Transition',
131+
next,
132+
})
133+
);
125134

126135
useEffect(() => {
127-
const exit = config.states[machine.value]?.effect?.(sendNext, sendUpdater);
128-
return typeof exit === 'function' ? () => exit(sendNext, sendUpdater) : undefined;
136+
const exit = config.states[machine.value]?.effect?.(send, update);
137+
return typeof exit === 'function' ? () => exit(send, update) : undefined;
138+
// We are bypassing the linter here because we deliberately want the effects to run on explicit machine state changes.
139+
// eslint-disable-next-line react-hooks/exhaustive-deps
129140
}, [machine.value]);
130141

131-
return [machine, sendNext];
142+
return [machine, send];
132143
};
133144
}
134145

src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default function log(message: string, ...optionalParams: any[]) {
22
if (process.env.NODE_ENV === 'development') {
3-
const logArguments = ['%cuseStateMachine ' + `%c${message}`, 'color: #888;', 'color: default;', ...optionalParams];
3+
const logArguments = [`%cuseStateMachine %c${message}`, 'color: #888;', 'color: default;', ...optionalParams];
44

55
// Console.log clearly accepts parameters other than string, but TypeScript is complaining, so...
66
// @ts-ignore

0 commit comments

Comments
 (0)