Created
February 1, 2019 16:44
-
-
Save TobyEalden/99d91c95161c15bd39bc39877a9eb290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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