-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from iMrDJAi/v2
v2.0.0
- Loading branch information
Showing
7 changed files
with
172 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Publish to NPM | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
publish-npm: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Clone repo | ||
uses: actions/checkout@v2 | ||
- name: Setup .npmrc file | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '12.x' | ||
registry-url: 'https://registry.npmjs.org' | ||
- name: Publish! | ||
run: npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
dist/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* A hook used within React components to subscribe to a specific sync with the given ID, | ||
* and to access the sync value | ||
* @param id The sync ID to subscribe to | ||
* @param initialValue An initial value returned by the hook when the sync value is not available | ||
* @returns The sync value | ||
*/ | ||
declare function useSync (id: string, initialValue?: any): any | ||
/** | ||
* A function can be used everywhere to dispatch a sync with the given ID, | ||
* and to pass a new sync value to the subscribed components | ||
* @param id The sync ID to synchronize | ||
* @param newValue A new sync value to pass | ||
*/ | ||
declare function sync (id: string, newValue?: any): void | ||
/** | ||
* An optional object to store states inside | ||
*/ | ||
declare const storage: { [key: string]: any } | ||
|
||
export { useSync, sync, storage } | ||
export default useSync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,40 @@ | ||
import { useReducer, useEffect } from 'react' | ||
import { useReducer, useEffect, useRef } from 'react' | ||
|
||
var syncs = {} | ||
|
||
var useSync = id => { | ||
var [, render] = useReducer(p => !p, false) | ||
const syncs = {} | ||
|
||
useEffect(() => { | ||
if (!syncs[id]) syncs[id] = [] | ||
syncs[id].push(render) | ||
const useSync = (id, initialValue) => { | ||
const [, dispatch] = useReducer(state => !state, false) | ||
const value = useRef(initialValue) | ||
const render = (newValue) => { | ||
value.current = newValue | ||
dispatch() | ||
} | ||
|
||
return () => { | ||
var index = syncs[id].findIndex(e => e === render) | ||
syncs[id].splice(index, 1) | ||
if (syncs[id].length === 0) delete syncs[id] | ||
} | ||
}, [id]) | ||
useEffect(() => { | ||
if (!syncs[id]) syncs[id] = [] | ||
syncs[id].push(render) | ||
|
||
return () => { | ||
const index = syncs[id].findIndex(r => r === render) | ||
syncs[id].splice(index, 1) | ||
if (syncs[id].length === 0) delete syncs[id] | ||
} | ||
}, [id]) | ||
|
||
return value.current | ||
} | ||
|
||
var sync = id => { | ||
if (syncs[id]) for (let render of syncs[id]) setTimeout(() => render(), 0) | ||
const sync = (...args) => { | ||
const [id, newValue] = args | ||
|
||
if (!syncs[id]) return | ||
for (const render of syncs[id]) { | ||
setTimeout(() => args.length < 2 ? render() : render(newValue), 0) | ||
} | ||
} | ||
|
||
var storage = {} | ||
const storage = {} | ||
|
||
export { useSync, sync, storage } | ||
|
||
export default useSync | ||
export default useSync |