clean-state
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -1,190 +0,1 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('eventemitter3')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'react', 'eventemitter3'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['clean-state'] = {}, global.react, global.EventEmitter)); | ||
}(this, (function (exports, react, EventEmitter) { 'use strict'; | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter); | ||
function splitPropertyAndMethod(modules) { | ||
var rootState = {}; | ||
var rootReducers = {}; | ||
var rootEffects = {}; | ||
Object.keys(modules).forEach(function (key) { | ||
var module = modules[key]; | ||
rootState[key] = {}; | ||
rootReducers[key] = {}; | ||
rootEffects[key] = {}; | ||
Object.assign(rootState[key], module.state); | ||
Object.assign(rootReducers[key], module.reducers); | ||
Object.assign(rootEffects[key], module.effects); | ||
}); | ||
return { rootState: rootState, rootReducers: rootReducers, rootEffects: rootEffects }; | ||
} | ||
var Container = (function () { | ||
function Container(modules) { | ||
var initialData = splitPropertyAndMethod(modules); | ||
this.rootState = initialData.rootState; | ||
this.rootReducers = initialData.rootReducers; | ||
this.rootEffects = initialData.rootEffects; | ||
this.emitter = new EventEmitter__default['default'](); | ||
} | ||
Container.prototype.getRootEffects = function () { | ||
return this.rootEffects; | ||
}; | ||
Container.prototype.getRootReducers = function () { | ||
return this.rootReducers; | ||
}; | ||
Container.prototype.getRootState = function () { | ||
return this.rootState; | ||
}; | ||
Container.prototype.getModule = function (namespace) { | ||
var _this = this; | ||
var combineModule = {}; | ||
var assign = function (k) { | ||
var _a; | ||
var module = { | ||
state: _this.rootState[k], | ||
reducers: _this.rootReducers[k], | ||
effects: _this.rootEffects[k] | ||
}; | ||
Object.assign(combineModule, (_a = {}, _a[k] = module, _a)); | ||
}; | ||
if (Array.isArray(namespace)) { | ||
namespace.forEach(function (k) { return assign(k); }); | ||
} | ||
else { | ||
assign(namespace); | ||
} | ||
return combineModule; | ||
}; | ||
Container.prototype.getState = function (namespace) { | ||
var combineModule = this.getModule(namespace); | ||
return Object.keys(combineModule).reduce(function (result, key) { | ||
result[key] = combineModule[key].state; | ||
return result; | ||
}, {}); | ||
}; | ||
Container.prototype.setState = function (namespace, newState) { | ||
if (this.rootState[namespace]) { | ||
this.rootState[namespace] = newState; | ||
} | ||
this.trigger(namespace); | ||
}; | ||
Container.prototype.addListener = function (namespace, listener) { | ||
var _this = this; | ||
if (Array.isArray(namespace)) { | ||
namespace.forEach(function (k) { return _this.emitter.on(k, listener); }); | ||
} | ||
else { | ||
this.emitter.on(namespace, listener); | ||
} | ||
}; | ||
Container.prototype.removeListener = function (namespace, listener) { | ||
var _this = this; | ||
if (Array.isArray(namespace)) { | ||
namespace.forEach(function (k) { return _this.emitter.removeListener(k, listener); }); | ||
} | ||
else { | ||
this.emitter.removeListener(namespace, listener); | ||
} | ||
}; | ||
Container.prototype.trigger = function (namespace) { | ||
this.emitter.emit(namespace); | ||
}; | ||
return Container; | ||
}()); | ||
var DISPATCH_TYPE = 'CS_DISPATCH_TYPE'; | ||
var plugins = []; | ||
var bootstrap = function (modules) { | ||
var container = new Container(modules); | ||
var pluginEmitter = new EventEmitter__default['default'](); | ||
var dispatch = function (nameAndMethod, payload) { | ||
var _a = nameAndMethod.split('/'), namespace = _a[0], methodName = _a[1]; | ||
var combineModule = container.getModule(namespace); | ||
var _b = combineModule[namespace], state = _b.state, reducers = _b.reducers, effects = _b.effects; | ||
var rootState = container.getRootState(); | ||
if (effects[methodName]) { | ||
pluginEmitter.emit(DISPATCH_TYPE, { | ||
type: nameAndMethod, | ||
payload: payload | ||
}); | ||
return effects[methodName]({ state: state, payload: payload, rootState: rootState, dispatch: dispatch }); | ||
} | ||
else if (reducers[methodName]) { | ||
var newState = reducers[methodName]({ | ||
state: state, | ||
rootState: rootState, | ||
payload: payload | ||
}); | ||
container.setState(namespace, newState); | ||
pluginEmitter.emit(DISPATCH_TYPE, { | ||
type: nameAndMethod, | ||
payload: payload, | ||
newState: newState | ||
}); | ||
} | ||
}; | ||
var injectFns = function (reducersOrEffects) { | ||
Object.keys(reducersOrEffects).forEach(function (key) { | ||
if (!dispatch[key]) | ||
dispatch[key] = {}; | ||
var originFns = reducersOrEffects[key]; | ||
var fns = {}; | ||
Object.keys(originFns).forEach(function (fnKey) { | ||
fns[fnKey] = function (payload) { | ||
return dispatch(key + "/" + fnKey, payload); | ||
}; | ||
}); | ||
Object.assign(dispatch[key], fns); | ||
}); | ||
}; | ||
function useModule(namespace) { | ||
var _a = react.useState({}), setState = _a[1]; | ||
var setStateProxy = react.useCallback(function () { return setState({}); }, [setState]); | ||
react.useEffect(function () { | ||
container.addListener(namespace, setStateProxy); | ||
return function () { return container.removeListener(namespace, setStateProxy); }; | ||
}, [namespace, setStateProxy]); | ||
return container.getState(namespace); | ||
} | ||
var rootReducers = container.getRootReducers(); | ||
var rootEffects = container.getRootEffects(); | ||
injectFns(rootReducers); | ||
injectFns(rootEffects); | ||
plugins.forEach(function (plugin) { return plugin(modules, pluginEmitter); }); | ||
return { useModule: useModule, dispatch: dispatch }; | ||
}; | ||
bootstrap.addPlugin = function (plugin) { | ||
plugins.push(plugin); | ||
}; | ||
bootstrap.DISPATCH_TYPE = DISPATCH_TYPE; | ||
var mixin = function (common, modules) { | ||
var keys = Object.keys(modules); | ||
keys.forEach(function (key) { | ||
var module = modules[key]; | ||
module.state = module.state || {}; | ||
module.reducers = module.reducers || {}; | ||
module.effects = module.effects || {}; | ||
if (common.state) | ||
Object.assign(module.state, common.state); | ||
if (common.reducers) | ||
Object.assign(module.reducers, common.reducers); | ||
if (common.effects) | ||
Object.assign(module.effects, common.effects); | ||
}); | ||
return modules; | ||
}; | ||
exports.default = bootstrap; | ||
exports.mixin = mixin; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
//# sourceMappingURL=index.min.js.map | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("eventemitter3")):"function"==typeof define&&define.amd?define(["exports","react","eventemitter3"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["clean-state"]={},t.react,t.EventEmitter)}(this,(function(t,e,r){"use strict";function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var n=o(r);var s=function(){function t(t){var e=function(t){var e={},r={},o={};return Object.keys(t).forEach((function(n){var s=t[n];e[n]={},r[n]={},o[n]={},Object.assign(e[n],s.state),Object.assign(r[n],s.reducers),Object.assign(o[n],s.effects)})),{rootState:e,rootReducers:r,rootEffects:o}}(t);this.rootState=e.rootState,this.rootReducers=e.rootReducers,this.rootEffects=e.rootEffects,this.emitter=new n.default}return t.prototype.getRootEffects=function(){return this.rootEffects},t.prototype.getRootReducers=function(){return this.rootReducers},t.prototype.getRootState=function(){return this.rootState},t.prototype.getModule=function(t){var e=this,r={},o=function(t){var o,n={state:e.rootState[t],reducers:e.rootReducers[t],effects:e.rootEffects[t]};Object.assign(r,((o={})[t]=n,o))};return Array.isArray(t)?t.forEach((function(t){return o(t)})):o(t),r},t.prototype.getState=function(t){var e=this.getModule(t);return Object.keys(e).reduce((function(t,r){return t[r]=e[r].state,t}),{})},t.prototype.setState=function(t,e){this.rootState[t]&&(this.rootState[t]=e),this.trigger(t)},t.prototype.addListener=function(t,e){var r=this;Array.isArray(t)?t.forEach((function(t){return r.emitter.on(t,e)})):this.emitter.on(t,e)},t.prototype.removeListener=function(t,e){var r=this;Array.isArray(t)?t.forEach((function(t){return r.emitter.removeListener(t,e)})):this.emitter.removeListener(t,e)},t.prototype.trigger=function(t){this.emitter.emit(t)},t}(),i="CS_DISPATCH_TYPE",c=[],a=function(t){var r=new s(t),o=new n.default,a=function(t,e){var n=t.split("/"),s=n[0],c=n[1],u=r.getModule(s)[s],f=u.state,d=u.reducers,p=u.effects,y=r.getRootState();if(p[c])return o.emit(i,{type:t,payload:e}),p[c]({state:f,payload:e,rootState:y,dispatch:a});if(d[c]){var h=d[c]({state:f,rootState:y,payload:e});r.setState(s,h),o.emit(i,{type:t,payload:e,newState:h})}},u=function(t){Object.keys(t).forEach((function(e){a[e]||(a[e]={});var r=t[e],o={};Object.keys(r).forEach((function(t){o[t]=function(r){return a(e+"/"+t,r)}})),Object.assign(a[e],o)}))};var f=r.getRootReducers(),d=r.getRootEffects();return u(f),u(d),c.forEach((function(e){return e(t,o)})),{useModule:function(t){var o=e.useState({})[1],n=e.useCallback((function(){return o({})}),[o]);return e.useEffect((function(){return r.addListener(t,n),function(){return r.removeListener(t,n)}}),[t,n]),r.getState(t)},dispatch:a}};a.addPlugin=function(t){c.push(t)},a.DISPATCH_TYPE=i;t.default=a,t.mixin=function(t,e){return Object.keys(e).forEach((function(r){var o=e[r];o.state=o.state||{},o.reducers=o.reducers||{},o.effects=o.effects||{},t.state&&Object.assign(o.state,t.state),t.reducers&&Object.assign(o.reducers,t.reducers),t.effects&&Object.assign(o.effects,t.effects)})),e},Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "clean-state", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "", | ||
@@ -43,2 +43,3 @@ "main": "lib/index.min.js", | ||
"rollup-plugin-sourcemaps": "^0.6.3", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"rollup-plugin-typescript": "^1.0.1", | ||
@@ -45,0 +46,0 @@ "typescript": "^4.1.2" |
import typescript from 'rollup-plugin-typescript'; | ||
import sourceMaps from 'rollup-plugin-sourcemaps'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
@@ -12,3 +12,3 @@ module.exports = { | ||
}), | ||
sourceMaps(), | ||
terser(), | ||
], | ||
@@ -20,5 +20,4 @@ output: [ | ||
file: 'lib/index.min.js', | ||
sourcemap: true, | ||
}, | ||
], | ||
}; |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
15390
14
10
169
2