UNPKG

react-hookstore

Version:

A state management library for react using the bleeding edge hooks feature

1 lines 3.25 kB
!function(t){var e={};function r(s){if(e[s])return e[s].exports;var o=e[s]={i:s,l:!1,exports:{}};return t[s].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=t,r.c=e,r.d=function(t,e,s){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:s})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var s=Object.create(null);if(r.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(s,o,function(e){return t[e]}.bind(null,o));return s},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=1)}([function(t,e){t.exports=void 0},function(t,e,r){"use strict";r.r(e),r.d(e,"createStore",function(){return c}),r.d(e,"getStoreByName",function(){return u}),r.d(e,"useStore",function(){return f});var s=r(0);let o={},n={};const i=(t,e)=>e;class a{constructor(t,e,r){this.name=t,r?this.dispatch=e.setState:this.setState=e.setState,this.getState=(()=>e.state),this.subscribe=this.subscribe.bind(this)}subscribe(t){if(!t||"function"!=typeof t)throw new TypeError(`[React Hookstore] store.subscribe callback argument must be a function. got '${typeof t}' instead.`);if(!n[this.name].find(e=>e===t))return n[this.name].push(t),()=>{n[this.name]=n[this.name].filter(e=>e!==t)};console.warn("[React Hookstore] This callback is already subscribed to this store. skipping subscription")}setState(){console.warn(`[React Hookstore] Store ${this.name} uses a reducer to handle its state updates. use dispatch instead of setState`)}dispatch(){console.warn(`[React Hookstore] Store ${this.name} does not use a reducer to handle state updates. use setState instead of dispatch`)}}function c(t,e={},r=i){if("string"!=typeof t)throw new TypeError("[React Hookstore] Store name must be a string");o[t]&&console.warn(`[React Hookstore] Store with name ${t} already exists. Overriding`);const s={state:e,reducer:r,setState(e,r){this.state=this.reducer(this.state,e),this.setters.forEach(t=>{try{t(this.state)}catch(t){console.error(t),console.error("[React Hookstore] The error above was caused while React Hookstore was trying to call setState on a component. If you think this is a bug with React Hookstore, please file an issue https://github.com/jhonnymichel/react-hookstore/issues/new")}}),n[t].length&&n[t].forEach(t=>t(this.state,e)),"function"==typeof r&&r(this.state)},setters:[]};return s.setState=s.setState.bind(s),n[t]=[],s.public=new a(t,s,r!==i),o=Object.assign({},o,{[t]:s}),s.public}function u(t){try{return o[t].public}catch(e){return console.warn(`[React Hookstore] Store with name ${t} does not exist`),null}}function f(t){const e=function(t){const e=t instanceof a?t.name:t;if(!o[e])throw new Error(`[React Hookstore] Store with name ${e} does not exist`);return o[e]}(t),[r,n]=Object(s.useState)(e.state);return e.setters.includes(n)||e.setters.push(n),Object(s.useEffect)(()=>()=>{e.setters=e.setters.filter(t=>t!==n)},[]),[r,e.setState]}}]);