atomic-state
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -59,3 +59,4 @@ /** @license Atomic State | ||
remove(k: string): Promise<void>; | ||
get(k: string): any; | ||
}; | ||
export {}; |
10
index.js
@@ -237,3 +237,13 @@ "use strict"; | ||
}, | ||
get: function (k) { | ||
if (typeof localStorage !== "undefined") { | ||
try { | ||
return JSON.parse(localStorage[k]); | ||
} | ||
catch (err) { | ||
return ""; | ||
} | ||
} | ||
}, | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "atomic-state", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "State managment library for React and React Native apps", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
155
src/index.ts
@@ -8,30 +8,30 @@ /** @license Atomic State | ||
import { EventEmitter } from "events"; | ||
import { Dispatch, SetStateAction, useEffect, useMemo, useState } from "react"; | ||
import { EventEmitter } from "events" | ||
import { Dispatch, SetStateAction, useEffect, useMemo, useState } from "react" | ||
type AtomType<T> = { | ||
name: string; | ||
default: T; | ||
localStoragePersistence?: boolean; | ||
name: string | ||
default: T | ||
localStoragePersistence?: boolean | ||
actions?: { | ||
[name: string]: (st: { | ||
args: any; | ||
state: T; | ||
dispatch: Dispatch<SetStateAction<T>>; | ||
}) => void; | ||
}; | ||
}; | ||
args: any | ||
state: T | ||
dispatch: Dispatch<SetStateAction<T>> | ||
}) => void | ||
} | ||
} | ||
const atomEmitters: { | ||
[key: string]: { | ||
emitter: EventEmitter; | ||
notify: (storeName: string, hookCall: string, payload?: {}) => void; | ||
}; | ||
} = {}; | ||
emitter: EventEmitter | ||
notify: (storeName: string, hookCall: string, payload?: {}) => void | ||
} | ||
} = {} | ||
function createEmitter() { | ||
const emitter = new EventEmitter(); | ||
emitter.setMaxListeners(10e12); | ||
const emitter = new EventEmitter() | ||
emitter.setMaxListeners(10e12) | ||
function notify(storeName: string, hookCall: string, payload = {}) { | ||
emitter.emit(storeName, { hookCall, payload }); | ||
emitter.emit(storeName, { hookCall, payload }) | ||
} | ||
@@ -41,7 +41,7 @@ return { | ||
notify, | ||
}; | ||
} | ||
} | ||
function useAtomCreate<R>(init: AtomType<R>) { | ||
const hookCall = useMemo(() => `${Math.random()}`.split(".")[1], []); | ||
const hookCall = useMemo(() => `${Math.random()}`.split(".")[1], []) | ||
@@ -55,12 +55,12 @@ const initialValue = (function getInitialValue() { | ||
: init.default | ||
: init.default; | ||
})(); | ||
: init.default | ||
})() | ||
const [state, setState] = useState<R>(initialValue); | ||
const [state, setState] = useState<R>(initialValue) | ||
if (!atomEmitters[init.name]) { | ||
atomEmitters[init.name] = createEmitter(); | ||
atomEmitters[init.name] = createEmitter() | ||
} | ||
const { emitter, notify } = atomEmitters[init.name]; | ||
const { emitter, notify } = atomEmitters[init.name] | ||
@@ -70,20 +70,20 @@ useEffect(() => { | ||
if (e.hookCall !== hookCall) { | ||
setState(e.payload); | ||
setState(e.payload) | ||
} | ||
}; | ||
} | ||
emitter.addListener(init.name, handler); | ||
emitter.addListener(init.name, handler) | ||
return () => { | ||
emitter.removeListener(init.name, handler); | ||
}; | ||
emitter.removeListener(init.name, handler) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
}, []) | ||
const updateState: Dispatch<SetStateAction<R>> = (v) => { | ||
// First notify other subscribers | ||
notify(init.name, hookCall, v); | ||
notify(init.name, hookCall, v) | ||
// Finally update state | ||
setState(v); | ||
}; | ||
setState(v) | ||
} | ||
@@ -93,13 +93,13 @@ useEffect(() => { | ||
if (init.localStoragePersistence) { | ||
localStorage[`store-${init.name}`] = JSON.stringify(state); | ||
localStorage[`store-${init.name}`] = JSON.stringify(state) | ||
} else { | ||
if (typeof localStorage[`store-${init.name}`] !== "undefined") { | ||
localStorage.removeItem(`store-${init.name}`); | ||
localStorage.removeItem(`store-${init.name}`) | ||
} | ||
} | ||
} | ||
}, [init.name, init.localStoragePersistence, state]); | ||
}, [init.name, init.localStoragePersistence, state]) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const actions = useMemo(() => init.actions || {}, []); | ||
const actions = useMemo(() => init.actions || {}, []) | ||
const __actions = useMemo( | ||
@@ -120,3 +120,3 @@ () => | ||
[state] | ||
); | ||
) | ||
@@ -127,3 +127,3 @@ return [ | ||
__actions as { [name: string]: (args?: any) => void }, | ||
]; | ||
] | ||
} | ||
@@ -135,5 +135,5 @@ | ||
export function atom<R>(init: AtomType<R>) { | ||
return () => useAtomCreate<R>(init); | ||
return () => useAtomCreate<R>(init) | ||
} | ||
export const createAtom = atom; | ||
export const createAtom = atom | ||
@@ -144,3 +144,3 @@ type useAtomType<R> = () => ( | ||
| { [name: string]: (args?: any) => void } | ||
)[]; | ||
)[] | ||
@@ -155,3 +155,3 @@ /** | ||
{ [name: string]: (args?: any) => void } | ||
]; | ||
] | ||
} | ||
@@ -163,5 +163,5 @@ | ||
export function useValue<R>(atom: useAtomType<R>) { | ||
return atom()[0] as R; | ||
return atom()[0] as R | ||
} | ||
export const useAtomValue = useValue; | ||
export const useAtomValue = useValue | ||
@@ -172,5 +172,5 @@ /** | ||
export function useDispatch<R>(atom: useAtomType<R>) { | ||
return atom()[1] as (cb: ((c: R) => R) | R) => void; | ||
return atom()[1] as (cb: ((c: R) => R) | R) => void | ||
} | ||
export const useAtomDispatch = useDispatch; | ||
export const useAtomDispatch = useDispatch | ||
@@ -181,5 +181,5 @@ /** | ||
export function useActions<R>(atom: useAtomType<R>) { | ||
return atom()[2] as { [name: string]: (args?: any) => void }; | ||
return atom()[2] as { [name: string]: (args?: any) => void } | ||
} | ||
export const useAtomActions = useActions; | ||
export const useAtomActions = useActions | ||
@@ -189,16 +189,16 @@ // localStorage utilities for web apps | ||
const storageEmitter = (() => { | ||
const emm = new EventEmitter(); | ||
emm.setMaxListeners(10 ** 10); | ||
return emm; | ||
})(); | ||
const emm = new EventEmitter() | ||
emm.setMaxListeners(10 ** 10) | ||
return emm | ||
})() | ||
export function useStorage(): { | ||
[key: string]: any; | ||
[key: string]: any | ||
} { | ||
const [keys, setKeys] = useState({}); | ||
const [keys, setKeys] = useState({}) | ||
async function updateStore() { | ||
let $keys: { | ||
[key: string]: any; | ||
} = {}; | ||
[key: string]: any | ||
} = {} | ||
@@ -209,5 +209,5 @@ if (typeof localStorage !== "undefined") { | ||
try { | ||
$keys[k] = JSON.parse(localStorage[k]); | ||
$keys[k] = JSON.parse(localStorage[k]) | ||
} catch (err) { | ||
$keys[k] = localStorage[k]; | ||
$keys[k] = localStorage[k] | ||
} | ||
@@ -217,16 +217,16 @@ } | ||
} | ||
setKeys($keys); | ||
setKeys($keys) | ||
} | ||
useEffect(() => { | ||
updateStore(); | ||
}, []); | ||
updateStore() | ||
}, []) | ||
useEffect(() => { | ||
storageEmitter.addListener("store-changed", updateStore); | ||
storageEmitter.addListener("store-changed", updateStore) | ||
return () => { | ||
storageEmitter.removeListener("store-changes", updateStore); | ||
}; | ||
}, []); | ||
return keys; | ||
storageEmitter.removeListener("store-changes", updateStore) | ||
} | ||
}, []) | ||
return keys | ||
} | ||
@@ -237,4 +237,4 @@ | ||
if (typeof localStorage !== "undefined") { | ||
localStorage[k] = JSON.stringify(v); | ||
storageEmitter.emit("store-changed", v); | ||
localStorage[k] = JSON.stringify(v) | ||
storageEmitter.emit("store-changed", v) | ||
} | ||
@@ -244,6 +244,15 @@ }, | ||
if (typeof localStorage !== "undefined") { | ||
localStorage.removeItem(k); | ||
storageEmitter.emit("store-changed", {}); | ||
localStorage.removeItem(k) | ||
storageEmitter.emit("store-changed", {}) | ||
} | ||
}, | ||
}; | ||
get(k: string) { | ||
if (typeof localStorage !== "undefined") { | ||
try { | ||
return JSON.parse(localStorage[k]) | ||
} catch (err) { | ||
return "" | ||
} | ||
} | ||
}, | ||
} |
Sorry, the diff of this file is not supported yet
66682
549