Socket
Socket
Sign inDemoInstall

amos

Package Overview
Dependencies
7
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.5 to 0.2.0

src/index.spec.ts

66

dist/amos.cjs.js

@@ -104,2 +104,40 @@ 'use strict';

*/
var identity = function (v) { return v; };
function shallowEqual(a, b) {
if (a === b) {
return true;
}
var ka = Object.keys(a);
if (ka.length !== Object.keys(b).length) {
return false;
}
for (var i = 0; i < ka.length; i++) {
if (!b.hasOwnProperty(ka[i]) || a[ka[i]] !== b[ka[i]]) {
return false;
}
}
return true;
}
function hoistMethod(src, dst) {
var _a;
var copy = function (name) {
if (dst.hasOwnProperty(name)) {
return;
}
Object.defineProperty(dst, name, Object.getOwnPropertyDescriptor(src, name));
};
Object.getOwnPropertyNames(src).forEach(copy);
(_a = Object.getOwnPropertySymbols) === null || _a === void 0 ? void 0 : _a.call(Object, src).forEach(copy);
return dst;
}
var kAmosObject = typeof Symbol === 'function' ? Symbol('AMOS_OBJECT') : 'Symbol(AMOS_OBJECT)';
function defineAmosObject(key, obj) {
if (!obj.hasOwnProperty(kAmosObject)) {
Object.defineProperty(obj, kAmosObject, { value: key });
}
return obj;
}
function isAmosObject(key, o) {
return !!o && o[kAmosObject] === key;
}
function strictEqual(a, b) {

@@ -120,12 +158,2 @@ return a === b;

var isArray = Array.isArray;
var identity = function (v) { return v; };
function defineObject(object, target) {
if (!target.hasOwnProperty('object')) {
Object.defineProperty(target, 'object', { value: object });
}
return target;
}
function is(object, o) {
return !!o && o.object === object;
}

@@ -184,3 +212,3 @@ /*

var a0 = args[0];
if (is('store.select', a0)) {
if (isAmosObject('store.select', a0)) {
return fn.apply(void 0, tslib.__spread(args));

@@ -273,3 +301,3 @@ }

},
dispatch: defineObject('store.dispatch', function (tasks) {
dispatch: defineAmosObject('store.dispatch', function (tasks) {
if (++dispatchDepth === 1) {

@@ -294,3 +322,3 @@ dispatchingSnapshot = {};

}),
select: defineObject('store.select', function (selectable, snapshot) {
select: defineAmosObject('store.select', function (selectable, snapshot) {
if (typeof selectable === 'function') {

@@ -323,4 +351,2 @@ if (snapshot) {

store = enhancers.reduce(function (previousValue, currentValue) { return currentValue(previousValue); }, store);
store.dispatch = defineObject('store.dispatch', store.dispatch);
store.select = defineObject('store.select', store.select);
if (typeof process === 'object' && process.env.NODE_ENV === 'development') {

@@ -593,9 +619,19 @@ Object.freeze(store);

/*
* @since 2020-11-03 13:22:41
* @author acrazing <joking.young@gmail.com>
*/
var VERSION = '0.2.0';
exports.Box = Box;
exports.Consumer = Consumer;
exports.Provider = Provider;
exports.VERSION = VERSION;
exports.action = action;
exports.createStore = createStore;
exports.hoistMethod = hoistMethod;
exports.identity = identity;
exports.isAmosObject = isAmosObject;
exports.selector = selector;
exports.shallowEqual = shallowEqual;
exports.signal = signal;

@@ -602,0 +638,0 @@ exports.useDispatch = useDispatch;

@@ -96,2 +96,40 @@ import { __spread, __values, __read } from 'tslib';

*/
var identity = function (v) { return v; };
function shallowEqual(a, b) {
if (a === b) {
return true;
}
var ka = Object.keys(a);
if (ka.length !== Object.keys(b).length) {
return false;
}
for (var i = 0; i < ka.length; i++) {
if (!b.hasOwnProperty(ka[i]) || a[ka[i]] !== b[ka[i]]) {
return false;
}
}
return true;
}
function hoistMethod(src, dst) {
var _a;
var copy = function (name) {
if (dst.hasOwnProperty(name)) {
return;
}
Object.defineProperty(dst, name, Object.getOwnPropertyDescriptor(src, name));
};
Object.getOwnPropertyNames(src).forEach(copy);
(_a = Object.getOwnPropertySymbols) === null || _a === void 0 ? void 0 : _a.call(Object, src).forEach(copy);
return dst;
}
var kAmosObject = typeof Symbol === 'function' ? Symbol('AMOS_OBJECT') : 'Symbol(AMOS_OBJECT)';
function defineAmosObject(key, obj) {
if (!obj.hasOwnProperty(kAmosObject)) {
Object.defineProperty(obj, kAmosObject, { value: key });
}
return obj;
}
function isAmosObject(key, o) {
return !!o && o[kAmosObject] === key;
}
function strictEqual(a, b) {

@@ -112,12 +150,2 @@ return a === b;

var isArray = Array.isArray;
var identity = function (v) { return v; };
function defineObject(object, target) {
if (!target.hasOwnProperty('object')) {
Object.defineProperty(target, 'object', { value: object });
}
return target;
}
function is(object, o) {
return !!o && o.object === object;
}

@@ -176,3 +204,3 @@ /*

var a0 = args[0];
if (is('store.select', a0)) {
if (isAmosObject('store.select', a0)) {
return fn.apply(void 0, __spread(args));

@@ -265,3 +293,3 @@ }

},
dispatch: defineObject('store.dispatch', function (tasks) {
dispatch: defineAmosObject('store.dispatch', function (tasks) {
if (++dispatchDepth === 1) {

@@ -286,3 +314,3 @@ dispatchingSnapshot = {};

}),
select: defineObject('store.select', function (selectable, snapshot) {
select: defineAmosObject('store.select', function (selectable, snapshot) {
if (typeof selectable === 'function') {

@@ -315,4 +343,2 @@ if (snapshot) {

store = enhancers.reduce(function (previousValue, currentValue) { return currentValue(previousValue); }, store);
store.dispatch = defineObject('store.dispatch', store.dispatch);
store.select = defineObject('store.select', store.select);
if (typeof process === 'object' && process.env.NODE_ENV === 'development') {

@@ -585,3 +611,9 @@ Object.freeze(store);

export { Box, Consumer, Provider, action, createStore, identity, selector, signal, useDispatch, useSelector, useStore };
/*
* @since 2020-11-03 13:22:41
* @author acrazing <joking.young@gmail.com>
*/
var VERSION = '0.2.0';
export { Box, Consumer, Provider, VERSION, action, createStore, hoistMethod, identity, isAmosObject, selector, shallowEqual, signal, useDispatch, useSelector, useStore };
//# sourceMappingURL=amos.es.js.map

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

!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).Amos={},e.React)}(this,(function(e,r){"use strict";function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=t(r),o=function(){function e(e,r,t){this.key=e,this.initialState=r,this.preload=t,this.listeners={}}return e.prototype.subscribe=function(e,r){this.listeners["string"==typeof e?e:e.type]=r},e.prototype.mutation=function(e,r){var t=this;return function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return{object:"mutation",type:r,box:t,args:n,result:n[0],mutator:e}}},e}();function u(e){var r="function"==typeof Symbol&&Symbol.iterator,t=r&&e[r],n=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(r?"Object is not iterable.":"Symbol.iterator is not defined.")}function s(e,r){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var n,o,u=t.call(e),s=[];try{for(;(void 0===r||r-- >0)&&!(n=u.next()).done;)s.push(n.value)}catch(e){o={error:e}}finally{try{n&&!n.done&&(t=u.return)&&t.call(u)}finally{if(o)throw o.error}}return s}function i(){for(var e=[],r=0;r<arguments.length;r++)e=e.concat(s(arguments[r]));return e}function c(e,r){return e===r}function a(e,r){if(e.length!==r.length)return!1;for(var t=0;t<e.length;t++)if(e[t]!==r[t])return!1;return!0}var f=Array.isArray,l=function(e){return e};function p(e,r){return r.hasOwnProperty("object")||Object.defineProperty(r,"object",{value:e}),r}function d(e,r){return!!r&&r.object===e}var y=r.createContext(null);function v(){var e=r.useContext(y);if(!e)throw new Error("[Amos] you are using hooks without <Provider />.");return e.store}var h={selectors:[],deps:[],snapshots:[],results:[]};function g(e,r){for(var t in e)if(e.hasOwnProperty(t)&&r.hasOwnProperty(t))return!0;return!1}function b(e,r,t,n){var o,u;if(!(null===(u=e.factory)||void 0===u?void 0:u.deps)||!t[n])return!0;var s=(o=e.factory).deps.apply(o,i([r.select],e.args)),c=a(t[n]||[],s);return t[n]=s,!c}function m(e,r,t){return e.factory?e.factory.compare(r,t):c(r,t)}function w(e,r,t,n,o){var u,s;if(!(e&&"function"==typeof e&&t&&e.args&&r.args))return!0;if(!(e===r||r.factory&&r.factory===e.factory))return!0;if(void 0===(null===(s=r.factory)||void 0===s?void 0:s.deps))return!a(e.args,r.args);var c=(u=r.factory).deps.apply(u,i([n.select],r.args));return!a(o||[],c)&&c}e.Box=o,e.Consumer=function(e){var r=e.children;return n.default.createElement(y.Consumer,null,(function(e){if(!e)throw new Error("[Amos] <Consumer /> should use inside <Provider />.");return r(e.store)}))},e.Provider=function(e){var t=e.store,o=e.children,u=s(r.useState({store:t}),2),i=u[0],c=u[1];return r.useEffect((function(){i.store!=i.store&&c({store:t})}),[t]),n.default.createElement(y.Provider,{value:i},o)},e.action=function(e,r){return Object.assign((function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return{object:"action",type:r,args:t,actor:e}}),{type:r})},e.createStore=function(e){for(var r=[],t=1;t<arguments.length;t++)r[t-1]=arguments[t];var n,o={},s=[],c=[],a=function(r){if(!o.hasOwnProperty(r.key)){var t=r.initialState;(null==e?void 0:e.hasOwnProperty(r.key))&&(t=r.preload(e[r.key],t)),o[r.key]=t,s.push(r)}},l=0,d={},y=function(e,r){(r!==o[e]||d.hasOwnProperty(e))&&(d[e]=r,o[e]=r)},v=function(e){var r,t;switch(e.object){case"action":return e.actor.apply(e,i([h.dispatch,h.select],e.args));case"mutation":return a(e.box),y(e.box.key,e.mutator.apply(e,i([o[e.box.key]],e.args))),e.result;case"signal":try{for(var n=u(s),c=n.next();!c.done;c=n.next()){var f=c.value,l=f.listeners[e.type];l&&y(f.key,l(o[f.key],e.data))}}catch(e){r={error:e}}finally{try{c&&!c.done&&(t=n.return)&&t.call(n)}finally{if(r)throw r.error}}return e.data}},h={snapshot:function(){return o},subscribe:function(e){return c.push(e),function(){var r=c.indexOf(e);r>-1&&c.splice(r,1)}},dispatch:p("store.dispatch",(function(e){1==++l&&(d={});try{return f(e)?e.map(v):v(e)}finally{0==--l&&Object.keys(d).length>0&&c.forEach((function(e){return e(d)}))}})),select:p("store.select",(function(e,r){if("function"!=typeof e)return a(e),n&&(n[e.key]=o[e.key]),o[e.key];if(!r)return e(h.select);if(n)throw new Error("[Amos] recursive snapshot collection is not supported.");n=r;try{return e(h.select)}finally{n=void 0}}))};return(h=r.reduce((function(e,r){return r(e)}),h)).dispatch=p("store.dispatch",h.dispatch),h.select=p("store.select",h.select),"object"==typeof process&&"development"===process.env.NODE_ENV&&Object.freeze(h),h},e.identity=l,e.selector=function(e,r,t,n){void 0===t&&(t=c);var o=Object.assign((function(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var n=r[0];if(d("store.select",n))return e.apply(void 0,i(r));var u=function(t){return e.apply(void 0,i([t],r))};return u.factory=o,u.args=r,u}),{deps:r,compare:t,type:n});return o},e.signal=function(e,r){return void 0===r&&(r=l),Object.assign((function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return{object:"signal",type:e,data:r.apply(void 0,i(t))}}),{type:e})},e.useDispatch=function(){return v().dispatch},e.useSelector=function(){for(var e,t,n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var u,i=s(r.useReducer((function(e){return e+1}),0),2),c=i[1],a=v(),f=r.useRef(h),l=r.useRef();if((null===(e=l.current)||void 0===e?void 0:e.store)!==a&&(f.current=h,null===(t=l.current)||void 0===t||t.disposer(),l.current={store:a,updated:!1,error:void 0,disposer:a.subscribe((function(e){var r,t=0,n=f.current,o=n.selectors,u=n.snapshots,s=n.results,i=n.deps,p=o.length;try{for(;t<p;t++){var d=o[t],y=u[t];if("function"==typeof d){if((!y||g(y,e))&&b(d,a,i,t)){var v={},h=a.select(d,v);(r=l.current).updated||(r.updated=!m(d,s[t],h)),u[t]=v,s[t]=h}}else if(e.hasOwnProperty(d.key)){var w=a.select(d);l.current.updated=w!==s[t],s[t]=w}}l.current.updated&&c()}catch(e){u.length=s.length=t-1,l.current.error="object"==typeof e&&e?Object.assign(e,{message:"[Amos] selector throws error: "+e.message}):new Error("[Amos] selector throws falsy error: "+e),c()}}))}),r.useEffect((function(){return function(){var e;return null===(e=l.current)||void 0===e?void 0:e.disposer()}}),[]),l.current.error){var p=l.current.error;throw l.current.error=void 0,p}if(l.current.updated)l.current.updated=!1,u=f.current.results;else{f.current===h&&(f.current={selectors:[],deps:[],snapshots:[],results:[]});for(var d=f.current,y=d.selectors,j=d.deps,O=d.snapshots,k=d.results,x=0;x<n.length;x++){var P=y[x],E=n[x];if("object"==typeof E)k[x]=a.select(E),y[x]=E;else{var S=w(P,E,O[x],a,j[x]);if(S){O[x]=void 0;var A={};k[x]=a.select(E,A),j[x]=!0===S?void 0:S,O[x]=A,y[x]=E}}}k.length=n.length,u=k}return r.useDebugValue(u,(function(e){return e.reduce((function(e,r,t){var o,u,s,i=n[t],c="function"==typeof i?null!==(s=null!==(o=i.type)&&void 0!==o?o:null===(u=i.factory)||void 0===u?void 0:u.type)&&void 0!==s?s:i.name:i.key;return e.hasOwnProperty(c)&&(c=c+"_"+t),e[c]=r,e}),{})})),u},e.useStore=v,Object.defineProperty(e,"__esModule",{value:!0})}));
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e((r="undefined"!=typeof globalThis?globalThis:r||self).Amos={},r.React)}(this,(function(r,e){"use strict";function t(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var n=t(e),o=function(){function r(r,e,t){this.key=r,this.initialState=e,this.preload=t,this.listeners={}}return r.prototype.subscribe=function(r,e){this.listeners["string"==typeof r?r:r.type]=e},r.prototype.mutation=function(r,e){var t=this;return function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return{object:"mutation",type:e,box:t,args:n,result:n[0],mutator:r}}},r}();function u(r){var e="function"==typeof Symbol&&Symbol.iterator,t=e&&r[e],n=0;if(t)return t.call(r);if(r&&"number"==typeof r.length)return{next:function(){return r&&n>=r.length&&(r=void 0),{value:r&&r[n++],done:!r}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function i(r,e){var t="function"==typeof Symbol&&r[Symbol.iterator];if(!t)return r;var n,o,u=t.call(r),i=[];try{for(;(void 0===e||e-- >0)&&!(n=u.next()).done;)i.push(n.value)}catch(r){o={error:r}}finally{try{n&&!n.done&&(t=u.return)&&t.call(u)}finally{if(o)throw o.error}}return i}function s(){for(var r=[],e=0;e<arguments.length;e++)r=r.concat(i(arguments[e]));return r}var a=function(r){return r};var c="function"==typeof Symbol?Symbol("AMOS_OBJECT"):"Symbol(AMOS_OBJECT)";function f(r,e){return e.hasOwnProperty(c)||Object.defineProperty(e,c,{value:r}),e}function l(r,e){return!!e&&e[c]===r}function p(r,e){return r===e}function y(r,e){if(r.length!==e.length)return!1;for(var t=0;t<r.length;t++)if(r[t]!==e[t])return!1;return!0}var d=Array.isArray;var v=e.createContext(null);function h(){var r=e.useContext(v);if(!r)throw new Error("[Amos] you are using hooks without <Provider />.");return r.store}var b={selectors:[],deps:[],snapshots:[],results:[]};function g(r,e){for(var t in r)if(r.hasOwnProperty(t)&&e.hasOwnProperty(t))return!0;return!1}function m(r,e,t,n){var o,u;if(!(null===(u=r.factory)||void 0===u?void 0:u.deps)||!t[n])return!0;var i=(o=r.factory).deps.apply(o,s([e.select],r.args)),a=y(t[n]||[],i);return t[n]=i,!a}function O(r,e,t){return r.factory?r.factory.compare(e,t):p(e,t)}function w(r,e,t,n,o){var u,i;if(!(r&&"function"==typeof r&&t&&r.args&&e.args))return!0;if(!(r===e||e.factory&&e.factory===r.factory))return!0;if(void 0===(null===(i=e.factory)||void 0===i?void 0:i.deps))return!y(r.args,e.args);var a=(u=e.factory).deps.apply(u,s([n.select],e.args));return!y(o||[],a)&&a}r.Box=o,r.Consumer=function(r){var e=r.children;return n.default.createElement(v.Consumer,null,(function(r){if(!r)throw new Error("[Amos] <Consumer /> should use inside <Provider />.");return e(r.store)}))},r.Provider=function(r){var t=r.store,o=r.children,u=i(e.useState({store:t}),2),s=u[0],a=u[1];return e.useEffect((function(){s.store!=s.store&&a({store:t})}),[t]),n.default.createElement(v.Provider,{value:s},o)},r.VERSION="0.2.0",r.action=function(r,e){return Object.assign((function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return{object:"action",type:e,args:t,actor:r}}),{type:e})},r.createStore=function(r){for(var e=[],t=1;t<arguments.length;t++)e[t-1]=arguments[t];var n,o={},i=[],a=[],c=function(e){if(!o.hasOwnProperty(e.key)){var t=e.initialState;(null==r?void 0:r.hasOwnProperty(e.key))&&(t=e.preload(r[e.key],t)),o[e.key]=t,i.push(e)}},l=0,p={},y=function(r,e){(e!==o[r]||p.hasOwnProperty(r))&&(p[r]=e,o[r]=e)},v=function(r){var e,t;switch(r.object){case"action":return r.actor.apply(r,s([h.dispatch,h.select],r.args));case"mutation":return c(r.box),y(r.box.key,r.mutator.apply(r,s([o[r.box.key]],r.args))),r.result;case"signal":try{for(var n=u(i),a=n.next();!a.done;a=n.next()){var f=a.value,l=f.listeners[r.type];l&&y(f.key,l(o[f.key],r.data))}}catch(r){e={error:r}}finally{try{a&&!a.done&&(t=n.return)&&t.call(n)}finally{if(e)throw e.error}}return r.data}},h={snapshot:function(){return o},subscribe:function(r){return a.push(r),function(){var e=a.indexOf(r);e>-1&&a.splice(e,1)}},dispatch:f("store.dispatch",(function(r){1==++l&&(p={});try{return d(r)?r.map(v):v(r)}finally{0==--l&&Object.keys(p).length>0&&a.forEach((function(r){return r(p)}))}})),select:f("store.select",(function(r,e){if("function"!=typeof r)return c(r),n&&(n[r.key]=o[r.key]),o[r.key];if(!e)return r(h.select);if(n)throw new Error("[Amos] recursive snapshot collection is not supported.");n=e;try{return r(h.select)}finally{n=void 0}}))};return h=e.reduce((function(r,e){return e(r)}),h),"object"==typeof process&&"development"===process.env.NODE_ENV&&Object.freeze(h),h},r.hoistMethod=function(r,e){var t,n=function(t){e.hasOwnProperty(t)||Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))};return Object.getOwnPropertyNames(r).forEach(n),null===(t=Object.getOwnPropertySymbols)||void 0===t||t.call(Object,r).forEach(n),e},r.identity=a,r.isAmosObject=l,r.selector=function(r,e,t,n){void 0===t&&(t=p);var o=Object.assign((function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=e[0];if(l("store.select",n))return r.apply(void 0,s(e));var u=function(t){return r.apply(void 0,s([t],e))};return u.factory=o,u.args=e,u}),{deps:e,compare:t,type:n});return o},r.shallowEqual=function(r,e){if(r===e)return!0;var t=Object.keys(r);if(t.length!==Object.keys(e).length)return!1;for(var n=0;n<t.length;n++)if(!e.hasOwnProperty(t[n])||r[t[n]]!==e[t[n]])return!1;return!0},r.signal=function(r,e){return void 0===e&&(e=a),Object.assign((function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return{object:"signal",type:r,data:e.apply(void 0,s(t))}}),{type:r})},r.useDispatch=function(){return h().dispatch},r.useSelector=function(){for(var r,t,n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var u,s=i(e.useReducer((function(r){return r+1}),0),2),a=s[1],c=h(),f=e.useRef(b),l=e.useRef();if((null===(r=l.current)||void 0===r?void 0:r.store)!==c&&(f.current=b,null===(t=l.current)||void 0===t||t.disposer(),l.current={store:c,updated:!1,error:void 0,disposer:c.subscribe((function(r){var e,t=0,n=f.current,o=n.selectors,u=n.snapshots,i=n.results,s=n.deps,p=o.length;try{for(;t<p;t++){var y=o[t],d=u[t];if("function"==typeof y){if((!d||g(d,r))&&m(y,c,s,t)){var v={},h=c.select(y,v);(e=l.current).updated||(e.updated=!O(y,i[t],h)),u[t]=v,i[t]=h}}else if(r.hasOwnProperty(y.key)){var b=c.select(y);l.current.updated=b!==i[t],i[t]=b}}l.current.updated&&a()}catch(r){u.length=i.length=t-1,l.current.error="object"==typeof r&&r?Object.assign(r,{message:"[Amos] selector throws error: "+r.message}):new Error("[Amos] selector throws falsy error: "+r),a()}}))}),e.useEffect((function(){return function(){var r;return null===(r=l.current)||void 0===r?void 0:r.disposer()}}),[]),l.current.error){var p=l.current.error;throw l.current.error=void 0,p}if(l.current.updated)l.current.updated=!1,u=f.current.results;else{f.current===b&&(f.current={selectors:[],deps:[],snapshots:[],results:[]});for(var y=f.current,d=y.selectors,v=y.deps,j=y.snapshots,P=y.results,k=0;k<n.length;k++){var E=d[k],S=n[k];if("object"==typeof S)P[k]=c.select(S),d[k]=S;else{var x=w(E,S,j[k],c,v[k]);if(x){j[k]=void 0;var A={};P[k]=c.select(S,A),v[k]=!0===x?void 0:x,j[k]=A,d[k]=S}}}P.length=n.length,u=P}return e.useDebugValue(u,(function(r){return r.reduce((function(r,e,t){var o,u,i,s=n[t],a="function"==typeof s?null!==(i=null!==(o=s.type)&&void 0!==o?o:null===(u=s.factory)||void 0===u?void 0:u.type)&&void 0!==i?i:s.name:s.key;return r.hasOwnProperty(a)&&(a=a+"_"+t),r[a]=e,r}),{})})),u},r.useStore=h,Object.defineProperty(r,"__esModule",{value:!0})}));
//# sourceMappingURL=amos.umd.js.map

