UNPKG

zustand-debounce

Version:

**Lightweight** and **powerful** extension for [Zustand](https://github.com/pmndrs/zustand) providing debounced JSON state storage.

1 lines 5.13 kB
'use strict';var middleware=require('zustand/middleware');var A=Object.defineProperty;var V=(i,e,t)=>e in i?A(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var g=(i,e,t)=>V(i,typeof e!="symbol"?e+"":e,t);var d=class{constructor(e){this.storageApi=e;}async getItem(e){return this.storageApi.getItem(e)}async setItem(e,t){return this.storageApi.setItem(e,t)}async removeItem(e){return this.storageApi.removeItem(e)}};var n=class{constructor(e){this.wrappedStorage=e;}async getItem(e){return this.wrappedStorage.getItem(e)}async setItem(e,t){await this.wrappedStorage.setItem(e,t);}async removeItem(e){await this.wrappedStorage.removeItem(e);}async flush(){this.wrappedStorage.flush&&await this.wrappedStorage.flush();}};var v=class extends n{constructor(t,r){super(t);g(this,"eventEmitter");this.eventEmitter=r;}on(t,r){this.eventEmitter.on(t,r);}async setItem(t,r){this.eventEmitter.emit("write",t,r),await this.wrappedStorage.setItem(t,r);}async getItem(t){let r=await this.wrappedStorage.getItem(t);return this.eventEmitter.emit("get",t,r),r}async removeItem(t){await this.wrappedStorage.removeItem(t),this.eventEmitter.emit("remove",t);}};var S=class extends n{constructor(t,r={},o){super(t);this.options=r;this.eventEmitter=o;}async setItem(t,r){let{maxRetries:o=3,retryDelay:s=1e3,backoffMultiplier:l=1}=this.options,a=0;for(;a<o;)try{await this.wrappedStorage.setItem(t,r);return}catch(m){if(a++,a>=o)throw this.eventEmitter&&this.eventEmitter.emit("error",t,m),m;let p=s*l**(a-1);this.eventEmitter&&this.eventEmitter.emit("retry",t,a,m,p),await new Promise(c=>setTimeout(c,p));}}};var f=class extends n{constructor(t,r){super(t);this.options=r;}async setItem(t,r){let o=this.options.serialize(r);await this.wrappedStorage.setItem(t,o);}async getItem(t){let r=await this.wrappedStorage.getItem(t);return r?this.options.deserialize(r):null}};var w=class extends n{constructor(t,r,o){super(t);this.options=r;g(this,"timeoutId");g(this,"pendingValue",null);g(this,"lastExecutionTime",0);g(this,"eventEmitter");this.eventEmitter=o;}async setItem(t,r){let{debounceTime:o=0,throttleTime:s=0,immediately:l=!1}=this.options;if(l){await this.wrappedStorage.setItem(t,r),this.eventEmitter.emit("save",t,r);return}let a=Date.now();s&&a-this.lastExecutionTime<s||(this.pendingValue={key:t,value:r},this.timeoutId&&clearTimeout(this.timeoutId),this.timeoutId=setTimeout(()=>{if(this.pendingValue!==null){let{key:m,value:p}=this.pendingValue;this.wrappedStorage.setItem(m,p).then(()=>{this.lastExecutionTime=Date.now(),this.eventEmitter.emit("save",m,p),this.pendingValue=null;});}},o));}async flush(){if(this.timeoutId&&(clearTimeout(this.timeoutId),this.timeoutId=void 0),this.pendingValue!==null){let{key:t,value:r}=this.pendingValue;await this.wrappedStorage.setItem(t,r),this.eventEmitter.emit("save",t,r),this.eventEmitter.emit("flush",t,r),this.pendingValue=null;}await this.wrappedStorage.flush?.();}};var h=class extends n{constructor(t,r){super(t);this.options=r;}async setItem(t,r){let o=Date.now()+this.options.ttl,s=JSON.stringify({value:r,expiresAt:o});await this.wrappedStorage.setItem(t,s);}async getItem(t){let r=await this.wrappedStorage.getItem(t);if(!r)return null;try{let{value:o,expiresAt:s}=JSON.parse(r);return Date.now()>s?(await this.removeItem(t),null):o}catch{return null}}};var I=class{constructor(){g(this,"events",{});}on(e,t){this.events[e]||(this.events[e]=[]),this.events[e].push(t);}emit(e,...t){if(this.events[e])for(let r of this.events[e])r(...t);}};function z(){let i={};return {getItem:async e=>i[e]||null,setItem:async(e,t)=>{i[e]=t;},removeItem:async e=>{delete i[e];}}}function O(i){if(typeof i=="object"&&i!==null&&typeof i.getItem=="function")return i;if(i==="localStorage"){if(typeof window<"u"&&window.localStorage)return {getItem:async e=>window.localStorage.getItem(e),setItem:async(e,t)=>window.localStorage.setItem(e,t),removeItem:async e=>window.localStorage.removeItem(e)};throw new Error("localStorage no est\xE1 disponible.")}if(i==="sessionStorage"){if(typeof window<"u"&&window.sessionStorage)return {getItem:async e=>window.sessionStorage.getItem(e),setItem:async(e,t)=>window.sessionStorage.setItem(e,t),removeItem:async e=>window.sessionStorage.removeItem(e)};throw new Error("sessionStorage no est\xE1 disponible.")}if(i==="memoryStorage")return z();throw new Error(`Adaptador de almacenamiento no soportado: ${i}`)}function R(i,e={}){let t=new I,r=new d(O(i)),{debounceTime:o,throttleTime:s,immediately:l,maxRetries:a,retryDelay:m,onWrite:p,onSave:c,serialize:E,deserialize:x,ttl:b,...D}=e;E&&x&&(r=new f(r,{serialize:E,deserialize:x})),typeof b=="number"&&(r=new h(r,{ttl:b})),(a||m)&&(r=new S(r,{maxRetries:a,retryDelay:m})),(o||s||l!==void 0)&&(r=new w(r,{debounceTime:o,throttleTime:s,immediately:l},t));let y=new v(r,t);p&&y.on("write",p),c&&y.on("save",c),r=y;let k={getItem:async u=>await r.getItem(u),setItem:async(u,P)=>{await r.setItem(u,P);},removeItem:async u=>{await r.removeItem(u);}},T=middleware.createJSONStorage(()=>k,D);if(T===void 0)throw new Error("createJSONStorage returned undefined");return T}exports.createDebouncedJSONStorage=R;