S
SolidJS10mo ago
halu

Integrate External, Read-Only Data Source Into Reactive System

Suppose the following:
// Hypothetical external data
let externalData = new Map()
EventEmitter.emit("dataChanged", /*no event info*/)

// My problem...
function externMemo (externData) {
EventEmitter.on("dataChanged", ()=>/*trigger getter refetch & dom update*/ )
let getter = ()=>{
// ...
return externData
}

return getter
}

// Hypothetical Component
let component = () => {
let reactiveData = externMemo(externalData)
return (
<div>
{reactiveData().values() /*returns current externalData values*/}
</div>
)
}
// Hypothetical external data
let externalData = new Map()
EventEmitter.emit("dataChanged", /*no event info*/)

// My problem...
function externMemo (externData) {
EventEmitter.on("dataChanged", ()=>/*trigger getter refetch & dom update*/ )
let getter = ()=>{
// ...
return externData
}

return getter
}

// Hypothetical Component
let component = () => {
let reactiveData = externMemo(externalData)
return (
<div>
{reactiveData().values() /*returns current externalData values*/}
</div>
)
}
How do I get that external event to trigger DOM updates and recall the getter? https://playground.solidjs.com/anonymous/c937bc30-3d80-432c-814b-956ee3b58efa
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
10 Replies
halu
halu10mo ago
so I have two ways: plain old signal with explicit non-equal setter:
function externMemo(externData: Map<Date, number>) {
let [val, setVal] = createSignal(externData, {equals: false, name: "trigger"})
EE.on("dataChanged", ()=>{
console.log( "dataChanged", JSON.stringify([...externalData.values()]) )
setVal(externData)
})
return val
}
function externMemo(externData: Map<Date, number>) {
let [val, setVal] = createSignal(externData, {equals: false, name: "trigger"})
EE.on("dataChanged", ()=>{
console.log( "dataChanged", JSON.stringify([...externalData.values()]) )
setVal(externData)
})
return val
}
Tracker with vanilla getter:
function externMemo(externData: Map<Date, number>) {
let [track, dirty] = createSignal(undefined, {equals: false, name: "trigger"})

let test = ()=>{
track()
console.log( 'dataChanged', JSON.stringify([...externalData.values()]) )
return externData
}

EE.on("dataChanged", ()=>{
console.log("dataChanged")
dirty()
})

return test
}
function externMemo(externData: Map<Date, number>) {
let [track, dirty] = createSignal(undefined, {equals: false, name: "trigger"})

let test = ()=>{
track()
console.log( 'dataChanged', JSON.stringify([...externalData.values()]) )
return externData
}

EE.on("dataChanged", ()=>{
console.log("dataChanged")
dirty()
})

return test
}
So my follow up questions: 1. for very large external data, would using the Tracker method be more performant and memory efficient than the plain old signal method? 2. are there any lower level solutions?
halu
halu10mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
bigmistqke 🌈
bigmistqke 🌈10mo ago
memory wise it will be exactly the same I just wouldn't use <For/> to loop through the data. since For is keyed on reference it would create a new html-element on each update (since the data coming in will be completely new each time, i assume).
bigmistqke 🌈
bigmistqke 🌈10mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
bigmistqke 🌈
bigmistqke 🌈10mo ago
so you could use Index
bigmistqke 🌈
bigmistqke 🌈10mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
bigmistqke 🌈
bigmistqke 🌈10mo ago
if you would still need to switch around rows, you might wanna use setStore with reconcile, but it comes with the cost of a diff and a proxy (and it sounds to me ur use case is more vast amounts of data coming in and needing to be visualised)
bigmistqke 🌈
bigmistqke 🌈10mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
bigmistqke 🌈
bigmistqke 🌈10mo ago
but ye, especially in ur example. memory would be exactly the same. you pass around this reference to this externalData-map. bit confused with the set-up ngl.
halu
halu10mo ago
I'm wrapping a DB lib, it handles a cache that keeps the data in sync with the server So I need to listen to changes on that cache in a reactive manor my worry was that createSignal would somehow instance the data to make it reactive ( I forget how it works under the hood ) if that's not he case then all is good 👍 thx
Want results from more Discord servers?
Add your server
More Posts
vite bundling unused components from external libraries (SSR)o/ question, i'm using https://github.com/x64Bits/solid-icons for icons and while i really like it, How to mutate a createResource storageHi all, I'd like to get some help with mutating a store that is set as a createResource storage. ``Are there any important considerations to creating signals in control component callbacks like ForHi, wondering if there's anything to consider in terms of tracking etc in having something like thisDispose function not actually disposing of anythingI'm running Solid as a partial view inside another, non-Solid app. I've used webpack to export SolidReact key prop substitute!I know what you're thinking, James, you silly sausage - Solid.js doesn't require keys for loops, andBest way to dynamically access i18n-translations with typescriptHey folks, I am currently using @solid-primitives/i18n (https://github.com/solidjs-community/solid-i18n and context issuesHey folks, I am currently migrating to the 2.0.0 release of `@solid-primitives/i18n`. (https://gitDifference between `setState` and `produce`?I have `appState` as a store, and I'm trying to pass `appState.selectedTable` as a prop to a child cSolid Start not deploying on vercel in turborepoHi all, my solid start project doesn't deploy to vercel, aka every build fails. There are no error'Problem duplicating a component navigating with dynamic routingI am having difficulty causing a route component to be duplicated when navigating between a dynamic