Skip to content
Commits on Source (2)
{
"name": "i4-js-commons",
"version": "3.9.16",
"version": "3.9.17",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......
{
"name": "i4-js-commons",
"version": "3.9.16",
"version": "3.9.17",
"description": "Just a load of common stuff for i4 front-ends and some probably usable in back-end as well",
"dependencies": {
"@material-ui/core": "^4.8.0",
......
......@@ -26,31 +26,41 @@ export const withSubs = ({
ttl = 60,
lengths,
from,
noHistoryCb,
}) => {
// settings: { myKey: { init: x, rid:'<aRid>'} }
// const dataKey = R.o(R.head, R.keys)(settings);
const doTransform = transform && typeof transform === 'function';
const getMsgTransform = R.curry(prevData =>
R.o(
x => (doTransform ? transform(x, prevData) : x),
x => (last ? msgMerge([prevData, x]) : x),
),
);
return Component => {
const WithSubs = props => {
// console.log('WithSubs render:', propName, channels, init, rest);
const unsubscribes = useRef([]);
const [point, setPoint] = useState(init);
const [points, setPoints] = useState([]);
console.log('WithSubs render:', points, point);
const setter = useCallback(
msg => {
// console.log('WithSubs setter:', msg);
setPoint(prevData => {
return R.o(
x => (doTransform ? transform(x, prevData) : x),
x => (last ? msgMerge([prevData, x]) : x),
)(msg);
});
console.log('WithSubs setter:', msg);
setPoint(prevData => getMsgTransform(prevData)(msg));
},
[setPoint],
);
const historySetter = useCallback(
msgs => {
console.log('WithSubs historySetter:', msgs);
setPoints(prevData => R.zipWith(getMsgTransform)(prevData, msgs));
},
[setPoints],
);
useEffect(() => {
// console.log('WithSubs init-effect:', channels);
if (!channels || channels.some(hasnt('rid'))) return undefined;
......@@ -62,7 +72,7 @@ export const withSubs = ({
lengths,
from,
cb: setter,
historyCb: setPoints,
...(noHistoryCb ? {} : { historyCb: historySetter }),
}).then(unsubs => (unsubscribes.current = unsubs));
return () => unsubscribes.current.forEach(f => f());
}, []);
......@@ -72,7 +82,7 @@ export const withSubs = ({
ttl={ttl}
{...props}
{...{ [propName]: point }}
{...{ [historyPropName]: points }}
{...(noHistoryCb ? {} : { [historyPropName]: points })}
/>
);
};
......