use-idb-storage 0.0.7 → 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.d.ts +18 -13
- package/dist/index.js +1 -1
- package/package.json +3 -1
package/dist/index.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ interface IDBConfigProps extends Partial<IDBConfigValues> {
|
|
|
31
31
|
/**
|
|
32
32
|
* Return type of the useIDBStorage hook
|
|
33
33
|
*/
|
|
34
|
-
type UseIDBStorageReturn<T> = [T, (value: T | ((prevState: T) => T)) =>
|
|
34
|
+
type UseIDBStorageReturn<T> = [T, (value: T | ((prevState: T) => T)) => void, () => void];
|
|
35
35
|
//#endregion
|
|
36
36
|
//#region src/idb-storage.d.ts
|
|
37
37
|
/**
|
|
@@ -121,9 +121,20 @@ declare class IDBStorage {
|
|
|
121
121
|
close(): void;
|
|
122
122
|
}
|
|
123
123
|
//#endregion
|
|
124
|
+
//#region src/config.d.ts
|
|
125
|
+
/**
|
|
126
|
+
* Configure global defaults for IDBStorage.
|
|
127
|
+
*/
|
|
128
|
+
declare function configureIDBStorage(config: Partial<IDBConfigValues>): void;
|
|
129
|
+
/**
|
|
130
|
+
* Get the current global configuration.
|
|
131
|
+
*/
|
|
132
|
+
declare function getGlobalConfig(): IDBConfigValues;
|
|
133
|
+
//#endregion
|
|
124
134
|
//#region src/hook.d.ts
|
|
125
135
|
/**
|
|
126
136
|
* Hook to persist state in IndexedDB with a clean object-based API.
|
|
137
|
+
* Optimized for performance - synchronous updates like useState with background IDB persistence.
|
|
127
138
|
*
|
|
128
139
|
* @param options - Configuration object containing key, defaultValue, and optional database/store names
|
|
129
140
|
* @returns A tuple of the stored value, an async updater function, and a remove function.
|
|
@@ -160,9 +171,13 @@ declare class IDBStorage {
|
|
|
160
171
|
declare function useIDBStorage<T>(options: IDBStorageOptions<T>): UseIDBStorageReturn<T>;
|
|
161
172
|
//#endregion
|
|
162
173
|
//#region src/idb-context.d.ts
|
|
174
|
+
/**
|
|
175
|
+
* Hook to get the current IDB config from context or global fallback
|
|
176
|
+
*/
|
|
177
|
+
declare function useIDBConfig(): IDBConfigValues;
|
|
163
178
|
/**
|
|
164
179
|
* Provider component to configure default IDBStorage settings.
|
|
165
|
-
* This
|
|
180
|
+
* This passes config to children via context instead of global state.
|
|
166
181
|
*
|
|
167
182
|
* @param props - Configuration props containing database, store, and children
|
|
168
183
|
* @returns The provider component wrapping children
|
|
@@ -179,17 +194,7 @@ declare function IDBConfig({
|
|
|
179
194
|
...conf
|
|
180
195
|
}: IDBConfigProps): react_jsx_runtime0.JSX.Element;
|
|
181
196
|
//#endregion
|
|
182
|
-
//#region src/config.d.ts
|
|
183
|
-
/**
|
|
184
|
-
* Configure global defaults for IDBStorage.
|
|
185
|
-
*/
|
|
186
|
-
declare function configureIDBStorage(config: Partial<IDBConfigValues>): void;
|
|
187
|
-
/**
|
|
188
|
-
* Get the current global configuration.
|
|
189
|
-
*/
|
|
190
|
-
declare function getGlobalConfig(): IDBConfigValues;
|
|
191
|
-
//#endregion
|
|
192
197
|
//#region src/index.d.ts
|
|
193
198
|
declare const idb: IDBStorage;
|
|
194
199
|
//#endregion
|
|
195
|
-
export { IDBConfig, type IDBConfigProps, type IDBConfigValues, IDBStorage, type IDBStorageOptions, IDBStore, type UseIDBStorageReturn, configureIDBStorage, getGlobalConfig, idb, useIDBStorage };
|
|
200
|
+
export { IDBConfig, type IDBConfigProps, type IDBConfigValues, IDBStorage, type IDBStorageOptions, IDBStore, type UseIDBStorageReturn, configureIDBStorage, getGlobalConfig, idb, useIDBConfig, useIDBStorage };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as e from"react";import{
|
|
1
|
+
import*as e from"react";import{jsx as t}from"react/jsx-runtime";const n=new Map;function r(){return typeof indexedDB<`u`}function i(e,t,i=1,o){if(!r())throw Error(`IndexedDB is not available in this environment`);let s=`${e}:${i}:${t}`;if(n.has(s))return n.get(s);let c=a(e,t,i,o);return n.set(s,c),c}function a(e,t,r,i){return new Promise((o,s)=>{let c=indexedDB.open(e,r);c.onerror=()=>{n.delete(`${e}:${r}:${t}`),s(c.error)},c.onupgradeneeded=e=>{let n=e.target.result;n.objectStoreNames.contains(t)||n.createObjectStore(t)},c.onsuccess=()=>{let s=c.result;if(!s.objectStoreNames.contains(t)){s.close(),n.delete(`${e}:${r}:${t}`),o(a(e,t,r+1,i));return}s.onversionchange=()=>{s.close();for(let[t]of n)t.startsWith(`${e}:`)&&n.delete(t);i?.()},o(s)}})}function o(e,t,n){return new Promise((r,i)=>{try{let a=e.transaction([t],`readonly`).objectStore(t).get(n);a.onerror=()=>i(a.error),a.onsuccess=()=>r(a.result)}catch(e){i(e)}})}function s(e,t,n,r){return new Promise((i,a)=>{try{let o=e.transaction([t],`readwrite`).objectStore(t).put(r,n);o.onerror=()=>a(o.error),o.onsuccess=()=>i()}catch(e){a(e)}})}function c(e,t,n){return new Promise((r,i)=>{try{let a=e.transaction([t],`readwrite`).objectStore(t).delete(n);a.onerror=()=>i(a.error),a.onsuccess=()=>r()}catch(e){i(e)}})}var l=class{db;storeName;constructor(e,t){this.db=e,this.storeName=t}async get(e){try{return await o(this.db,this.storeName,e)}catch(t){throw console.error(`Failed to get value for key "${e}":`,t),t}}async set(e,t){try{await s(this.db,this.storeName,e,t)}catch(t){throw console.error(`Failed to set value for key "${e}":`,t),t}}async delete(e){try{await c(this.db,this.storeName,e)}catch(t){throw console.error(`Failed to delete key "${e}":`,t),t}}async getMany(e){try{let t=e.map(e=>this.get(e));return await Promise.all(t)}catch(e){throw console.error(`Failed to get multiple values:`,e),e}}async setMany(e){try{let t=this.db.transaction([this.storeName],`readwrite`).objectStore(this.storeName),n=e.map(([e,n])=>new Promise((r,i)=>{let a=t.put(n,e);a.onerror=()=>i(a.error),a.onsuccess=()=>r()}));await Promise.all(n)}catch(e){throw console.error(`Failed to set multiple values:`,e),e}}async deleteMany(e){try{let t=this.db.transaction([this.storeName],`readwrite`).objectStore(this.storeName),n=e.map(e=>new Promise((n,r)=>{let i=t.delete(e);i.onerror=()=>r(i.error),i.onsuccess=()=>n()}));await Promise.all(n)}catch(e){throw console.error(`Failed to delete multiple keys:`,e),e}}async update(e,t){try{let n=t(await this.get(e));await this.set(e,n)}catch(t){throw console.error(`Failed to update value for key "${e}":`,t),t}}async clear(){try{let e=this.db.transaction([this.storeName],`readwrite`).objectStore(this.storeName).clear();await new Promise((t,n)=>{e.onerror=()=>n(e.error),e.onsuccess=()=>t()})}catch(e){throw console.error(`Failed to clear store:`,e),e}}async keys(){try{let e=this.db.transaction([this.storeName],`readonly`).objectStore(this.storeName).getAllKeys();return await new Promise((t,n)=>{e.onerror=()=>n(e.error),e.onsuccess=()=>t(Array.from(e.result))})}catch(e){throw console.error(`Failed to get keys:`,e),e}}async values(){try{let e=this.db.transaction([this.storeName],`readonly`).objectStore(this.storeName).getAll();return await new Promise((t,n)=>{e.onerror=()=>n(e.error),e.onsuccess=()=>t(e.result)})}catch(e){throw console.error(`Failed to get values:`,e),e}}async entries(){try{let e=this.db.transaction([this.storeName],`readonly`).objectStore(this.storeName).openCursor(),t=[];return new Promise((n,r)=>{e.onerror=()=>r(e.error),e.onsuccess=()=>{let r=e.result;r?(t.push([r.key,r.value]),r.continue()):n(t)}})}catch(e){throw console.error(`Failed to get entries:`,e),e}}},u=class{config;db=null;dbPromise=null;constructor(e){this.config={database:`sohanemon-idb`,version:1,store:`default`,...e},this.config.version=Math.max(1,Math.floor(this.config.version))}async getDB(){return this.db?this.db:this.dbPromise?this.dbPromise:(this.dbPromise=i(this.config.database,this.config.store,this.config.version,()=>{this.db=null,this.dbPromise=null}),this.db=await this.dbPromise,this.dbPromise=null,this.db)}async get(e){return new l(await this.getDB(),e)}get store(){return this.get(this.config.store)}async drop(e){await(await this.get(e)).clear()}close(){this.db&&=(this.db.close(),null)}};let d={database:`sohanemon-idb`,version:1,store:`default`};function f(e){d={...d,...e}}function p(){return{...d}}const m=e.createContext(null);function h(){return e.useContext(m)||p()}function g({children:e,...n}){let r={database:n.database||`sohanemon-idb`,version:n.version||1,store:n.store||`default`};return t(m.Provider,{value:r,children:e})}function _(t){let{key:n,defaultValue:i,...a}=t,o=h(),s=a.database||o.database,c=Math.max(1,Math.floor(a.version||o.version||1)),l=a.store||o.store,[d,f]=e.useState(i),p=e.useRef(!1),m=e.useRef(null),g=e.useRef(null),_=e.useRef(null),v=e.useRef(null),y=e.useRef(!1);e.useEffect(()=>{if(typeof window>`u`)return;let e=!0;return(async()=>{try{if(!r()){console.warn(`IndexedDB is not available, using default values only`),p.current=!0,y.current=!0;return}m.current&&m.current.close();let t=new u({database:s,version:c,store:l}),i=await t.get(l);if(!e)return;m.current=t,g.current=i;let a=await i.get(n);e&&a!==void 0&&f(a),p.current=!0,y.current=!0}catch(e){console.error(`Failed to initialize IDBStorage:`,e),p.current=!0,y.current=!0}})(),()=>{e=!1,_.current&&(clearTimeout(_.current),v.current!==null&&g.current&&g.current.set(n,v.current).catch(console.error)),m.current&&(m.current.close(),m.current=null,g.current=null)}},[s,c,l,n]);let b=e.useCallback(e=>{!p.current||!g.current||!y.current||(v.current=e,_.current&&clearTimeout(_.current),_.current=setTimeout(()=>{let e=v.current;v.current=null,e!==null&&g.current&&g.current.set(n,e).catch(e=>{console.error(`Failed to save value to IndexedDB:`,e)})},0))},[n]);return[d,e.useCallback(e=>{f(t=>{let n=typeof e==`function`?e(t):e;return b(n),n})},[b]),e.useCallback(()=>{f(i),b(i)},[i,b])]}const v=new u;export{g as IDBConfig,u as IDBStorage,l as IDBStore,f as configureIDBStorage,p as getGlobalConfig,v as idb,h as useIDBConfig,_ as useIDBStorage};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-idb-storage",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"author": "Sohan Emon <sohanemon@outlook.com>",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"test:list": "vitest list",
|
|
20
20
|
"test:log": "vitest run --reporter verbose",
|
|
21
21
|
"test:ui": "vitest --ui",
|
|
22
|
+
"bench": "vitest bench",
|
|
22
23
|
"build:playground": "vite build --config playground/vite.config.ts",
|
|
23
24
|
"playground": "tsdown && vite build --config playground/vite.config.ts && vite --config playground/vite.config.ts",
|
|
24
25
|
"check": "biome check .",
|
|
@@ -57,6 +58,7 @@
|
|
|
57
58
|
"@vitest/ui": "^4.0.14",
|
|
58
59
|
"fake-indexeddb": "^6.2.5",
|
|
59
60
|
"jsdom": "^27.2.0",
|
|
61
|
+
"react-scan": "^0.4.3",
|
|
60
62
|
"tsdown": "^0.16.4",
|
|
61
63
|
"typescript": "^5.9.3",
|
|
62
64
|
"vite": "npm:rolldown-vite@^7.2.5",
|