use-json-localstorage 0.3.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-json-localstorage",
3
- "version": "0.3.0",
3
+ "version": "1.2.0",
4
4
  "description": "JSON local storage for React",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,6 +22,10 @@
22
22
  "require": "./dist/index.cjs"
23
23
  }
24
24
  },
25
+ "files": [
26
+ "dist",
27
+ "README.md"
28
+ ],
25
29
  "scripts": {
26
30
  "build": "tsup",
27
31
  "prepublishOnly": "pnpm build"
@@ -42,4 +46,4 @@
42
46
  "dependencies": {
43
47
  "superjson": "^2.2.6"
44
48
  }
45
- }
49
+ }
@@ -1,68 +0,0 @@
1
- 'use client';
2
-
3
- import SuperJSON from "superjson";
4
-
5
- interface SerializeStorage {
6
- set: <T>(key: string, value: T) => void;
7
- get: <T>(key: string) => T | null;
8
- on: (type: string, callback: (event: LocalStorageEvent) => void) => () => void;
9
- }
10
-
11
- type ExtendedGlobalThis = typeof globalThis & {
12
- runtimeLocalStorage: SerializeStorage;
13
- };
14
-
15
- function deserialize<T>(serializedJavascript: string): T {
16
- return SuperJSON.parse(serializedJavascript);
17
- }
18
-
19
- type Listener<T> = (event: T) => void;
20
-
21
- type LocalStorageEvent = {
22
- key: string;
23
- value: unknown;
24
- };
25
-
26
- function createEmitter<T>() {
27
- const listeners = new Map<string, Set<Listener<T>>>();
28
- return {
29
- emit(type: string, event: T) {
30
- listeners.get(type)?.forEach((listener) => listener(event));
31
- },
32
- on(type: string, listener: Listener<T>) {
33
- if (!listeners.has(type)) {
34
- listeners.set(type, new Set());
35
- }
36
- listeners.get(type)!.add(listener);
37
- return () => listeners.get(type)!.delete(listener);
38
- }
39
-
40
- };
41
- }
42
-
43
-
44
- export const getRuntimeLocalStorage = () => {
45
- if (!(globalThis as ExtendedGlobalThis).runtimeLocalStorage) {
46
- (globalThis as ExtendedGlobalThis).runtimeLocalStorage = (() => {
47
- const emitter = createEmitter<LocalStorageEvent>();
48
-
49
- return {
50
- set: (key: string, value: unknown) => {
51
- const serializedValue = SuperJSON.stringify(value);
52
- globalThis.localStorage.setItem(key, serializedValue);
53
- emitter.emit(`set`, { key, value });
54
- return;
55
- },
56
- get: <T>(key: string) => {
57
- const value = globalThis.localStorage.getItem(key);
58
- return value ? deserialize<T>(value) : null;
59
- },
60
- on: (type: string, callback: Listener<LocalStorageEvent>) => {
61
- return emitter.on(type, callback)
62
- },
63
- };
64
- })();
65
- };
66
-
67
- return (globalThis as ExtendedGlobalThis).runtimeLocalStorage;
68
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { getRuntimeLocalStorage } from './getRuntimeLocalStorage';
2
- export { useLocalStorageValue } from './useLocalStorageValue';
@@ -1,19 +0,0 @@
1
- import { useSyncExternalStore } from "react";
2
- import { getRuntimeLocalStorage } from "./getRuntimeLocalStorage";
3
-
4
- export const useLocalStorageValue = <T>(key: string, defaultValue: T) => {
5
- const store = getRuntimeLocalStorage();
6
- return useSyncExternalStore(
7
- (onStoreChange) => {
8
- return store.on('set', (event) => {
9
- if (event.key === key) {
10
- onStoreChange();
11
- }
12
- });
13
- },
14
- () => {
15
- const value = store.get<T>(key);
16
- return value ?? defaultValue;
17
- }
18
- );
19
- };
package/tsconfig.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2019",
4
- "module": "ESNext",
5
- "moduleResolution": "Bundler",
6
-
7
- "jsx": "react-jsx",
8
-
9
- "declaration": true,
10
- "declarationMap": true,
11
- "emitDeclarationOnly": false,
12
-
13
- "strict": true,
14
- "skipLibCheck": true,
15
-
16
- "esModuleInterop": true,
17
- "forceConsistentCasingInFileNames": true,
18
-
19
- "outDir": "dist",
20
- "rootDir": "src"
21
- },
22
- "include": ["src"]
23
- }
package/tsup.config.ts DELETED
@@ -1,8 +0,0 @@
1
- import { defineConfig } from "tsup";
2
-
3
- export default defineConfig({
4
- entry: ["src/index.ts"],
5
- format: ["esm", "cjs"],
6
- dts: true,
7
- clean: true,
8
- });