Skip to content

Commit 3288df9

Browse files
committed
Preparing for npm
1 parent 6ee44f8 commit 3288df9

File tree

8 files changed

+28
-106
lines changed

8 files changed

+28
-106
lines changed

.prettierrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

README.md

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
1-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
1+
# react-context-mutex
2+
A mutex implementation using React and React context. It will prevent a function from running multiple times, until you allow it to run again.
23

3-
## Available Scripts
4+
I created this because I wanted to prevent a fetch from running multiple times after implementing a custom hook in multiple components. The custom hook had a `useEffect` which ran the fetch function.
45

5-
In the project directory, you can run:
6+
## How to use
7+
```ts
8+
const fetchData = () => {
9+
const mutex = new Mutex('myUniqueKey1');
610

7-
### `npm start`
11+
mutex.run(async () => {
12+
mutex.lock();
13+
try {
14+
const response = await fetch('http://myurl');
15+
const data = await response.json();
16+
mutex.unlock();
17+
} catch (e) {
18+
mutex.unlock();
19+
}
20+
});
21+
};
822

9-
Runs the app in the development mode.<br />
10-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
23+
fetchData();
24+
fetchData();
25+
fetchData();
26+
```
1127

12-
The page will reload if you make edits.<br />
13-
You will also see any lint errors in the console.
28+
The fetchData function runs multiple times but the actual fetch is only done once. When the mutex is unlocked again, the fetch will also execute again.
1429

15-
### `npm test`
30+
## How it works
31+
Behind the scenes there's an array of strings, representing the mutex keys, registered using React context. A new record is added to the array when a process should be locked. Likewise, the record is removed when the process is unlocked.
1632

17-
Launches the test runner in the interactive watch mode.<br />
18-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
33+
When using the `mutex.run` method, there's a check to see if the process is locked or not. If it is, the callback within the `mutext.run` is abandoned. If the provided key does not reference to a locked process, the callback is executed.
1934

20-
### `npm run build`
21-
22-
Builds the app for production to the `build` folder.<br />
23-
It correctly bundles React in production mode and optimizes the build for the best performance.
24-
25-
The build is minified and the filenames include the hashes.<br />
26-
Your app is ready to be deployed!
27-
28-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29-
30-
### `npm run eject`
31-
32-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33-
34-
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.
35-
36-
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.
37-
38-
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.
39-
40-
## Learn More
41-
42-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43-
44-
To learn React, check out the [React documentation](https://reactjs.org/).
35+
Why React context? Because that way the "mutex store" array will only be initiated once, thus able to use throughout multiple components without causing unwanted collisions.

package-lock.json

Lines changed: 0 additions & 12 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"scripts": {
1313
"test": "jest",
1414
"build": "rollup -c",
15-
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
1615
"lint": "tslint -p tsconfig.json"
1716
},
17+
"files": ["dist/**/*"],
1818
"repository": {
1919
"type": "git",
2020
"url": "https://github.com/marcveens/react-context-mutext.git"
@@ -29,13 +29,11 @@
2929
"@types/jest": "^24.9.0",
3030
"@types/node": "^12.12.25",
3131
"jest": "^24.9.0",
32-
"prettier": "^1.19.1",
3332
"rollup": "^1.29.0",
3433
"rollup-plugin-typescript2": "^0.25.3",
3534
"ts-jest": "^24.3.0",
3635
"tslib": "^1.10.0",
3736
"tslint": "^5.20.1",
38-
"tslint-config-prettier": "^1.18.0",
3937
"typescript": "^3.7.5"
4038
}
4139
}

src/MutexContext/MutexContext.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/MutexContext/createMutex.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/MutexContext/useMutex.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

tslint.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{
2-
"extends": [
3-
"tslint-config-prettier"
4-
],
52
"rules": {
63
"semicolon": [
74
true,

0 commit comments

Comments
 (0)