stimulus-store
Version:
Lightweight state management for Stimulus.js
3 lines (2 loc) • 3.23 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).StimulusStore={})}(this,(function(e){"use strict";function t(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 r={String:String,Number:Number,Array:Array,Object:Object,Boolean:Boolean};class s{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)}}}e.createStore=function(e){const t=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")}(t),function(e){if("function"!=typeof r[e])throw new Error(`Invalid type: ${e}`)}(o.name);const i=Symbol(t),a=new s(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},e.useStore=function(e){const r=e.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=t(o),i=`on${t(o,!0)}Update`,a=e[i],c=r.getSubscription();if(a){const r=t=>{a.call(e,t)},n=`update${t(o,!0)}`;e[n]=r,s.push(c.subscribe(r))}const u=`set${t(o,!0)}Value`;e[u]=e=>{r.set(e)};const l=`reset${t(o,!0)}`;e[l]=()=>{r.resetValue()};const f=`${n}Value`;Object.defineProperty(e,f,{get:()=>r.get(),enumerable:!0,configurable:!0});let h=!1;const b=new Proxy(r,{get:(e,t,r)=>(h=function(e,t){return t||!0}(0,h),Reflect.get(e,t,r))});Object.defineProperty(e,n,{get:()=>b,enumerable:!0,configurable:!0})}));const o=e.disconnect.bind(e);e.disconnect=()=>{s.forEach((e=>{e&&e()})),o()}}}));
//# sourceMappingURL=bundle.umd.js.map