@@ -8,2 +8,3 @@ export { JSONState, Mutation, Box } from './box';

export { useStore, useDispatch, MapSelector, useSelector } from './hooks';
export { identity } from './utils';
export { identity, shallowEqual, isAmosObject, hoistMethod } from './utils';
export declare const VERSION = "__VERSION__";

@@ -5,2 +5,3 @@ import { Action } from './action';

import { Signal } from './signal';
import { AmosObject } from './utils';
/**

@@ -19,8 +20,7 @@ * the state snapshot in store

/**
* dispatch
* base amos signature, this is used for someone want to change the signature of useDispatch()
*
* @stable
*/
export interface Dispatch {
object: 'store.dispatch';
export interface AmosDispatch extends AmosObject<'store.dispatch'> {
<R>(task: Dispatchable<R>): R;

@@ -82,2 +82,4 @@ <R1>(tasks: readonly [Dispatchable<R1>]): [R1];

}
export interface Dispatch extends AmosDispatch {
}
/**

@@ -94,5 +96,4 @@ * selectable things

*/
export interface Select {
export interface Select extends AmosObject<'store.select'> {
<R>(selectable: Selectable<R>, snapshot?: Snapshot): R;
object: 'store.select';
}

@@ -99,0 +100,0 @@ /**

@@ -0,11 +1,13 @@

export declare const identity: <T>(v: T) => T;
export declare function shallowEqual<T extends object>(a: T, b: T): boolean;
export declare function hoistMethod<M extends (...args: any[]) => any>(src: M, dst: (...args: Parameters<M>) => ReturnType<M>): M;
export declare const kAmosObject: unique symbol;
export interface AmosObject<K extends string> {
[kAmosObject]: K;
}
export declare function defineAmosObject<K extends string, T extends object>(key: K, obj: T): T & AmosObject<K>;
export declare function isAmosObject<T extends AmosObject<any>>(key: T[typeof kAmosObject], o: any): o is T;
export declare function strictEqual<T>(a: T, b: T): boolean;
export declare function arrayEqual<T>(a: T[], b: T[]): boolean;
export declare function values<T extends object>(o: T): T[keyof T][];
export declare const isArray: (args: any) => args is any[] | readonly any[];
export declare const identity: <T>(v: T) => T;
export declare function values<T extends object>(o: T): T[keyof T][];
export declare function defineObject<O extends string, T extends object>(object: O, target: T): T & {
object: O;
};
export declare function is<T extends {
object: string;
}>(object: T['object'], o: unknown): o is T;
{
"name": "amos",
"version": "0.1.5",
"version": "0.2.0",
"description": "A decentralized state manager for react",

@@ -36,6 +36,8 @@ "keywords": [

"@rollup/plugin-commonjs": "^16.0.0",
"@testing-library/react": "^11.1.1",
"@rollup/plugin-replace": "^2.3.4",
"@testing-library/react": "^11.1.2",
"@testing-library/react-hooks": "^3.4.2",
"@types/express": "^4.17.8",
"@types/express": "^4.17.9",
"@types/jest": "^26.0.15",
"@types/react": "^16.9.55",
"@types/react-dom": "^16.9.9",

@@ -48,5 +50,6 @@ "express": "^4.17.1",

"prettier": "^2.1.2",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-test-renderer": "^17.0.1",
"rollup": "^2.33.1",
"rollup": "^2.33.2",
"rollup-plugin-node-resolve": "^5.2.0",

@@ -56,6 +59,4 @@ "rollup-plugin-sourcemaps": "^0.6.3",

"rollup-plugin-typescript2": "^0.29.0",
"ts-jest": "^26.4.3",
"typescript": "^4.0.5",
"@types/react": "^16.9.55",
"react": "^17.0.1"
"ts-jest": "^26.4.4",
"typescript": "^4.0.5"
},

@@ -69,2 +70,5 @@ "dependencies": {

},
"umdExternals": {
"react": "React"
},
"husky": {

@@ -71,0 +75,0 @@ "hooks": {

@@ -22,2 +22,4 @@ /*

export { useStore, useDispatch, MapSelector, useSelector } from './hooks';
export { identity } from './utils';
export { identity, shallowEqual, isAmosObject, hoistMethod } from './utils';
export const VERSION = '__VERSION__';

@@ -7,3 +7,3 @@ /*

import { Select } from './store';
import { is, strictEqual } from './utils';
import { isAmosObject, strictEqual } from './utils';

@@ -71,3 +71,3 @@ /**

const a0 = args[0];
if (is<Select>('store.select', a0)) {
if (isAmosObject<Select>('store.select', a0)) {
return fn(...(args as [Select, ...A]));

@@ -74,0 +74,0 @@ }

@@ -10,3 +10,3 @@ /*

import { Signal } from './signal';
import { defineObject, isArray } from './utils';
import { AmosObject, defineAmosObject, isArray } from './utils';

@@ -28,8 +28,7 @@ /**

/**
* dispatch
* base amos signature, this is used for someone want to change the signature of useDispatch()
*
* @stable
*/
export interface Dispatch {
object: 'store.dispatch';
export interface AmosDispatch extends AmosObject<'store.dispatch'> {
<R>(task: Dispatchable<R>): R;

@@ -104,2 +103,4 @@ <R1>(tasks: readonly [Dispatchable<R1>]): [R1];

export interface Dispatch extends AmosDispatch {}
/**

@@ -117,5 +118,4 @@ * selectable things

*/
export interface Select {
export interface Select extends AmosObject<'store.select'> {
<R>(selectable: Selectable<R>, snapshot?: Snapshot): R;
object: 'store.select';
}

@@ -218,21 +218,24 @@

},
dispatch: defineObject('store.dispatch', (tasks: Dispatchable | readonly Dispatchable[]) => {
if (++dispatchDepth === 1) {
dispatchingSnapshot = {};
}
try {
if (isArray(tasks)) {
return tasks.map(exec);
} else {
return exec(tasks);
dispatch: defineAmosObject(
'store.dispatch',
(tasks: Dispatchable | readonly Dispatchable[]) => {
if (++dispatchDepth === 1) {
dispatchingSnapshot = {};
}
} finally {
if (--dispatchDepth === 0) {
if (Object.keys(dispatchingSnapshot).length > 0) {
listeners.forEach((fn) => fn(dispatchingSnapshot));
try {
if (isArray(tasks)) {
return tasks.map(exec);
} else {
return exec(tasks);
}
} finally {
if (--dispatchDepth === 0) {
if (Object.keys(dispatchingSnapshot).length > 0) {
listeners.forEach((fn) => fn(dispatchingSnapshot));
}
}
}
}
}),
select: defineObject('store.select', (selectable: Selectable, snapshot?: Snapshot): any => {
},
),
select: defineAmosObject('store.select', (selectable: Selectable, snapshot?: Snapshot): any => {
if (typeof selectable === 'function') {

@@ -262,4 +265,2 @@ if (snapshot) {

store = enhancers.reduce((previousValue, currentValue) => currentValue(previousValue), store);
store.dispatch = defineObject('store.dispatch', store.dispatch);
store.select = defineObject('store.select', store.select);
if (typeof process === 'object' && process.env.NODE_ENV === 'development') {

@@ -266,0 +267,0 @@ Object.freeze(store);

@@ -6,3 +6,14 @@ /*

import { arrayEqual } from './utils';
import {
arrayEqual,
defineAmosObject,
hoistMethod,
identity,
isAmosObject,
isArray,
kAmosObject,
shallowEqual,
strictEqual,
values,
} from './utils';

@@ -16,2 +27,52 @@ describe('utils', () => {

});
it('should define amos object', () => {
const fn = defineAmosObject('amos.object', () => 1);
expect(fn[kAmosObject] === 'amos.object').toBe(true);
// @ts-expect-error
expect(fn[kAmosObject] === 'bad value').toBe(false);
expect(isAmosObject('amos.object', fn)).toBe(true);
expect(isAmosObject('amos.object', 1)).toBe(false);
expect(isAmosObject('amos.object1', fn)).toBe(false);
});
it('should hoist methods', () => {
const fn = defineAmosObject('amos.object', (id: number) => id * 2);
Object.assign(fn, { bar: 1 });
const fn2 = hoistMethod(fn, (id: number) => id * 4);
expect(fn2(2)).toEqual(8);
expect(fn2[kAmosObject]).toBe('amos.object');
});
it('should shallow compare', () => {
expect(shallowEqual({ a: 1 }, { a: 1 })).toBe(true);
expect(shallowEqual({ a: 1 }, { a: 2 })).toBe(false);
expect(shallowEqual({ a: 1 }, { a: 1, b: 3 })).toBe(false);
});
it('should identity', () => {
expect(identity(expect)).toBe(expect);
});
it('should strict compare', () => {
expect(strictEqual(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)).toBe(true);
expect(strictEqual(NaN, NaN)).toBe(false);
expect(strictEqual({}, {})).toBe(false);
expect(strictEqual('abc', 'abc')).toBe(true);
});
it('should compare array', () => {
expect(arrayEqual([1, 2, 3], [1, 2, 3])).toBe(true);
expect(arrayEqual([1, 2, 3, 4], [1, 2, 3])).toBe(false);
expect(arrayEqual([1, 2, {}], [1, 2, {}])).toBe(false);
});
it('should get values', () => {
expect(values(expect)).toEqual(Object.values(expect));
});
it('should test is array', () => {
expect(isArray([] as readonly [])).toBe(true);
});
});

@@ -6,2 +6,59 @@ /*

export const identity = <T>(v: T) => v;
export function shallowEqual<T extends object>(a: T, b: T): boolean {
if (a === b) {
return true;
}
const ka = Object.keys(a) as Array<keyof T>;
if (ka.length !== Object.keys(b).length) {
return false;
}
for (let i = 0; i < ka.length; i++) {
if (!b.hasOwnProperty(ka[i]) || a[ka[i]] !== b[ka[i]]) {
return false;
}
}
return true;
}
export function hoistMethod<M extends (...args: any[]) => any>(
src: M,
dst: (...args: Parameters<M>) => ReturnType<M>,
): M {
const copy = (name: PropertyKey) => {
if (dst.hasOwnProperty(name)) {
return;
}
Object.defineProperty(dst, name, Object.getOwnPropertyDescriptor(src, name)!);
};
Object.getOwnPropertyNames(src).forEach(copy);
Object.getOwnPropertySymbols?.(src).forEach(copy);
return dst as M;
}
export const kAmosObject: unique symbol =
typeof Symbol === 'function' ? Symbol('AMOS_OBJECT') : ('Symbol(AMOS_OBJECT)' as any);
export interface AmosObject<K extends string> {
[kAmosObject]: K;
}
export function defineAmosObject<K extends string, T extends object>(
key: K,
obj: T,
): T & AmosObject<K> {
if (!obj.hasOwnProperty(kAmosObject)) {
Object.defineProperty(obj, kAmosObject, { value: key });
}
return obj as any;
}
export function isAmosObject<T extends AmosObject<any>>(
key: T[typeof kAmosObject],
o: any,
): o is T {
return !!o && o[kAmosObject] === key;
}
export function strictEqual<T>(a: T, b: T) {

@@ -23,6 +80,2 @@ return a === b;

export const isArray: (args: any) => args is any[] | readonly any[] = Array.isArray;
export const identity = <T>(v: T) => v;
export function values<T extends object>(o: T): T[keyof T][] {

@@ -32,14 +85,2 @@ return Object.keys(o).map((k) => o[k as keyof T]);

export function defineObject<O extends string, T extends object>(
object: O,
target: T,
): T & { object: O } {
if (!target.hasOwnProperty('object')) {
Object.defineProperty(target, 'object', { value: object });
}
return target as any;
}
export function is<T extends { object: string }>(object: T['object'], o: unknown): o is T {
return !!o && (o as any).object === object;
}
export const isArray: (args: any) => args is any[] | readonly any[] = Array.isArray;

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

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