Skip to content

Commit

Permalink
1.0.0-alpha (pre-release)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMrDJAi committed Jan 22, 2021
0 parents commit 1269a5d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
***
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
28 changes: 28 additions & 0 deletions src/useSync.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1269a5d

Please sign in to comment.