Comparing version 1.2.0 to 1.2.1
@@ -33,20 +33,2 @@ 'use strict'; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
var EventKey; | ||
@@ -87,5 +69,6 @@ | ||
this.setState = function (key, value) { | ||
var nextState = _this.getNextState(value); | ||
this.setState = function (key, state, value) { | ||
var nextState = _this.getNextState(state, value); | ||
_this.state = nextState; | ||
emitStoreUpdate({ | ||
@@ -109,3 +92,3 @@ key: key, | ||
_proto.getNextState = function getNextState(value) { | ||
_proto.getNextState = function getNextState(state, value) { | ||
var nextState; // not function | ||
@@ -115,5 +98,5 @@ | ||
if (typeof this.state !== 'object') return value(this.state); | ||
if (typeof state !== 'object') return value(state); | ||
var useImmer = true; | ||
var immerState = immer.produce(this.state, function (draft) { | ||
var immerState = immer.produce(state, function (draft) { | ||
var fnValue = value(draft); | ||
@@ -133,3 +116,3 @@ if (fnValue === draft) return; // do nothing | ||
return _extends({}, nextState); | ||
return nextState; | ||
}; | ||
@@ -173,3 +156,3 @@ | ||
return function (value) { | ||
return newStore.setState(key, value); | ||
return newStore.setState(key, state, value); | ||
}; | ||
@@ -203,3 +186,3 @@ } | ||
if (store && store.setState) { | ||
store.setState(key, nextValue); | ||
store.setState(key, store.state, nextValue); | ||
} else { | ||
@@ -206,0 +189,0 @@ // init state, if no store exist |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e,r=require("react"),n=require("immer"),o=(t=require("mitt"))&&"object"==typeof t&&"default"in t?t.default:t,s=function(){function t(){}return t.set=function(e,r){var n=t.stores[e];n&&n.setState||(t.stores[e]=r)},t.get=function(e){return t.stores[e]},t}();function u(){return(u=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}s.stores={},function(t){t.STORE_INITED="STORE_INITED",t.STORE_UPDATED="STORE_UPDATED"}(e||(e={}));var i=o(),a=function(){function t(t){var r=this;this.setters=[],this.setState=function(t,n){var o=r.getNextState(n);return i.emit(e.STORE_UPDATED,{key:t,nextState:o}),r.state=o,r.setters.forEach((function(t){return t(o)})),o},this.state=t}return t.prototype.getNextState=function(t){var e;if("function"!=typeof t)return t;if("object"!=typeof this.state)return t(this.state);var r=!0,o=n.produce(this.state,(function(n){var o=t(n);o!==n&&o&&"object"==typeof o&&(e=o,r=!1)}));return r&&(e=o),u({},e)},t}();exports.Storage=s,exports.getState=function(t){var e=s.get(t);return e?e.state:void 0},exports.mutate=function(t,e){var r=s.get(t);r&&r.setState?r.setState(t,e):s.set(t,{state:e})},exports.onStoreInit=function(t){i.on(e.STORE_INITED,(function(e){t(e)}))},exports.onStoreUpdate=function(t){i.on(e.STORE_UPDATED,(function(e){t(e)}))},exports.useStore=function(t,n){var o=s.get(t),u=o?o.state:n;s.set(t,new a(u));var f=s.get(t),c=r.useState(u),p=c[0],S=c[1],E=f.setters;return r.useEffect((function(){return E.push(S),function(t){i.emit(e.STORE_INITED,t)}(t),function(){E.splice(E.indexOf(S),1)}}),[E,S]),[p,function(t){return function(e){return f.setState(t,e)}}(t)]}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e,n=require("react"),r=require("immer"),o=(t=require("mitt"))&&"object"==typeof t&&"default"in t?t.default:t,u=function(){function t(){}return t.set=function(e,n){var r=t.stores[e];r&&r.setState||(t.stores[e]=n)},t.get=function(e){return t.stores[e]},t}();u.stores={},function(t){t.STORE_INITED="STORE_INITED",t.STORE_UPDATED="STORE_UPDATED"}(e||(e={}));var s=o(),i=function(){function t(t){var n=this;this.setters=[],this.setState=function(t,r,o){var u=n.getNextState(r,o);return n.state=u,s.emit(e.STORE_UPDATED,{key:t,nextState:u}),n.state=u,n.setters.forEach((function(t){return t(u)})),u},this.state=t}return t.prototype.getNextState=function(t,e){var n;if("function"!=typeof e)return e;if("object"!=typeof t)return e(t);var o=!0,u=r.produce(t,(function(t){var r=e(t);r!==t&&r&&"object"==typeof r&&(n=r,o=!1)}));return o&&(n=u),n},t}();exports.Storage=u,exports.getState=function(t){var e=u.get(t);return e?e.state:void 0},exports.mutate=function(t,e){var n=u.get(t);n&&n.setState?n.setState(t,n.state,e):u.set(t,{state:e})},exports.onStoreInit=function(t){s.on(e.STORE_INITED,(function(e){t(e)}))},exports.onStoreUpdate=function(t){s.on(e.STORE_UPDATED,(function(e){t(e)}))},exports.useStore=function(t,r){var o=u.get(t),a=o?o.state:r;u.set(t,new i(a));var f=u.get(t),c=n.useState(a),S=c[0],p=c[1],E=f.setters;return n.useEffect((function(){return E.push(p),function(t){s.emit(e.STORE_INITED,t)}(t),function(){E.splice(E.indexOf(p),1)}}),[E,p]),[S,function(t){return function(e){return f.setState(t,S,e)}}(t)]}; | ||
//# sourceMappingURL=stook.cjs.production.min.js.map |
@@ -27,20 +27,2 @@ import { useState, useEffect } from 'react'; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
var EventKey; | ||
@@ -81,5 +63,6 @@ | ||
this.setState = function (key, value) { | ||
var nextState = _this.getNextState(value); | ||
this.setState = function (key, state, value) { | ||
var nextState = _this.getNextState(state, value); | ||
_this.state = nextState; | ||
emitStoreUpdate({ | ||
@@ -103,3 +86,3 @@ key: key, | ||
_proto.getNextState = function getNextState(value) { | ||
_proto.getNextState = function getNextState(state, value) { | ||
var nextState; // not function | ||
@@ -109,5 +92,5 @@ | ||
if (typeof this.state !== 'object') return value(this.state); | ||
if (typeof state !== 'object') return value(state); | ||
var useImmer = true; | ||
var immerState = produce(this.state, function (draft) { | ||
var immerState = produce(state, function (draft) { | ||
var fnValue = value(draft); | ||
@@ -127,3 +110,3 @@ if (fnValue === draft) return; // do nothing | ||
return _extends({}, nextState); | ||
return nextState; | ||
}; | ||
@@ -167,3 +150,3 @@ | ||
return function (value) { | ||
return newStore.setState(key, value); | ||
return newStore.setState(key, state, value); | ||
}; | ||
@@ -197,3 +180,3 @@ } | ||
if (store && store.setState) { | ||
store.setState(key, nextValue); | ||
store.setState(key, store.state, nextValue); | ||
} else { | ||
@@ -200,0 +183,0 @@ // init state, if no store exist |
@@ -10,3 +10,3 @@ import { Dispatch, SetStateAction } from './types'; | ||
private getNextState; | ||
setState: (key: any, value: any) => any; | ||
setState: (key: any, state: any, value: any) => any; | ||
} |
{ | ||
"name": "stook", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "A minimalist design state management library for React", | ||
@@ -29,3 +29,3 @@ "license": "MIT", | ||
}, | ||
"gitHead": "5b2467f789c86f1d910066f6afa00061358260e7" | ||
"gitHead": "7f577415205e31f77e52dec021df12d29f6ecb73" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
40987
395