Skip to content

Instantly share code, notes, and snippets.

@TobyEalden
Created February 1, 2019 16:44
Show Gist options
  • Save TobyEalden/99d91c95161c15bd39bc39877a9eb290 to your computer and use it in GitHub Desktop.
Save TobyEalden/99d91c95161c15bd39bc39877a9eb290 to your computer and use it in GitHub Desktop.
import React from "react";
const useDataSubscription = (tdx, datasetId, selector, options) => {
const [data, setData] = React.useState([]);
React.useEffect(
() => {
let subscription;
let observer;
if (tdx) {
subscription = tdx.subscribe(
"dataset-data",
[
datasetId,
selector,
options
],
() => {
const clientSelector = {...selector, _d: datasetId};
observer = tdx.cache.data.find(clientSelector).observe(
() => setData(tdx.cache.data.find(clientSelector, options))
);
}
);
}
return () => {
observer && observer.stop();
subscription && tdx.unsubscribe(subcription);
}
},
[]
);
return data;
};
const SubcriptionTest = ({context}) => {
const data = useDataSubscription(
context.tdxConnections.defaultTDX,
"B1eaPY-F7f"
);
return (
<div>
<p>{data.length}</p>
<pre>
{JSON.stringify(data, null, 2)}
</pre>
</div>
);
};
export default SubcriptionTest;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment