diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..681bd1f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 iMrDJAi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..05e80bc --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +*** +# **useSync** - Custom React Hook +[![npm](https://img.shields.io/npm/v/usesync?color=red)](https://www.npmjs.com/package/usesync) + +A custom React hook to synchronize and share public state across different components on your React project. +*** \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..66daaff --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "usesync", + "version": "1.0.0-alpha", + "description": "A custom React hook to synchronize and share public state across different components on your React project", + "main": "src/useSync.js", + "repository": { + "type": "git", + "url": "git+https://github.com/iMrDJAi/useSync.git" + }, + "keywords": [ + "react", + "hooks", + "state", + "react-hook", + "react-state", + "usesync" + ], + "author": "iMrDJAi", + "license": "MIT", + "bugs": { + "url": "https://github.com/iMrDJAi/useSync/issues" + }, + "homepage": "https://github.com/iMrDJAi/useSync#readme", + "peerDependencies": { + "react": ">= 16.8.0" + } +} \ No newline at end of file diff --git a/src/useSync.js b/src/useSync.js new file mode 100644 index 0000000..77f6835 --- /dev/null +++ b/src/useSync.js @@ -0,0 +1,28 @@ +import { useReducer, useEffect } from 'react' + +var syncs = {} + +var useSync = id => { + var [, render] = useReducer(p => !p, false) + + useEffect(() => { + if (!syncs[id]) syncs[id] = [] + syncs[id].push(render) + + return () => { + var index = syncs[id].findIndex(e => e === render) + syncs[id].splice(index, 1) + if (syncs[id].length === 0) delete syncs[id] + } + }, [id]) +} + +var sync = id => { + if (syncs[id]) for (let render of syncs[id]) render() +} + +var storage = {} + +export { useSync, sync, storage } + +export default useSync \ No newline at end of file