Skip to content

Commit

Permalink
try to fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed Feb 16, 2022
1 parent 43bfeaa commit 4b9eccb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
62 changes: 61 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,61 @@
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.newRidgeState=void 0;const react_1=require("react"),useIsomorphicLayoutEffect="undefined"!=typeof window||"undefined"!=typeof document?react_1.useLayoutEffect:react_1.useEffect,equ=(e,t)=>e===t,FR={};function useComparator(e,t=equ){const r=(0,react_1.useRef)(FR);let u=r.current;return useIsomorphicLayoutEffect(()=>{r.current=u}),u=r.current!==FR&&t(e,r.current)?u:e}function newRidgeState(e,u){let n=[],o=e;function c(e,t){const r=o;o=e instanceof Function?e(o):e,setTimeout(()=>{n.forEach(e=>e(o,r)),t?.(o,r),u?.onSet?.(o,r)})}function s(t){return n.push(t),()=>{n=n.filter(e=>e!==t)}}function a(){var e,[t,r]=(0,react_1.useState)(o);return e=r,useIsomorphicLayoutEffect(()=>s(e),[e]),[t,c]}return{use:a,useSelector:function(e,t=equ){var[r]=a();return useComparator(e(r),t)},useValue:()=>a()[0],get:()=>o,set:c,reset:()=>c(e),subscribe:s}}exports.newRidgeState=newRidgeState;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.newRidgeState = void 0;
const react_1 = require("react");
const useIsomorphicLayoutEffect = typeof window !== "undefined" || typeof document !== "undefined"
? react_1.useLayoutEffect
: react_1.useEffect;
const equ = (a, b) => a === b;
const FR = {};
function useComparator(v, c = equ) {
const f = (0, react_1.useRef)(FR);
let nv = f.current;
useIsomorphicLayoutEffect(() => {
f.current = nv;
});
if (f.current === FR || !c(v, f.current)) {
nv = v;
}
return nv;
}
function newRidgeState(initialValue, options) {
let sb = [];
let v = initialValue;
function set(newValue, callback) {
const pv = v;
v = newValue instanceof Function ? newValue(v) : newValue;
setTimeout(() => {
sb.forEach((c) => c(v, pv));
callback?.(v, pv);
options?.onSet?.(v, pv);
});
}
function subscribe(subscriber) {
sb.push(subscriber);
return () => {
sb = sb.filter((f) => f !== subscriber);
};
}
function useSubscription(subscriber) {
useIsomorphicLayoutEffect(() => subscribe(subscriber), [subscriber]);
}
function use() {
const [l, s] = (0, react_1.useState)(v);
useSubscription(s);
return [l, set];
}
function useSelector(selector, comparator = equ) {
const [rv] = use();
return useComparator(selector(rv), comparator);
}
return {
use,
useSelector,
useValue: () => use()[0],
get: () => v,
set,
reset: () => set(initialValue),
subscribe,
};
}
exports.newRidgeState = newRidgeState;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-ridge-state",
"version": "4.2.6",
"version": "4.2.7",
"description": "react-ridge-state is a very simple global state management library for React and React Native",
"type": "module",
"main": "dist/index.js",
Expand All @@ -11,7 +11,7 @@
"scripts": {
"mocha": "mocha -r jsdom-global/register -r ts-node/register tests/**/*.mocha.tsx",
"test": "jest --maxWorkers=150",
"build": "tsc && npm run minify",
"build": "tsc",
"lint": "eslint src tests",
"minify": "uglifyjs --compress --mangle --output dist/index.js -- dist/index.js"
},
Expand Down

0 comments on commit 4b9eccb

Please sign in to comment.