react-hookstore
Advanced tools
Comparing version 1.4.4 to 1.5.0
@@ -175,3 +175,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
if (!callback || typeof callback !== 'function') { | ||
throw "store.subscribe callback argument must be a function. got '".concat(_typeof(callback), "' instead."); | ||
throw new TypeError("[React Hookstore] store.subscribe callback argument must be a function. got '".concat(_typeof(callback), "' instead.")); | ||
} | ||
@@ -182,3 +182,3 @@ | ||
})) { | ||
console.warn('This callback is already subscribed to this store. skipping subscription'); | ||
console.warn('[React Hookstore] This callback is already subscribed to this store. skipping subscription'); | ||
return; | ||
@@ -213,3 +213,3 @@ } | ||
if (!stores[name]) { | ||
throw "Store with name ".concat(name, " does not exist"); | ||
throw new Error("[React Hookstore] Store with name ".concat(name, " does not exist")); | ||
} | ||
@@ -224,2 +224,3 @@ | ||
* @callback reducer [null] | ||
* @param {Boolean} overrideIfExists - It'll override an existent store with the same name. useful for SSR. | ||
* @returns {StoreInterface} The store instance. | ||
@@ -239,7 +240,7 @@ */ | ||
if (typeof name !== 'string') { | ||
throw 'Store name must be a string'; | ||
throw new TypeError('[React Hookstore] Store name must be a string'); | ||
} | ||
if (stores[name]) { | ||
throw "Store with name ".concat(name, " already exists"); | ||
console.warn("[React Hookstore] Store with name ".concat(name, " already exists. Overriding")); | ||
} | ||
@@ -254,4 +255,9 @@ | ||
this.state = this.reducer(this.state, action); | ||
this.setters.forEach(function (setter) { | ||
return setter(_this2.state); | ||
this.setters.forEach(function (set) { | ||
try { | ||
set(_this2.state); | ||
} catch (e) { | ||
console.error(e); | ||
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'); | ||
} | ||
}); | ||
@@ -285,3 +291,4 @@ | ||
} catch (e) { | ||
throw "Store with name ".concat(name, " does not exist"); | ||
console.warn("[React Hookstore] Store with name ".concat(name, " does not exist")); | ||
return null; | ||
} | ||
@@ -303,7 +310,7 @@ } | ||
if (!store.setters.includes(set)) { | ||
store.setters.push(set); | ||
} | ||
Object(react__WEBPACK_IMPORTED_MODULE_0__["useEffect"])(function () { | ||
if (!store.setters.includes(set)) { | ||
store.setters.push(set); | ||
} | ||
return function () { | ||
@@ -310,0 +317,0 @@ store.setters = store.setters.filter(function (setter) { |
@@ -1,1 +0,1 @@ | ||
!function(t){var e={};function s(n){if(e[n])return e[n].exports;var r=e[n]={i:n,l:!1,exports:{}};return t[n].call(r.exports,r,r.exports,s),r.l=!0,r.exports}s.m=t,s.c=e,s.d=function(t,e,n){s.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},s.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},s.t=function(t,e){if(1&e&&(t=s(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(s.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)s.d(n,r,function(e){return t[e]}.bind(null,r));return n},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=1)}([function(t,e){t.exports=void 0},function(t,e,s){"use strict";s.r(e),s.d(e,"createStore",function(){return u}),s.d(e,"getStoreByName",function(){return c}),s.d(e,"useStore",function(){return f});var n=s(0);let r={},o={};const i=(t,e)=>e;class a{constructor(t,e,s){this.name=t,s?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`store.subscribe callback argument must be a function. got '${typeof t}' instead.`;if(!o[this.name].find(e=>e===t))return o[this.name].push(t),()=>{o[this.name]=o[this.name].filter(e=>e!==t)};console.warn("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 u(t,e={},s=i){if("string"!=typeof t)throw"Store name must be a string";if(r[t])throw`Store with name ${t} already exists`;const n={state:e,reducer:s,setState(e,s){this.state=this.reducer(this.state,e),this.setters.forEach(t=>t(this.state)),o[t].length&&o[t].forEach(t=>t(this.state,e)),"function"==typeof s&&s(this.state)},setters:[]};return n.setState=n.setState.bind(n),o[t]=[],n.public=new a(t,n,s!==i),r=Object.assign({},r,{[t]:n}),n.public}function c(t){try{return r[t].public}catch(e){throw`Store with name ${t} does not exist`}}function f(t){const e=function(t){const e=t instanceof a?t.name:t;if(!r[e])throw`Store with name ${e} does not exist`;return r[e]}(t),[s,o]=Object(n.useState)(e.state);return Object(n.useEffect)(()=>(e.setters.includes(o)||e.setters.push(o),()=>{e.setters=e.setters.filter(t=>t!==o)}),[]),[s,e.setState]}}]); | ||
!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]}}]); |
{ | ||
"name": "react-hookstore", | ||
"version": "1.4.4", | ||
"version": "1.5.0", | ||
"description": "A state management library for react using the bleeding edge hooks feature", | ||
@@ -5,0 +5,0 @@ "main": "dist/react-hookstore.js", |
@@ -1,2 +0,2 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { useState, useEffect, useRef } from 'react'; | ||
@@ -30,6 +30,6 @@ let stores = {}; | ||
if (!callback || typeof callback !== 'function') { | ||
throw `store.subscribe callback argument must be a function. got '${typeof callback}' instead.`; | ||
throw new TypeError(`[React Hookstore] store.subscribe callback argument must be a function. got '${typeof callback}' instead.`); | ||
} | ||
if (subscriptions[this.name].find(c => c === callback)) { | ||
console.warn('This callback is already subscribed to this store. skipping subscription'); | ||
console.warn('[React Hookstore] This callback is already subscribed to this store. skipping subscription'); | ||
return; | ||
@@ -55,3 +55,3 @@ } | ||
if (!stores[name]) { | ||
throw `Store with name ${name} does not exist`; | ||
throw new Error(`[React Hookstore] Store with name ${name} does not exist`); | ||
} | ||
@@ -66,2 +66,3 @@ return stores[name]; | ||
* @callback reducer [null] | ||
* @param {Boolean} overrideIfExists - It'll override an existent store with the same name. useful for SSR. | ||
* @returns {StoreInterface} The store instance. | ||
@@ -76,6 +77,7 @@ */ | ||
if (typeof name !== 'string') { | ||
throw 'Store name must be a string'; | ||
throw new TypeError('[React Hookstore] Store name must be a string'); | ||
} | ||
if (stores[name]) { | ||
throw `Store with name ${name} already exists`; | ||
console.warn(`[React Hookstore] Store with name ${name} already exists. Overriding`); | ||
} | ||
@@ -88,3 +90,10 @@ | ||
this.state = this.reducer(this.state, action); | ||
this.setters.forEach(setter => setter(this.state)); | ||
this.setters.forEach(set => { | ||
try { | ||
set(this.state) | ||
} catch(e) { | ||
console.error(e) | ||
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') | ||
} | ||
}); | ||
if (subscriptions[name].length) { | ||
@@ -115,3 +124,4 @@ subscriptions[name].forEach(c => c(this.state, action)); | ||
} catch(e) { | ||
throw `Store with name ${name} does not exist`; | ||
console.warn(`[React Hookstore] Store with name ${name} does not exist`); | ||
return null; | ||
} | ||
@@ -130,9 +140,7 @@ } | ||
if (!store.setters.includes(set)) { | ||
store.setters.push(set); | ||
} | ||
useEffect(() => { | ||
if (!store.setters.includes(set)) { | ||
store.setters.push(set); | ||
} | ||
set(store.state); | ||
return () => { | ||
@@ -139,0 +147,0 @@ store.setters = store.setters.filter(setter => setter !== set) |
@@ -16,8 +16,8 @@ import { createStore, getStoreByName, subscribe, unsubscribe } from '..'; | ||
it('Should not allow stores with the same name to be created', () => { | ||
it('Should warn about overriding an existing store', () => { | ||
const consoleWarn = jest.spyOn(global.console, 'warn'); | ||
const store = createStore('unique', 0); | ||
expect(store).toBeTruthy(); | ||
expect(() => { | ||
createStore('unique'); | ||
}).toThrow(); | ||
createStore('unique'); | ||
expect(consoleWarn).toHaveBeenCalled(); | ||
}); | ||
@@ -41,6 +41,6 @@ | ||
it('Should throw an error if store does not exist', () => { | ||
expect(() => { | ||
getStoreByName('Unexistent store'); | ||
}).toThrow(); | ||
it('Should return null if store does not exist', () => { | ||
expect( | ||
getStoreByName('Unexistent store') | ||
).toEqual(null); | ||
}); | ||
@@ -47,0 +47,0 @@ }); |
Sorry, the diff of this file is not supported yet
58529
731