Comparing version 0.2.0 to 0.2.1
@@ -28,30 +28,60 @@ const | ||
loop: | ||
let last_fn = 0; | ||
let last_fn_path; | ||
while (cursor) { | ||
switch (true) { | ||
case types.isFunctionDeclaration(cursor.parent): | ||
is_func_decl = 1; | ||
cursor_path = cursor.parentPath; | ||
cursor = cursor.parent; | ||
break loop; | ||
case types.isFunctionExpression(cursor.parent): | ||
is_func_expr = 1; | ||
cursor_path = cursor.parentPath; | ||
cursor = cursor.parent; | ||
break loop; | ||
case types.isArrowFunctionExpression(cursor.parent): | ||
is_arrow_expr = 1; | ||
cursor_path = cursor.parentPath; | ||
cursor = cursor.parent; | ||
if (types.isFunctionDeclaration(cursor.parent) || | ||
types.isFunctionExpression(cursor.parent) || | ||
types.isArrowFunctionExpression(cursor.parent) | ||
) { | ||
let has_return_statement = types.isArrowFunctionExpression(cursor.parent) && | ||
!types.isBlockStatement(cursor.parent.body); | ||
if (types.isBlockStatement(cursor.body)) { | ||
is_arrow_expr_block = 1; | ||
} | ||
break loop; | ||
if (!has_return_statement) { | ||
traverse(cursor.parent, { | ||
ReturnStatement(path) { | ||
let cur = path; | ||
loop: | ||
while (cur) { | ||
switch (true) { | ||
case types.isFunctionDeclaration(cur.node): | ||
case types.isFunctionExpression(cur.node): | ||
case types.isArrowFunctionExpression(cur.node): | ||
if (cur.node !== cursor.parent) return; | ||
break loop; | ||
} | ||
cur = cur.parentPath; | ||
} | ||
has_return_statement = true; | ||
} | ||
}, cursor.parentPath.scope, cursor.parentPath); | ||
} | ||
if (has_return_statement) { | ||
last_fn = cursor.parent; | ||
last_fn_path = cursor.parentPath; | ||
} | ||
} | ||
if (types.isProgram(cursor.parent)) { | ||
cursor = null; | ||
break; | ||
} | ||
else { | ||
cursor = cursor.parentPath; | ||
cursor = cursor.parentPath; | ||
} | ||
if (!last_fn) return; | ||
cursor = last_fn; | ||
cursor_path = last_fn_path; | ||
if (types.isFunctionDeclaration(cursor)) { | ||
is_func_decl = 1; | ||
} | ||
else if (types.isFunctionExpression(cursor)) { | ||
is_func_expr = 1; | ||
} | ||
else if (types.isArrowFunctionExpression(cursor)) { | ||
is_arrow_expr = 1; | ||
if (types.isBlockStatement(cursor.body)) { | ||
is_arrow_expr_block = 1; | ||
} | ||
@@ -58,0 +88,0 @@ } |
@@ -1,1 +0,1 @@ | ||
import{useReducer as t,useRef as e,useEffect as n}from"react";function o(t,e,n,o){if(4!==arguments.length)throw new Error('Unsupported "changed" function call outside of unit "expression"');let r=!1;if(n.has(o)){const c=n.get(o);r=!(e||Object.is)(t,c),r&&n.set(o,t)}else n.set(o,t);return r}function r(t){if(t){const e=t.pending;if(!0===e||!1===e)return e}throw new Error('Function "pending" support only async unit method as agrument')}let c=1,f=0,i=0,u=new Set,s=[],l=null,p=0,d=new Map,a=new Map,h=new Set,g=new Map,w=[],y=null,M=new Map;function b(){if(i||u.clear(),i++,i>1e4)throw new Error("Tick deep limit exception")}function m(t){let e;do{e=t,t=new Set;for(const n of e){const e=a.get(n);if(e)for(const n of e)h.add(n),t.add(n)}}while(t.size)}function S(){if(i>1||!u.size)return void i--;let t=1e4;for(;--t;){m(u),u.clear();for(const t of h){const e=g.get(t);e&&e()}if(!u.size)break}if(i--,!t)throw new Error("Limit of expressions iteration")}function E(){const t=++c;return y&&y.push(t),t}function O(t){const e=d.get(t);if(e)for(const n of e){const e=a.get(n);e&&e.delete(t)}const n=a.get(t);if(n)for(const e of n){const n=d.get(e);n&&n.delete(t)}d.delete(t),a.delete(t),h.delete(t),g.delete(t)}function v(){w.push(y),y=[]}function k(){const t=++f;return M.set(t,y),y=w.pop(),t}function j(t){const e=M.get(t);for(const t of e)O(t);M.delete(t)}function x(){s.push([l,p])}function z(){[l,p]=s.pop()}function P(t){x(),l=new Set,p=t}function U(){h.delete(p),d.set(p,l);for(const t of l){let e=a.get(t);e||a.set(t,e=new Set),e.add(p)}z()}function F(t){const e=E();return g.set(e,t),e}function T(t){P(t),b()}function A(){S(),U()}function I(){x(),l=null,p=1,b()}function L(){S(),z()}let $=0,q=new Map,B=new Map,C=new Map,D=new Map,G=new Map,H=new Set,J=0,K=[],N=new Map,Q=new Map,R=new Map,V=0;function W(t){const e=++$;return q.set(t,e),B.set(e,t),H.add(e),V||V||(V=setTimeout(()=>{V=0;const t=[];for(const e of H)C.has(e)||t.push(B.get(e));if(t.length){for(const e of t)console.error("Unit without holder detected",e);throw new Error("Memory leak detected")}H.clear()})),e}function X(t){K.push(J),J=t}function Y(){J=K.pop()}function Z(t,e){const n=q.get(t),o=(C.get(n)||0)+1;if(C.set(n,o),1===arguments.length&&(e=J),e){let t=D.get(e);t||D.set(e,t=new Set),t.add(n);let o=G.get(n);o||G.set(n,o=new Set),o.add(e)}return n}function _(t){C.set(t,C.get(t)-1),function(){let t=[];for(const[e,n]of C)n||(t.push(e),C.delete(e));if(!t.length)return;let e,n=[];do{n.push.apply(n,t),e=[];for(const n of t){const t=D.get(n);if(t)for(const n of t){const t=C.get(n)-1;C.set(n,t),t||e.push(n)}}t=e}while(t.length);n=n.reverse();for(const t of n)tt(t)}()}function tt(t){q.delete(B.get(t)),B.delete(t),C.delete(t);const e=D.get(t);if(e)for(const n of e){let e=G.get(n);e.delete(t),e.size||G.delete(n)}D.delete(t);let n=N.get(t);if(n){I();for(const t of n)t();L(),N.delete(t)}if(n=Q.get(t),n){for(const t of n)j(t);Q.delete(t)}if(n=R.get(t),n){for(const t of n)O(t);R.delete(t)}}function et(t){let e=N.get(J);e||N.set(J,e=[]),e.push(t)}function nt(t){let e=Q.get(J);e||Q.set(J,e=[]),e.push(t)}function ot(t){if(!(t&&t instanceof Function))throw new Error('Only function supported as argument for "effect" function');const e=t();e instanceof Function&&et(e)}let rt=new Map,ct=0,ft=0,it=0,ut=[],st=[];function lt(t){ut.push([ft,it]),it=0,ft=t}function pt(){[ft,it]=ut.pop()}function dt(t){return(t+1)%16777215}function at(t,e,n){let o=rt.get(ft);o||rt.set(ft,o=new Map);const r=++it;if(e){if(o.has(r))return o.get(r);{const n=e(t);return Z(n),o.set(r,n),n}}if(n=n||[],o.has(r)){let[e,c]=o.get(r),f=n.length!==c.length;if(!f){if(0===n.length)return e;let t=n.length;for(;t--;)if(!Object.is(c[t],n[t])){f=1;break}}return f?(e&&_(e),e=t.apply(null,n),Z(e),o.set(r,[e,n]),e):e}{const e=t.apply(null,n);return Z(e),o.set(r,[e,n]),e}}function ht(t){if(!ft&&!Pt())throw new Error("Unsupported useOwn outside render function");const e=Array.prototype.slice.call(arguments,1);return at(t,0,e.length?e:0)}function gt(t){if(!ft&&!Pt())throw new Error("Unsupported useShared outside render function");return at(t,$t)}const wt=Symbol(),yt=Symbol();let Mt=0;function bt(t,e){let n=E(),o=0;return t&&t[yt]&&(o=Z(t)),e||(e=J),{get:()=>{return e=n,l&&l.add(e),t;var e},set:r=>{Object.is(t,r)||(o&&(o=_(o)),t=r,r&&r[yt]&&(o=Z(r,e)),function(t){const e=!i;e&&b(),u.add(t),e&&S()}(n))}}}function mt(t){return function(){throw new Error(`Manual call of unit "${t}" unsupported`)}}Et.link=function(t){let e=Z(t,0);return()=>_(e)},Et.v=[function(){let o;if(0!==ct)return void ct++;try{o=t(dt,0)[1]}catch(t){if(-1===String(t).indexOf("Invalid hook call"))throw t;return void lt(0)}ct++;const r=e();if(!r.current){const t=W(o),e=Z(o),n=F(o);X(t),function(t){let e=R.get(J);e||R.set(J,e=[]),e.push(t)}(n),Y();const c=()=>()=>{_(e),rt.delete(t)};r.current=[t,n,c]}const[c,f,i]=r.current;n(i,st),lt(c),X(c),T(f)},function(){ft?(ct--,0===ct&&(A(),Y(),pt())):pt()}],Et.b=bt,Et.c=[F,T,A,function(){const t=E();return h.add(t),t},function(t){return l&&l.add(t),!h.has(t)||(P(t),!1)},function(){U()},I,L];let St={[yt]:!0,constructor:mt("constructor"),destructor:mt("destructor"),expression:mt("expression")};function Et(t){let e=arguments,n=e[0],o=e[1],r=e[2],c=e[3];if(5!==e.length)throw new Error("Invalid `unit` call. Please check your babel config.");function f(){const t=Ut(f);if(t)return t.apply(this,arguments);let e=Object.create(St),i=W(e);X(i),v(),++Mt;let u=n.call(e);--Mt;let s=k();nt(s);let l=4;for(let t of o)Object.defineProperty(e,t,bt(u[l++],i));for(let t of r)Object.defineProperty(e,t,{get:u[l++]});for(let t of c)Object.defineProperty(e,t,{value:u[l++]});return u[0]&&u[0].apply(e,arguments),u[1]&&et(u[1]),u[2]&&u[3](),Y(),e}return f[wt]=c,f}let Ot=new Map,vt=0,kt=[],jt=0,xt=0,zt=0;function Pt(){return xt}function Ut(t){return Ot.get(t)}function Ft(t,e){if(!e&&jt){let e=null;if(t&&t[wt]&&(e=t[wt]),!e)throw"Test mocks ony unit supported";const n={};for(const t of e)n[t]=jt();const o=function(){return n};return Ot.set(t,o),n}Ot.set(t,e)}function Tt(){const t=Array.prototype.slice.call(arguments);if(t.length)for(const e of t)Ft.delete(e);else Ot.clear()}Ft.s=function(t){kt.push([xt,jt,zt]);const e=++vt;xt=e,jt=t;const n=W(e);zt=Z(n),X(n)},Ft.f=function(){_(zt),function(){for(const t of H)tt(t)}(),Y(),Tt(),[xt,jt,zt]=kt.pop()};const At=new Map,It=[];function Lt(t){if(Mt||Pt())return $t(t);throw new Error("`shared` function supported only in unit schema and unit tests")}function $t(t){let e=At.get(1);e||At.set(1,e=new Map);let n=e.get(t);return n||e.set(t,n=t()),n}function qt({unit:t}){const o=e();if(!o.current){const e=Z($t(t)),n=()=>()=>_(e);o.current=n}return n(o.current,It),null}export{qt as Shared,o as changed,ot as effect,Ft as mock,r as pending,Lt as shared,Et as unit,Tt as unmock,ht as useOwn,gt as useShared}; | ||
import{useReducer as t,useRef as e,useEffect as n}from"react";function o(t,e,n,o){if(4!==arguments.length)throw new Error('Unsupported "changed" function call outside of unit "expression"');let r=!1;if(n.has(o)){const c=n.get(o);r=!(e||Object.is)(t,c),r&&n.set(o,t)}else n.set(o,t);return r}function r(t){if(t){const e=t.pending;if(!0===e||!1===e)return e}throw new Error('Function "pending" support only async unit method as agrument')}let c=1,f=0,i=0,u=new Set,s=[],l=null,p=0,a=new Map,d=new Map,h=new Set,g=new Map,w=[],y=null,M=new Map;function b(){if(i||u.clear(),i++,i>1e4)throw new Error("Tick deep limit exception")}function m(t){let e;do{e=t,t=new Set;for(const n of e){const e=d.get(n);if(e)for(const n of e)h.add(n),t.add(n)}}while(t.size)}function S(){if(i>1||!u.size)return void i--;let t=1e4;for(;--t;){m(u),u.clear();for(const t of h){const e=g.get(t);e&&e()}if(!u.size)break}if(i--,!t)throw new Error("Limit of expressions iteration")}function E(){const t=++c;return y&&y.push(t),t}function O(t){const e=a.get(t);if(e)for(const n of e){const e=d.get(n);e&&e.delete(t)}const n=d.get(t);if(n)for(const e of n){const n=a.get(e);n&&n.delete(t)}a.delete(t),d.delete(t),h.delete(t),g.delete(t)}function v(){w.push(y),y=[]}function k(){const t=++f;return M.set(t,y),y=w.pop(),t}function j(t){const e=M.get(t);for(const t of e)O(t);M.delete(t)}function x(){s.push([l,p])}function z(){[l,p]=s.pop()}function P(t){x(),l=new Set,p=t}function U(){h.delete(p),a.set(p,l);for(const t of l){let e=d.get(t);e||d.set(t,e=new Set),e.add(p)}z()}function F(t){const e=E();return g.set(e,t),e}function T(t){P(t),b()}function A(){S(),U()}function I(){x(),l=null,p=1,b()}function L(){S(),z()}let $=0,q=new Map,B=new Map,C=new Map,D=new Map,G=new Map,H=new Set,J=0,K=[],N=new Map,Q=new Map,R=new Map,V=0;function W(t){const e=++$;return q.set(t,e),B.set(e,t),H.add(e),V||V||(V=setTimeout(()=>{V=0;const t=[];for(const e of H)C.has(e)||t.push(B.get(e));if(t.length){for(const e of t)console.error("Unit without holder detected",e);throw new Error("Memory leak detected")}H.clear()})),e}function X(t){K.push(J),J=t}function Y(){J=K.pop()}function Z(t,e){const n=q.get(t),o=(C.get(n)||0)+1;if(C.set(n,o),1===arguments.length&&(e=J),e){let t=D.get(e);t||D.set(e,t=new Set),t.add(n);let o=G.get(n);o||G.set(n,o=new Set),o.add(e)}return n}function _(t){C.set(t,C.get(t)-1),function(){let t=[];for(const[e,n]of C)n||(t.push(e),C.delete(e));if(!t.length)return;let e,n=[];do{n.push.apply(n,t),e=[];for(const n of t){const t=D.get(n);if(t)for(const n of t){const t=C.get(n)-1;C.set(n,t),t||e.push(n)}}t=e}while(t.length);n=n.reverse();for(const t of n)tt(t)}()}function tt(t){q.delete(B.get(t)),B.delete(t),C.delete(t);const e=D.get(t);if(e)for(const n of e){let e=G.get(n);e.delete(t),e.size||G.delete(n)}D.delete(t);let n=N.get(t);if(n){I();for(const t of n)t();L(),N.delete(t)}if(n=Q.get(t),n){for(const t of n)j(t);Q.delete(t)}if(n=R.get(t),n){for(const t of n)O(t);R.delete(t)}}function et(t){let e=N.get(J);e||N.set(J,e=[]),e.push(t)}function nt(t){let e=Q.get(J);e||Q.set(J,e=[]),e.push(t)}function ot(t){if(!(t&&t instanceof Function))throw new Error('Only function supported as argument for "effect" function');const e=t();e instanceof Function&&et(e)}let rt=new Map,ct=0,ft=0,it=0,ut=[],st=[];function lt(t){ut.push([ft,it]),it=0,ft=t}function pt(){[ft,it]=ut.pop()}function at(t){return(t+1)%16777215}function dt(t,e,n){let o=rt.get(ft);o||rt.set(ft,o=new Map);const r=++it;if(e){if(o.has(r))return o.get(r);{const n=e(t);return Z(n),o.set(r,n),n}}if(n=n||[],o.has(r)){let[e,c]=o.get(r),f=n.length!==c.length;if(!f){if(0===n.length)return e;let t=n.length;for(;t--;)if(!Object.is(c[t],n[t])){f=1;break}}return f?(e&&_(e),e=t.apply(null,n),Z(e),o.set(r,[e,n]),e):e}{const e=t.apply(null,n);return Z(e),o.set(r,[e,n]),e}}function ht(t){if(!ft&&!Pt())throw new Error("Unsupported useOwn outside render function");const e=Array.prototype.slice.call(arguments,1);return dt(t,0,e.length?e:0)}function gt(t){if(!ft&&!Pt())throw new Error("Unsupported useShared outside render function");return dt(t,$t)}const wt=Symbol(),yt=Symbol(),Mt=function(){const t={};t[yt]=!0;const e=["constructor","destructor","expression"];for(let n=0,o=e.length;n<o;n++)t[e[n]]=St(e[n]);return t}();let bt=0;function mt(t,e){let n=E(),o=0;return t&&t[yt]&&(o=Z(t)),e||(e=J),{get:()=>{return e=n,l&&l.add(e),t;var e},set:r=>{Object.is(t,r)||(o&&(o=_(o)),t=r,r&&r[yt]&&(o=Z(r,e)),function(t){const e=!i;e&&b(),u.add(t),e&&S()}(n))}}}function St(t){return function(){throw new Error(`Manual call of unit "${t}" unsupported`)}}function Et(t){let e=arguments,n=e[0],o=e[1],r=e[2],c=e[3];if(5!==e.length)throw new Error("Invalid `unit` call. Please check your babel config.");function f(){const t=Ut(f);if(t)return t.apply(this,arguments);let e=Object.create(Mt),i=W(e);X(i),v(),++bt;let u=n.call(e);--bt;let s=k();nt(s);let l=4;for(let t of o)Object.defineProperty(e,t,mt(u[l++],i));for(let t of r)Object.defineProperty(e,t,{get:u[l++]});for(let t of c)Object.defineProperty(e,t,{value:u[l++]});return u[0]&&u[0].apply(e,arguments),u[1]&&et(u[1]),u[2]&&u[3](),Y(),e}return f[wt]=c,f}Et.link=function(t){let e=Z(t,0);return()=>_(e)},Et.v=[function(){let o;if(0!==ct)return void ct++;try{o=t(at,0)[1]}catch(t){if(-1===String(t).indexOf("Invalid hook call"))throw t;return void lt(0)}ct++;const r=e();if(!r.current){const t=W(o),e=Z(o),n=F(o);X(t),function(t){let e=R.get(J);e||R.set(J,e=[]),e.push(t)}(n),Y();const c=()=>()=>{_(e),rt.delete(t)};r.current=[t,n,c]}const[c,f,i]=r.current;n(i,st),lt(c),X(c),T(f)},function(){ft?(ct--,0===ct&&(A(),Y(),pt())):pt()}],Et.b=mt,Et.c=[F,T,A,function(){const t=E();return h.add(t),t},function(t){return l&&l.add(t),!h.has(t)||(P(t),!1)},function(){U()},I,L];let Ot=new Map,vt=0,kt=[],jt=0,xt=0,zt=0;function Pt(){return xt}function Ut(t){return Ot.get(t)}function Ft(t,e){if(!e&&jt){let e=null;if(t&&t[wt]&&(e=t[wt]),!e)throw"Test mocks ony unit supported";const n={};for(const t of e)n[t]=jt();const o=function(){return n};return Ot.set(t,o),n}Ot.set(t,e)}function Tt(){const t=Array.prototype.slice.call(arguments);if(t.length)for(const e of t)Ft.delete(e);else Ot.clear()}Ft.s=function(t){kt.push([xt,jt,zt]);const e=++vt;xt=e,jt=t;const n=W(e);zt=Z(n),X(n)},Ft.f=function(){_(zt),function(){for(const t of H)tt(t)}(),Y(),Tt(),[xt,jt,zt]=kt.pop()};const At=new Map,It=[];function Lt(t){if(bt||Pt())return $t(t);throw new Error("`shared` function supported only in unit schema and unit tests")}function $t(t){let e=At.get(1);e||At.set(1,e=new Map);let n=e.get(t);return n||e.set(t,n=t()),n}function qt({unit:t}){const o=e();if(!o.current){const e=Z($t(t)),n=()=>()=>_(e);o.current=n}return n(o.current,It),null}export{qt as Shared,o as changed,ot as effect,Ft as mock,r as pending,Lt as shared,Et as unit,Tt as unmock,ht as useOwn,gt as useShared}; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react");let e=1,n=0,o=0,r=new Set,c=[],s=null,u=0,f=new Map,i=new Map,l=new Set,p=new Map,d=[],a=null,h=new Map;function g(){if(o||r.clear(),o++,o>1e4)throw new Error("Tick deep limit exception")}function w(t){let e;do{e=t,t=new Set;for(const n of e){const e=i.get(n);if(e)for(const n of e)l.add(n),t.add(n)}}while(t.size)}function y(){if(o>1||!r.size)return void o--;let t=1e4;for(;--t;){w(r),r.clear();for(const t of l){const e=p.get(t);e&&e()}if(!r.size)break}if(o--,!t)throw new Error("Limit of expressions iteration")}function M(){const t=++e;return a&&a.push(t),t}function x(t){const e=f.get(t);if(e)for(const n of e){const e=i.get(n);e&&e.delete(t)}const n=i.get(t);if(n)for(const e of n){const n=f.get(e);n&&n.delete(t)}f.delete(t),i.delete(t),l.delete(t),p.delete(t)}function b(){d.push(a),a=[]}function S(){const t=++n;return h.set(t,a),a=d.pop(),t}function m(t){const e=h.get(t);for(const t of e)x(t);h.delete(t)}function E(){c.push([s,u])}function O(){[s,u]=c.pop()}function v(t){E(),s=new Set,u=t}function k(){l.delete(u),f.set(u,s);for(const t of s){let e=i.get(t);e||i.set(t,e=new Set),e.add(u)}O()}function j(t){const e=M();return p.set(e,t),e}function P(t){v(t),g()}function z(){y(),k()}function U(){E(),s=null,u=1,g()}function F(){y(),O()}let R=0,T=new Map,A=new Map,I=new Map,_=new Map,q=new Map,L=new Set,$=0,B=[],C=new Map,D=new Map,G=new Map,H=0;function J(t){const e=++R;return T.set(t,e),A.set(e,t),L.add(e),H||H||(H=setTimeout(()=>{H=0;const t=[];for(const e of L)I.has(e)||t.push(A.get(e));if(t.length){for(const e of t)console.error("Unit without holder detected",e);throw new Error("Memory leak detected")}L.clear()})),e}function K(t){B.push($),$=t}function N(){$=B.pop()}function Q(t,e){const n=T.get(t),o=(I.get(n)||0)+1;if(I.set(n,o),1===arguments.length&&(e=$),e){let t=_.get(e);t||_.set(e,t=new Set),t.add(n);let o=q.get(n);o||q.set(n,o=new Set),o.add(e)}return n}function V(t){I.set(t,I.get(t)-1),function(){let t=[];for(const[e,n]of I)n||(t.push(e),I.delete(e));if(!t.length)return;let e,n=[];do{n.push.apply(n,t),e=[];for(const n of t){const t=_.get(n);if(t)for(const n of t){const t=I.get(n)-1;I.set(n,t),t||e.push(n)}}t=e}while(t.length);n=n.reverse();for(const t of n)W(t)}()}function W(t){T.delete(A.get(t)),A.delete(t),I.delete(t);const e=_.get(t);if(e)for(const n of e){let e=q.get(n);e.delete(t),e.size||q.delete(n)}_.delete(t);let n=C.get(t);if(n){U();for(const t of n)t();F(),C.delete(t)}if(n=D.get(t),n){for(const t of n)m(t);D.delete(t)}if(n=G.get(t),n){for(const t of n)x(t);G.delete(t)}}function X(t){let e=C.get($);e||C.set($,e=[]),e.push(t)}function Y(t){let e=D.get($);e||D.set($,e=[]),e.push(t)}let Z=new Map,tt=0,et=0,nt=0,ot=[],rt=[];function ct(t){ot.push([et,nt]),nt=0,et=t}function st(){[et,nt]=ot.pop()}function ut(t){return(t+1)%16777215}function ft(t,e,n){let o=Z.get(et);o||Z.set(et,o=new Map);const r=++nt;if(e){if(o.has(r))return o.get(r);{const n=e(t);return Q(n),o.set(r,n),n}}if(n=n||[],o.has(r)){let[e,c]=o.get(r),s=n.length!==c.length;if(!s){if(0===n.length)return e;let t=n.length;for(;t--;)if(!Object.is(c[t],n[t])){s=1;break}}return s?(e&&V(e),e=t.apply(null,n),Q(e),o.set(r,[e,n]),e):e}{const e=t.apply(null,n);return Q(e),o.set(r,[e,n]),e}}const it=Symbol(),lt=Symbol();let pt=0;function dt(t,e){let n=M(),c=0;return t&&t[lt]&&(c=Q(t)),e||(e=$),{get:()=>{return e=n,s&&s.add(e),t;var e},set:s=>{Object.is(t,s)||(c&&(c=V(c)),t=s,s&&s[lt]&&(c=Q(s,e)),function(t){const e=!o;e&&g(),r.add(t),e&&y()}(n))}}}function at(t){return function(){throw new Error(`Manual call of unit "${t}" unsupported`)}}gt.link=function(t){let e=Q(t,0);return()=>V(e)},gt.v=[function(){let e;if(0!==tt)return void tt++;try{e=t.useReducer(ut,0)[1]}catch(t){if(-1===String(t).indexOf("Invalid hook call"))throw t;return void ct(0)}tt++;const n=t.useRef();if(!n.current){const t=J(e),o=Q(e),r=j(e);K(t),function(t){let e=G.get($);e||G.set($,e=[]),e.push(t)}(r),N();const c=()=>()=>{V(o),Z.delete(t)};n.current=[t,r,c]}const[o,r,c]=n.current;t.useEffect(c,rt),ct(o),K(o),P(r)},function(){et?(tt--,0===tt&&(z(),N(),st())):st()}],gt.b=dt,gt.c=[j,P,z,function(){const t=M();return l.add(t),t},function(t){return s&&s.add(t),!l.has(t)||(v(t),!1)},function(){k()},U,F];let ht={[lt]:!0,constructor:at("constructor"),destructor:at("destructor"),expression:at("expression")};function gt(t){let e=arguments,n=e[0],o=e[1],r=e[2],c=e[3];if(5!==e.length)throw new Error("Invalid `unit` call. Please check your babel config.");function s(){const t=Et(s);if(t)return t.apply(this,arguments);let e=Object.create(ht),u=J(e);K(u),b(),++pt;let f=n.call(e);--pt;let i=S();Y(i);let l=4;for(let t of o)Object.defineProperty(e,t,dt(f[l++],u));for(let t of r)Object.defineProperty(e,t,{get:f[l++]});for(let t of c)Object.defineProperty(e,t,{value:f[l++]});return f[0]&&f[0].apply(e,arguments),f[1]&&X(f[1]),f[2]&&f[3](),N(),e}return s[it]=c,s}let wt=new Map,yt=0,Mt=[],xt=0,bt=0,St=0;function mt(){return bt}function Et(t){return wt.get(t)}function Ot(t,e){if(!e&&xt){let e=null;if(t&&t[it]&&(e=t[it]),!e)throw"Test mocks ony unit supported";const n={};for(const t of e)n[t]=xt();const o=function(){return n};return wt.set(t,o),n}wt.set(t,e)}function vt(){const t=Array.prototype.slice.call(arguments);if(t.length)for(const e of t)Ot.delete(e);else wt.clear()}Ot.s=function(t){Mt.push([bt,xt,St]);const e=++yt;bt=e,xt=t;const n=J(e);St=Q(n),K(n)},Ot.f=function(){V(St),function(){for(const t of L)W(t)}(),N(),vt(),[bt,xt,St]=Mt.pop()};const kt=new Map,jt=[];function Pt(t){let e=kt.get(1);e||kt.set(1,e=new Map);let n=e.get(t);return n||e.set(t,n=t()),n}exports.Shared=function({unit:e}){const n=t.useRef();if(!n.current){const t=Q(Pt(e)),o=()=>()=>V(t);n.current=o}return t.useEffect(n.current,jt),null},exports.changed=function(t,e,n,o){if(4!==arguments.length)throw new Error('Unsupported "changed" function call outside of unit "expression"');let r=!1;if(n.has(o)){const c=n.get(o);r=!(e||Object.is)(t,c),r&&n.set(o,t)}else n.set(o,t);return r},exports.effect=function(t){if(!(t&&t instanceof Function))throw new Error('Only function supported as argument for "effect" function');const e=t();e instanceof Function&&X(e)},exports.mock=Ot,exports.pending=function(t){if(t){const e=t.pending;if(!0===e||!1===e)return e}throw new Error('Function "pending" support only async unit method as agrument')},exports.shared=function(t){if(pt||mt())return Pt(t);throw new Error("`shared` function supported only in unit schema and unit tests")},exports.unit=gt,exports.unmock=vt,exports.useOwn=function(t){if(!et&&!mt())throw new Error("Unsupported useOwn outside render function");const e=Array.prototype.slice.call(arguments,1);return ft(t,0,e.length?e:0)},exports.useShared=function(t){if(!et&&!mt())throw new Error("Unsupported useShared outside render function");return ft(t,Pt)}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react");let e=1,n=0,o=0,r=new Set,c=[],f=null,s=0,u=new Map,i=new Map,l=new Set,p=new Map,d=[],a=null,h=new Map;function g(){if(o||r.clear(),o++,o>1e4)throw new Error("Tick deep limit exception")}function w(t){let e;do{e=t,t=new Set;for(const n of e){const e=i.get(n);if(e)for(const n of e)l.add(n),t.add(n)}}while(t.size)}function y(){if(o>1||!r.size)return void o--;let t=1e4;for(;--t;){w(r),r.clear();for(const t of l){const e=p.get(t);e&&e()}if(!r.size)break}if(o--,!t)throw new Error("Limit of expressions iteration")}function M(){const t=++e;return a&&a.push(t),t}function x(t){const e=u.get(t);if(e)for(const n of e){const e=i.get(n);e&&e.delete(t)}const n=i.get(t);if(n)for(const e of n){const n=u.get(e);n&&n.delete(t)}u.delete(t),i.delete(t),l.delete(t),p.delete(t)}function b(){d.push(a),a=[]}function S(){const t=++n;return h.set(t,a),a=d.pop(),t}function m(t){const e=h.get(t);for(const t of e)x(t);h.delete(t)}function E(){c.push([f,s])}function O(){[f,s]=c.pop()}function v(t){E(),f=new Set,s=t}function k(){l.delete(s),u.set(s,f);for(const t of f){let e=i.get(t);e||i.set(t,e=new Set),e.add(s)}O()}function j(t){const e=M();return p.set(e,t),e}function P(t){v(t),g()}function z(){y(),k()}function U(){E(),f=null,s=1,g()}function F(){y(),O()}let R=0,T=new Map,A=new Map,I=new Map,_=new Map,q=new Map,L=new Set,$=0,B=[],C=new Map,D=new Map,G=new Map,H=0;function J(t){const e=++R;return T.set(t,e),A.set(e,t),L.add(e),H||H||(H=setTimeout(()=>{H=0;const t=[];for(const e of L)I.has(e)||t.push(A.get(e));if(t.length){for(const e of t)console.error("Unit without holder detected",e);throw new Error("Memory leak detected")}L.clear()})),e}function K(t){B.push($),$=t}function N(){$=B.pop()}function Q(t,e){const n=T.get(t),o=(I.get(n)||0)+1;if(I.set(n,o),1===arguments.length&&(e=$),e){let t=_.get(e);t||_.set(e,t=new Set),t.add(n);let o=q.get(n);o||q.set(n,o=new Set),o.add(e)}return n}function V(t){I.set(t,I.get(t)-1),function(){let t=[];for(const[e,n]of I)n||(t.push(e),I.delete(e));if(!t.length)return;let e,n=[];do{n.push.apply(n,t),e=[];for(const n of t){const t=_.get(n);if(t)for(const n of t){const t=I.get(n)-1;I.set(n,t),t||e.push(n)}}t=e}while(t.length);n=n.reverse();for(const t of n)W(t)}()}function W(t){T.delete(A.get(t)),A.delete(t),I.delete(t);const e=_.get(t);if(e)for(const n of e){let e=q.get(n);e.delete(t),e.size||q.delete(n)}_.delete(t);let n=C.get(t);if(n){U();for(const t of n)t();F(),C.delete(t)}if(n=D.get(t),n){for(const t of n)m(t);D.delete(t)}if(n=G.get(t),n){for(const t of n)x(t);G.delete(t)}}function X(t){let e=C.get($);e||C.set($,e=[]),e.push(t)}function Y(t){let e=D.get($);e||D.set($,e=[]),e.push(t)}let Z=new Map,tt=0,et=0,nt=0,ot=[],rt=[];function ct(t){ot.push([et,nt]),nt=0,et=t}function ft(){[et,nt]=ot.pop()}function st(t){return(t+1)%16777215}function ut(t,e,n){let o=Z.get(et);o||Z.set(et,o=new Map);const r=++nt;if(e){if(o.has(r))return o.get(r);{const n=e(t);return Q(n),o.set(r,n),n}}if(n=n||[],o.has(r)){let[e,c]=o.get(r),f=n.length!==c.length;if(!f){if(0===n.length)return e;let t=n.length;for(;t--;)if(!Object.is(c[t],n[t])){f=1;break}}return f?(e&&V(e),e=t.apply(null,n),Q(e),o.set(r,[e,n]),e):e}{const e=t.apply(null,n);return Q(e),o.set(r,[e,n]),e}}const it=Symbol(),lt=Symbol(),pt=function(){const t={};t[lt]=!0;const e=["constructor","destructor","expression"];for(let n=0,o=e.length;n<o;n++)t[e[n]]=ht(e[n]);return t}();let dt=0;function at(t,e){let n=M(),c=0;return t&&t[lt]&&(c=Q(t)),e||(e=$),{get:()=>{return e=n,f&&f.add(e),t;var e},set:f=>{Object.is(t,f)||(c&&(c=V(c)),t=f,f&&f[lt]&&(c=Q(f,e)),function(t){const e=!o;e&&g(),r.add(t),e&&y()}(n))}}}function ht(t){return function(){throw new Error(`Manual call of unit "${t}" unsupported`)}}function gt(t){let e=arguments,n=e[0],o=e[1],r=e[2],c=e[3];if(5!==e.length)throw new Error("Invalid `unit` call. Please check your babel config.");function f(){const t=Et(f);if(t)return t.apply(this,arguments);let e=Object.create(pt),s=J(e);K(s),b(),++dt;let u=n.call(e);--dt;let i=S();Y(i);let l=4;for(let t of o)Object.defineProperty(e,t,at(u[l++],s));for(let t of r)Object.defineProperty(e,t,{get:u[l++]});for(let t of c)Object.defineProperty(e,t,{value:u[l++]});return u[0]&&u[0].apply(e,arguments),u[1]&&X(u[1]),u[2]&&u[3](),N(),e}return f[it]=c,f}gt.link=function(t){let e=Q(t,0);return()=>V(e)},gt.v=[function(){let e;if(0!==tt)return void tt++;try{e=t.useReducer(st,0)[1]}catch(t){if(-1===String(t).indexOf("Invalid hook call"))throw t;return void ct(0)}tt++;const n=t.useRef();if(!n.current){const t=J(e),o=Q(e),r=j(e);K(t),function(t){let e=G.get($);e||G.set($,e=[]),e.push(t)}(r),N();const c=()=>()=>{V(o),Z.delete(t)};n.current=[t,r,c]}const[o,r,c]=n.current;t.useEffect(c,rt),ct(o),K(o),P(r)},function(){et?(tt--,0===tt&&(z(),N(),ft())):ft()}],gt.b=at,gt.c=[j,P,z,function(){const t=M();return l.add(t),t},function(t){return f&&f.add(t),!l.has(t)||(v(t),!1)},function(){k()},U,F];let wt=new Map,yt=0,Mt=[],xt=0,bt=0,St=0;function mt(){return bt}function Et(t){return wt.get(t)}function Ot(t,e){if(!e&&xt){let e=null;if(t&&t[it]&&(e=t[it]),!e)throw"Test mocks ony unit supported";const n={};for(const t of e)n[t]=xt();const o=function(){return n};return wt.set(t,o),n}wt.set(t,e)}function vt(){const t=Array.prototype.slice.call(arguments);if(t.length)for(const e of t)Ot.delete(e);else wt.clear()}Ot.s=function(t){Mt.push([bt,xt,St]);const e=++yt;bt=e,xt=t;const n=J(e);St=Q(n),K(n)},Ot.f=function(){V(St),function(){for(const t of L)W(t)}(),N(),vt(),[bt,xt,St]=Mt.pop()};const kt=new Map,jt=[];function Pt(t){let e=kt.get(1);e||kt.set(1,e=new Map);let n=e.get(t);return n||e.set(t,n=t()),n}exports.Shared=function({unit:e}){const n=t.useRef();if(!n.current){const t=Q(Pt(e)),o=()=>()=>V(t);n.current=o}return t.useEffect(n.current,jt),null},exports.changed=function(t,e,n,o){if(4!==arguments.length)throw new Error('Unsupported "changed" function call outside of unit "expression"');let r=!1;if(n.has(o)){const c=n.get(o);r=!(e||Object.is)(t,c),r&&n.set(o,t)}else n.set(o,t);return r},exports.effect=function(t){if(!(t&&t instanceof Function))throw new Error('Only function supported as argument for "effect" function');const e=t();e instanceof Function&&X(e)},exports.mock=Ot,exports.pending=function(t){if(t){const e=t.pending;if(!0===e||!1===e)return e}throw new Error('Function "pending" support only async unit method as agrument')},exports.shared=function(t){if(dt||mt())return Pt(t);throw new Error("`shared` function supported only in unit schema and unit tests")},exports.unit=gt,exports.unmock=vt,exports.useOwn=function(t){if(!et&&!mt())throw new Error("Unsupported useOwn outside render function");const e=Array.prototype.slice.call(arguments,1);return ut(t,0,e.length?e:0)},exports.useShared=function(t){if(!et&&!mt())throw new Error("Unsupported useShared outside render function");return ut(t,Pt)}; |
{ | ||
"name": "realar", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "React state manager", | ||
@@ -14,5 +14,4 @@ "repository": { | ||
"bootstrap": "npx lerna bootstrap --no-ci && npm run build", | ||
"clean": "npm run test:clean && npm run demo:clean && npm run build:clean && npm run perf:clean && lerna clean --yes", | ||
"clean": "npm run test:clean && npm run build:clean && npm run perf:clean && lerna clean --yes", | ||
"publish": "npm run test && npm run build:prod && lerna publish", | ||
"start": "npm run bootstrap && npm run demo", | ||
"reinstall": "npm run clean && npm run bootstrap", | ||
@@ -25,4 +24,2 @@ "test": "npm run types:ts:check && npm run test:clean && npm run test:run", | ||
"test:clean:ci": "npm run test:clean && rimraf coverage", | ||
"demo": "npm run demo:clean && cd demo && cross-env NODE_ENV=development parcel index.html --port 1210", | ||
"demo:clean": "cd demo && rimraf dist .cache", | ||
"build:clean": "rimraf build", | ||
@@ -55,3 +52,2 @@ "build:lib": "rollup -c", | ||
"react hooks", | ||
"micro frontends", | ||
"reactive programming", | ||
@@ -110,3 +106,3 @@ "typescript", | ||
}, | ||
"gitHead": "f85ccacc57fd3bd00a9fd99df42d8bff82c1faad" | ||
"gitHead": "380b2e7683c038f3f270da0a60299e752ac699c4" | ||
} |
# Realar <sup><sup><small><small>βeta</small></small></sup></sup> | ||
[![npm version](https://img.shields.io/npm/v/realar?style=flat-square)](https://www.npmjs.com/package/realar) [![typescript support](https://img.shields.io/npm/types/typescript?style=flat-square)](./lib/types/typescript.d.ts) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/realar@0.2.0?style=flat-square)](https://bundlephobia.com/result?p=realar@0.2.0) [![code coverage](https://img.shields.io/coveralls/github/betula/realar?style=flat-square)](https://coveralls.io/github/betula/realar) | ||
[![npm version](https://img.shields.io/npm/v/realar?style=flat-square)](https://www.npmjs.com/package/realar) [![typescript support](https://img.shields.io/npm/types/typescript?style=flat-square)](./lib/types/typescript.d.ts) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/realar?style=flat-square)](https://bundlephobia.com/result?p=realar) [![code coverage](https://img.shields.io/coveralls/github/betula/realar?style=flat-square)](https://coveralls.io/github/betula/realar) | ||
Reactive state manager for React. | ||
Imperative, Light, Fast and Pretty looked :kissing_heart: | ||
Impressive, Light, Fast, and Pretty looked :kissing_heart: | ||
@@ -13,10 +13,10 @@ ### Usage | ||
import React from "react"; | ||
import axios from "axios"; | ||
import { unit, useOwn } from "realar"; | ||
import axios from "axios"; | ||
const Todos = unit({ | ||
todos: [], | ||
todos: [], // Init immutable store | ||
async fetch() { | ||
const { data } = await axios.get("/api/todos"); | ||
this.todos = data; | ||
this.todos = data; // Update immutable store | ||
}, | ||
@@ -26,3 +26,3 @@ constructor() { | ||
}, | ||
// get completed() { | ||
// get completed() { // Cached selector | ||
// return this.todos.filter(task => task.completed); | ||
@@ -33,2 +33,3 @@ // }, | ||
const App = () => { | ||
// Use the own instance of Todos | ||
const { todos, fetch } = useOwn(Todos); | ||
@@ -42,3 +43,3 @@ | ||
return ( | ||
<ul>{todos.map((todo) => <li>{todo.text}</li>)}</ul> | ||
<ul>{todos.map(todo => <li>{todo.text}</li>)}</ul> | ||
); | ||
@@ -54,2 +55,8 @@ }; | ||
### Demos | ||
+ [Hello](https://github.com/realar-project/hello) - shared state demonstration. | ||
+ [Todos](https://github.com/realar-project/todos) - todomvc implementation. | ||
### Installation | ||
@@ -76,9 +83,1 @@ | ||
### Demo | ||
```bash | ||
git clone git@github.com:betula/realar.git | ||
cd realar | ||
npm run start | ||
# Open http://localhost:1210 in your browser |
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
40747
700
78