react-json-localstorage 0.1.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/dist/index.cjs +106 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +68 -0
- package/package.json +48 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
getRuntimeLocalStorage: () => getRuntimeLocalStorage,
|
|
34
|
+
useLocalStorageValue: () => useLocalStorageValue
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/getRuntimeLocalStorage.ts
|
|
39
|
+
var import_superjson = __toESM(require("superjson"), 1);
|
|
40
|
+
function deserialize(serializedJavascript) {
|
|
41
|
+
return import_superjson.default.parse(serializedJavascript);
|
|
42
|
+
}
|
|
43
|
+
function createEmitter() {
|
|
44
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
45
|
+
return {
|
|
46
|
+
emit(type, event) {
|
|
47
|
+
var _a;
|
|
48
|
+
(_a = listeners.get(type)) == null ? void 0 : _a.forEach((listener) => listener(event));
|
|
49
|
+
},
|
|
50
|
+
on(type, listener) {
|
|
51
|
+
if (!listeners.has(type)) {
|
|
52
|
+
listeners.set(type, /* @__PURE__ */ new Set());
|
|
53
|
+
}
|
|
54
|
+
listeners.get(type).add(listener);
|
|
55
|
+
return () => listeners.get(type).delete(listener);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
var getRuntimeLocalStorage = () => {
|
|
60
|
+
if (!globalThis.runtimeLocalStorage) {
|
|
61
|
+
globalThis.runtimeLocalStorage = (() => {
|
|
62
|
+
const emitter = createEmitter();
|
|
63
|
+
return {
|
|
64
|
+
set: (key, value) => {
|
|
65
|
+
const serializedValue = import_superjson.default.stringify(value);
|
|
66
|
+
globalThis.localStorage.setItem(key, serializedValue);
|
|
67
|
+
emitter.emit(`set`, { key, value });
|
|
68
|
+
return;
|
|
69
|
+
},
|
|
70
|
+
get: (key) => {
|
|
71
|
+
const value = globalThis.localStorage.getItem(key);
|
|
72
|
+
return value ? deserialize(value) : null;
|
|
73
|
+
},
|
|
74
|
+
on: (type, callback) => {
|
|
75
|
+
return emitter.on(type, callback);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
})();
|
|
79
|
+
}
|
|
80
|
+
;
|
|
81
|
+
return globalThis.runtimeLocalStorage;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// src/useLocalStorageValue.ts
|
|
85
|
+
var import_react = require("react");
|
|
86
|
+
var useLocalStorageValue = (key, defaultValue) => {
|
|
87
|
+
const store = getRuntimeLocalStorage();
|
|
88
|
+
return (0, import_react.useSyncExternalStore)(
|
|
89
|
+
(onStoreChange) => {
|
|
90
|
+
return store.on("set", (event) => {
|
|
91
|
+
if (event.key === key) {
|
|
92
|
+
onStoreChange();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
() => {
|
|
97
|
+
const value = store.get(key);
|
|
98
|
+
return value != null ? value : defaultValue;
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
};
|
|
102
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
103
|
+
0 && (module.exports = {
|
|
104
|
+
getRuntimeLocalStorage,
|
|
105
|
+
useLocalStorageValue
|
|
106
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface SerializeStorage {
|
|
2
|
+
set: <T>(key: string, value: T) => void;
|
|
3
|
+
get: <T>(key: string) => T | null;
|
|
4
|
+
on: (type: string, callback: (event: LocalStorageEvent) => void) => () => void;
|
|
5
|
+
}
|
|
6
|
+
type LocalStorageEvent = {
|
|
7
|
+
key: string;
|
|
8
|
+
value: unknown;
|
|
9
|
+
};
|
|
10
|
+
declare const getRuntimeLocalStorage: () => SerializeStorage;
|
|
11
|
+
|
|
12
|
+
declare const useLocalStorageValue: <T>(key: string, defaultValue: T) => T;
|
|
13
|
+
|
|
14
|
+
export { getRuntimeLocalStorage, useLocalStorageValue };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface SerializeStorage {
|
|
2
|
+
set: <T>(key: string, value: T) => void;
|
|
3
|
+
get: <T>(key: string) => T | null;
|
|
4
|
+
on: (type: string, callback: (event: LocalStorageEvent) => void) => () => void;
|
|
5
|
+
}
|
|
6
|
+
type LocalStorageEvent = {
|
|
7
|
+
key: string;
|
|
8
|
+
value: unknown;
|
|
9
|
+
};
|
|
10
|
+
declare const getRuntimeLocalStorage: () => SerializeStorage;
|
|
11
|
+
|
|
12
|
+
declare const useLocalStorageValue: <T>(key: string, defaultValue: T) => T;
|
|
13
|
+
|
|
14
|
+
export { getRuntimeLocalStorage, useLocalStorageValue };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// src/getRuntimeLocalStorage.ts
|
|
2
|
+
import SuperJSON from "superjson";
|
|
3
|
+
function deserialize(serializedJavascript) {
|
|
4
|
+
return SuperJSON.parse(serializedJavascript);
|
|
5
|
+
}
|
|
6
|
+
function createEmitter() {
|
|
7
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
8
|
+
return {
|
|
9
|
+
emit(type, event) {
|
|
10
|
+
var _a;
|
|
11
|
+
(_a = listeners.get(type)) == null ? void 0 : _a.forEach((listener) => listener(event));
|
|
12
|
+
},
|
|
13
|
+
on(type, listener) {
|
|
14
|
+
if (!listeners.has(type)) {
|
|
15
|
+
listeners.set(type, /* @__PURE__ */ new Set());
|
|
16
|
+
}
|
|
17
|
+
listeners.get(type).add(listener);
|
|
18
|
+
return () => listeners.get(type).delete(listener);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
var getRuntimeLocalStorage = () => {
|
|
23
|
+
if (!globalThis.runtimeLocalStorage) {
|
|
24
|
+
globalThis.runtimeLocalStorage = (() => {
|
|
25
|
+
const emitter = createEmitter();
|
|
26
|
+
return {
|
|
27
|
+
set: (key, value) => {
|
|
28
|
+
const serializedValue = SuperJSON.stringify(value);
|
|
29
|
+
globalThis.localStorage.setItem(key, serializedValue);
|
|
30
|
+
emitter.emit(`set`, { key, value });
|
|
31
|
+
return;
|
|
32
|
+
},
|
|
33
|
+
get: (key) => {
|
|
34
|
+
const value = globalThis.localStorage.getItem(key);
|
|
35
|
+
return value ? deserialize(value) : null;
|
|
36
|
+
},
|
|
37
|
+
on: (type, callback) => {
|
|
38
|
+
return emitter.on(type, callback);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
})();
|
|
42
|
+
}
|
|
43
|
+
;
|
|
44
|
+
return globalThis.runtimeLocalStorage;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/useLocalStorageValue.ts
|
|
48
|
+
import { useSyncExternalStore } from "react";
|
|
49
|
+
var useLocalStorageValue = (key, defaultValue) => {
|
|
50
|
+
const store = getRuntimeLocalStorage();
|
|
51
|
+
return useSyncExternalStore(
|
|
52
|
+
(onStoreChange) => {
|
|
53
|
+
return store.on("set", (event) => {
|
|
54
|
+
if (event.key === key) {
|
|
55
|
+
onStoreChange();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
() => {
|
|
60
|
+
const value = store.get(key);
|
|
61
|
+
return value != null ? value : defaultValue;
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
getRuntimeLocalStorage,
|
|
67
|
+
useLocalStorageValue
|
|
68
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-json-localstorage",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "JSON local storage for React",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/dohyeon-kr/react-json-localstorage.git"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"react",
|
|
11
|
+
"json",
|
|
12
|
+
"local",
|
|
13
|
+
"storage"
|
|
14
|
+
],
|
|
15
|
+
"author": "Dohyeon Ju",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"prepublishOnly": "pnpm build"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": ">=18",
|
|
34
|
+
"react-dom": ">=18"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/react": "^19.2.9",
|
|
38
|
+
"@types/react-dom": "^19.2.3",
|
|
39
|
+
"react": "^19.2.3",
|
|
40
|
+
"react-dom": "^19.2.3",
|
|
41
|
+
"tsup": "^8.5.1",
|
|
42
|
+
"typescript": "^5.9.3"
|
|
43
|
+
},
|
|
44
|
+
"packageManager": "pnpm@10.27.0",
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"superjson": "^2.2.6"
|
|
47
|
+
}
|
|
48
|
+
}
|