├── .circleci
└── config.yml
├── .eslintrc.json
├── .gitignore
├── .npmignore
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── build
└── index.js
├── docs
├── README.md
├── api.md
└── examples.md
├── index.d.ts
├── index.js
├── package.json
├── src
└── index.js
├── test
├── components
│ ├── TrafficLights.js
│ └── TrafficLightsWithWalk.js
├── usm-context.test.js
├── usm-nested.test.js
├── usm-revert.test.js
└── usm.test.js
└── yarn.lock
/.circleci/config.yml:
--------------------------------------------------------------------------------
1 | # Javascript Node CircleCI 2.0 configuration file
2 | #
3 | # Check https://circleci.com/docs/2.0/language-javascript/ for more details
4 | #
5 | version: 2
6 | jobs:
7 | build:
8 | docker:
9 | # specify the version you desire here
10 | - image: circleci/node:10.12
11 |
12 | # Specify service dependencies here if necessary
13 | # CircleCI maintains a library of pre-built images
14 | # documented at https://circleci.com/docs/2.0/circleci-images/
15 | # - image: circleci/mongo:3.4.4
16 |
17 | working_directory: ~/repo
18 |
19 | steps:
20 | - checkout
21 |
22 | # Download and cache dependencies
23 | - restore_cache:
24 | keys:
25 | - v1-dependencies-{{ checksum "package.json" }}
26 | # fallback to using the latest cache if no exact match is found
27 | - v1-dependencies-
28 |
29 | - run: yarn install
30 |
31 | - save_cache:
32 | paths:
33 | - node_modules
34 | key: v1-dependencies-{{ checksum "package.json" }}
35 |
36 | # run tests!
37 | - run: yarn test:ci
38 |
39 | - run:
40 | name: Coverage
41 | command: yarn coverage:ci
42 | # environment:
43 | # CODECOV_TOKEN: 1cc28c23-ea43-4955-8260-f5ccf2a08823
44 |
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "env": {
3 | "browser": true,
4 | "es6": true,
5 | "node": true
6 | },
7 | "extends": "eslint:recommended",
8 | "parserOptions": {
9 | "ecmaVersion": 2016,
10 | "sourceType": "module"
11 | },
12 | "rules": {
13 | "indent": [
14 | "error",
15 | 4
16 | ],
17 | "linebreak-style": [
18 | "error",
19 | "unix"
20 | ],
21 | "quotes": [
22 | "error",
23 | "single"
24 | ],
25 | "semi": [
26 | "error",
27 | "always"
28 | ]
29 | }
30 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | .vscode
3 | node_modules
4 | *.map
5 | *.log
6 |
7 | coverage
8 |
9 | tags
10 | tags.temp
11 | tags.lock
12 |
13 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | test
2 | docs
3 | src
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | * Using welcoming and inclusive language
12 | * Being respectful of differing viewpoints and experiences
13 | * Gracefully accepting constructive criticism
14 | * Focusing on what is best for the community
15 | * Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | * Trolling, insulting/derogatory comments, and personal or political attacks
21 | * Public or private harassment
22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | * Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at phenax5@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing
2 |
3 | When contributing to this repository, please first discuss the change you wish to make via an issue or any other method with the owners of this repository before making a change.
4 |
5 | Please note we have a [code of conduct](https://github.com/phenax/pipey/blob/master/CODE_OF_CONDUCT.md), please follow it in all your interactions with the project.
6 |
7 | ## Pull Request Process
8 |
9 | 1. Create an issue describing the problem or start a discussion on an exisiting, related issue.
10 | 2. Create a pull request.
11 | 3. Don't forget to write tests!
12 | 4. Update the README.md with details of changes to the interface.
13 | 5. You don't have to change the version number. It'll be handled in the publishing stage.
14 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Akshay Nair
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # useTinyStateMachine
3 | A tiny (~700 bytes) react hook to help you write finite state machines
4 |
5 | [](https://circleci.com/gh/phenax/use-tiny-state-machine)
6 | [](https://www.npmjs.com/package/use-tiny-state-machine)
7 | [](https://codecov.io/gh/phenax/use-tiny-state-machine)
8 |
9 |
10 | [Read the documentation for more information](https://github.com/phenax/use-tiny-state-machine/tree/master/docs)
11 |
12 | ## Install
13 |
14 | #### Import
15 | ```bash
16 | yarn add use-tiny-state-machine
17 | ```
18 |
19 |
20 | ## Examples
21 |
22 | ### Manual traffic lights
23 |
24 | ```js
25 | import useTinyStateMachine from 'use-tiny-state-machine';
26 |
27 | const stateChart = {
28 | id: 'traffixLight',
29 | initial: 'green',
30 | states: {
31 | green: { on: { NEXT: 'red' } },
32 | orange: { on: { NEXT: 'green' } },
33 | red: { on: { NEXT: 'orange' } },
34 | },
35 | };
36 |
37 | export default function ManualTrafficLights() {
38 | const { cata, state, dispatch } = useTinyStateMachine(stateChart);
39 |
40 | return (
41 |
42 |
52 | The light is {state}
53 |
54 |
55 |
56 | );
57 | };
58 | ```
59 |
60 |
61 | ### Automated traffic lights with onEntry action
62 | `onEntry` is called every time you enter a given state. `onEntry` is called with the current state machine instance.
63 |
64 | ```js
65 | import useTinyStateMachine from 'use-tiny-state-machine';
66 |
67 | const stateChart = {
68 | id: "traffixLight",
69 | initial: "green",
70 | states: {
71 | green: {
72 | onEntry: waitForNextLight,
73 | on: {
74 | NEXT: "red"
75 | }
76 | },
77 | orange: {
78 | onEntry: waitForNextLight,
79 | on: {
80 | NEXT: "green"
81 | }
82 | },
83 | red: {
84 | onEntry: waitForNextLight,
85 | on: {
86 | NEXT: "orange"
87 | }
88 | }
89 | }
90 | };
91 |
92 | function waitForNextLight({ dispatch }) {
93 | const timer = setTimeout(() => dispatch('NEXT'), 1000);
94 | return () => clearTimeout(timer);
95 | }
96 |
97 | function TrafficLights() {
98 | const { cata, state, dispatch } = useTinyStateMachine(stateChart);
99 |
100 | return (
101 |
102 |