stimulus-store
Version:
Lightweight state management for Stimulus.js
3 lines (2 loc) • 3 kB
JavaScript
;function e(e,t=!1){return e.replace(/(?:^\w|[A-Z]|\b\w)/g,((e,r)=>0!==r||t?e.toUpperCase():e.toLowerCase())).replace(/\s+/g,"")}const t={String:String,Number:Number,Array:Array,Object:Object,Boolean:Boolean};class r{constructor(e,t){this.name=e,this.subscribers=new Set,this.type=t}async set(e,t={filter:()=>!0}){if(e instanceof Promise)return this.resolvePromise(e,t);if(e===this.get())return;const r="function"==typeof e?e(this.get()):e;!function(e,t){if(Object.getPrototypeOf(e).constructor!==t)throw new Error(`Value '${e}' must be of type ${t.name}`)}(r,this.type),this.setInitialValue(r),this.setValue(r),this.notifySubscribers(t)}get(){return this.value}resetValue(){this.setValue(this.initialValue)}setValue(e){this.value=e}setInitialValue(e){void 0===this.value&&(this.initialValue=e)}subscribe(e){return this.subscribers.add(e),e(this.get()),()=>this.unsubscribe(e)}unsubscribe(e){this.subscribers.delete(e)}getSubscription(){return{subscribe:e=>this.subscribe(e)}}notifySubscribers(e){Array.from(this.subscribers).filter((()=>e.filter(this.get()))).forEach((e=>e(this.get())))}async resolvePromise(e,t){try{const r=await e;this.set(r,t)}catch(e){!function(e){throw new Error(`Failed to resolve promise:\n${e}`)}(e)}}}exports.createStore=function(e){const s=e.name,o=e.type,n=e.initialValue;!function(e){if(void 0===e)throw new Error("Store must be initialized with a value")}(n),function(e){if("string"!=typeof e)throw new Error("Store name must be of Type string")}(s),function(e){if("function"!=typeof t[e])throw new Error(`Invalid type: ${e}`)}(o.name);const i=Symbol(s),a=new r(i,o);return a.set(n).catch((e=>function(e){if(e instanceof Error)throw new Error(`Failed to create store: ${e.message}`);throw new Error("An unknown error occurred while creating the store")}(e))),a},exports.useStore=function(t){const r=t.constructor?.stores,s=[];!function(e){if(!e)throw new Error("'useStore' was called on a controller without a 'stores' static property. The 'stores' property is undefined.");if(!Array.isArray(e))throw new Error("'useStore' was called on a controller with a 'stores' static property that is not an array.");if(0===e.length)throw new Error("'useStore' was called on a controller with an empty 'stores' static property. The 'stores' array should contain at least one store.")}(r),r?.forEach((r=>{const o=r.name.toString().slice(7,-1),n=e(o),i=`on${e(o,!0)}Update`,a=t[i],c=r.getSubscription();if(a){const r=e=>{a.call(t,e)},n=`update${e(o,!0)}`;t[n]=r,s.push(c.subscribe(r))}const u=`set${e(o,!0)}Value`;t[u]=e=>{r.set(e)};const l=`reset${e(o,!0)}`;t[l]=()=>{r.resetValue()};const h=`${n}Value`;Object.defineProperty(t,h,{get:()=>r.get(),enumerable:!0,configurable:!0});let f=!1;const b=new Proxy(r,{get:(e,t,r)=>(f=function(e,t){return t||!0}(0,f),Reflect.get(e,t,r))});Object.defineProperty(t,n,{get:()=>b,enumerable:!0,configurable:!0})}));const o=t.disconnect.bind(t);t.disconnect=()=>{s.forEach((e=>{e&&e()})),o()}};
//# sourceMappingURL=bundle.cjs.js.map