react-hookstore
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -132,2 +132,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var stores = {}; | ||
var subscriptions = {}; | ||
@@ -150,2 +151,5 @@ var defaultReducer = function defaultReducer(state, payload) { | ||
}; | ||
this.subscribe = subscribe; | ||
this.unsubscribe = unsubscribe; | ||
} | ||
@@ -209,2 +213,8 @@ | ||
if (typeof callback === 'function') callback(this.state); | ||
if (action && action.type && subscriptions[action.type]) { | ||
subscriptions[action.type].forEach(function (subscription) { | ||
return subscription.name === name && subscription.callback(action, _this.state); | ||
}); | ||
} | ||
}, | ||
@@ -250,2 +260,6 @@ setters: [] | ||
Object(react__WEBPACK_IMPORTED_MODULE_0__["useEffect"])(function () { | ||
if (!store.setters.includes(set)) { | ||
store.setters.push(set); | ||
} | ||
return function () { | ||
@@ -257,10 +271,49 @@ store.setters = store.setters.filter(function (setter) { | ||
}, []); | ||
return [state, store.setState]; | ||
} | ||
if (!store.setters.includes(set)) { | ||
store.setters.push(set); | ||
} | ||
function subscribe(actions, callback) { | ||
var _this2 = this; | ||
return [state, store.setState]; | ||
if (!actions || !Array.isArray(actions)) throw 'first argument must be an array'; | ||
if (!callback || typeof callback !== 'function') throw 'second argument must be a function'; | ||
if (subscriberExists(this.name)) throw 'you are already subscribing to this store. unsubscribe to configure a new subscription.'; | ||
actions.forEach(function (action) { | ||
if (!subscriptions[action]) { | ||
subscriptions[action] = []; | ||
} | ||
subscriptions[action].push({ | ||
callback: callback, | ||
name: _this2.name | ||
}); | ||
}); | ||
} | ||
function unsubscribe() { | ||
var _this3 = this; | ||
var keys = Object.keys(subscriptions); | ||
keys.forEach(function (key) { | ||
if (subscriptions[key].length === 1) { | ||
delete subscriptions[key]; | ||
} else { | ||
subscriptions[key] = subscriptions[key].filter(function (action, i) { | ||
return action.name !== _this3.name; | ||
}); | ||
} | ||
}); | ||
} | ||
; | ||
function subscriberExists(name) { | ||
var keys = Object.keys(subscriptions); | ||
return keys.find(function (key) { | ||
return subscriptions[key].find(function (action) { | ||
return action && action.name === name; | ||
}); | ||
}); | ||
} | ||
/***/ }) | ||
@@ -267,0 +320,0 @@ /******/ ]); |
@@ -1,1 +0,1 @@ | ||
!function(t){var e={};function r(s){if(e[s])return e[s].exports;var n=e[s]={i:s,l:!1,exports:{}};return t[s].call(n.exports,n,n.exports,r),n.l=!0,n.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 n in t)r.d(s,n,function(e){return t[e]}.bind(null,n));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 i}),r.d(e,"getStoreByName",function(){return u}),r.d(e,"useStore",function(){return c});var s=r(0);let n={};const o=(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)}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 i(t,e={},r=o){if("string"!=typeof t)throw"store name must be a string";if(n[t])throw"store already exists";const s={state:e,reducer:r,setState(t,e){this.state=this.reducer(this.state,t),this.setters.forEach(t=>t(this.state)),"function"==typeof e&&e(this.state)},setters:[]};return s.setState=s.setState.bind(s),s.public=new a(t,s,r!==o),n=Object.assign({},n,{[t]:s}),s.public}function u(t){try{return n[t].public}catch(t){throw"store does not exist"}}function c(t){const e=function(t){const e=t instanceof a?t.name:t;return n[e]}(t);if(!e)throw"store does not exist";const[r,o]=Object(s.useState)(e.state);return Object(s.useEffect)(()=>()=>{e.setters=e.setters.filter(t=>t!==o)},[]),e.setters.includes(o)||e.setters.push(o),[r,e.setState]}}]); | ||
!function(t){var e={};function n(r){if(e[r])return e[r].exports;var s=e[r]={i:r,l:!1,exports:{}};return t[r].call(s.exports,s,s.exports,n),s.l=!0,s.exports}n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)n.d(r,s,function(e){return t[e]}.bind(null,s));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=1)}([function(t,e){t.exports=void 0},function(t,e,n){"use strict";n.r(e),n.d(e,"createStore",function(){return u}),n.d(e,"getStoreByName",function(){return c}),n.d(e,"useStore",function(){return f});var r=n(0);let s={},o={};const i=(t,e)=>e;class a{constructor(t,e,n){this.name=t,n?this.dispatch=e.setState:this.setState=e.setState,this.getState=(()=>e.state),this.subscribe=d,this.unsubscribe=l}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={},n=i){if("string"!=typeof t)throw"store name must be a string";if(s[t])throw"store already exists";const r={state:e,reducer:n,setState(e,n){this.state=this.reducer(this.state,e),this.setters.forEach(t=>t(this.state)),"function"==typeof n&&n(this.state),e&&e.type&&o[e.type]&&o[e.type].forEach(n=>n.name===t&&n.callback(e,this.state))},setters:[]};return r.setState=r.setState.bind(r),r.public=new a(t,r,n!==i),s=Object.assign({},s,{[t]:r}),r.public}function c(t){try{return s[t].public}catch(t){throw"store does not exist"}}function f(t){const e=function(t){const e=t instanceof a?t.name:t;return s[e]}(t);if(!e)throw"store does not exist";const[n,o]=Object(r.useState)(e.state);return Object(r.useEffect)(()=>(e.setters.includes(o)||e.setters.push(o),()=>{e.setters=e.setters.filter(t=>t!==o)}),[]),[n,e.setState]}function d(t,e){if(!t||!Array.isArray(t))throw"first argument must be an array";if(!e||"function"!=typeof e)throw"second argument must be a function";if(function(t){return Object.keys(o).find(e=>o[e].find(e=>e&&e.name===t))}(this.name))throw"you are already subscribing to this store. unsubscribe to configure a new subscription.";t.forEach(t=>{o[t]||(o[t]=[]),o[t].push({callback:e,name:this.name})})}function l(){Object.keys(o).forEach(t=>{1===o[t].length?delete o[t]:o[t]=o[t].filter((t,e)=>t.name!==this.name)})}}]); |
{ | ||
"name": "react-hookstore", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "A state management library for react using the bleeding edge hooks feature", | ||
@@ -5,0 +5,0 @@ "main": "dist/react-hookstore.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39237
276