Socket
Socket
Sign inDemoInstall

react-hookstore

Package Overview
Dependencies
6
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.4.0 to 1.4.1

101

dist/react-hookstore.js

@@ -124,2 +124,4 @@ (function webpackUniversalModuleDefinition(root, factory) {

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

@@ -138,3 +140,5 @@

};
/** The public interface of a store */
var StoreInterface =

@@ -153,7 +157,40 @@ /*#__PURE__*/

this.subscribe = subscribe;
this.unsubscribe = unsubscribe;
this.subscribe = this.subscribe.bind(this);
}
/**
* Subscribe to store changes
* @callback callback - The function to be invoked everytime the store is updated
* @return {Function} - Call the function returned by the method to cancel the subscription
*/
/**
*
* @param {callback} state, action
*/
_createClass(StoreInterface, [{
key: "subscribe",
value: function subscribe(callback) {
var _this = this;
if (!callback || typeof callback !== 'function') {
throw "store.subscribe callback argument must be a function. got '".concat(_typeof(callback), "' instead.");
}
if (subscriptions[this.name].find(function (c) {
return c === callback;
})) {
console.warn('This callback is already subscribed to this store. skipping subscription');
return;
}
subscriptions[this.name].push(callback);
return function () {
subscriptions[_this.name] = subscriptions[_this.name].filter(function (c) {
return c !== callback;
});
};
}
}, {
key: "setState",

@@ -207,15 +244,16 @@ value: function setState() {

setState: function setState(action, callback) {
var _this = this;
var _this2 = this;
this.state = this.reducer(this.state, action);
this.setters.forEach(function (setter) {
return setter(_this.state);
return setter(_this2.state);
});
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);
if (subscriptions[name].length) {
subscriptions[name].forEach(function (c) {
return c(_this2.state, action);
});
}
if (typeof callback === 'function') callback(this.state);
},

@@ -225,2 +263,3 @@ setters: []

store.setState = store.setState.bind(store);
subscriptions[name] = [];
store.public = new StoreInterface(name, store, reducer !== defaultReducer);

@@ -232,3 +271,3 @@ stores = Object.assign({}, stores, _defineProperty({}, name, store));

* Returns a store instance based on its name
* @param {String} name - The name of the wanted store
* @callback {String} name - The name of the wanted store
* @returns {StoreInterface} the store instance

@@ -276,46 +315,2 @@ */

function subscribe(actions, callback) {
var _this2 = this;
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;
});
});
}
/***/ })

@@ -322,0 +317,0 @@ /******/ ]);

@@ -1,1 +0,1 @@

!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)})}}]);
!function(t){var e={};function s(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,s),n.l=!0,n.exports}s.m=t,s.c=e,s.d=function(t,e,r){s.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},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 r=Object.create(null);if(s.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)s.d(r,n,function(e){return t[e]}.bind(null,n));return r},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 r=s(0);let n={},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(n[t])throw"store already exists";const r={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 r.setState=r.setState.bind(r),o[t]=[],r.public=new a(t,r,s!==i),n=Object.assign({},n,{[t]:r}),r.public}function c(t){try{return n[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 n[e]}(t);if(!e)throw"store does not exist";const[s,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)}),[]),[s,e.setState]}}]);
{
"name": "react-hookstore",
"version": "1.4.0",
"version": "1.4.1",
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc