S
SolidJS13mo ago
garth

Signal Factories and eslint warnings

I've created a factory for creating signals from a yjs document. Everything seems work ok, but the linter is giving warnings. Is the linter correct or is it a false positive due to the factory?
const metaSignal = <T = unknown,>(
provider: Accessor<HocuspocusProvider>,
key: string,
defaultValue: T,
): Accessor<T | undefined> =>
from<T>((set) => {
const document = provider().document
const meta = document.getMap<T>('meta')
const observer = (event: Y.YMapEvent<T>) => {
if (event.keysChanged.has(key)) {
set(() => meta.get(key) ?? defaultValue)
}
}
meta.observe(observer)
return () => meta.unobserve(observer)
})

const theme = metaSignal(provider, 'theme', 'white')
const metaSignal = <T = unknown,>(
provider: Accessor<HocuspocusProvider>,
key: string,
defaultValue: T,
): Accessor<T | undefined> =>
from<T>((set) => {
const document = provider().document
const meta = document.getMap<T>('meta')
const observer = (event: Y.YMapEvent<T>) => {
if (event.keysChanged.has(key)) {
set(() => meta.get(key) ?? defaultValue)
}
}
meta.observe(observer)
return () => meta.unobserve(observer)
})

const theme = metaSignal(provider, 'theme', 'white')
The last line gives the following lint warning: The reactive variable 'provider' should be used within JSX, a tracked scope (like createEffect), or inside an event handler function, or else changes will be ignored.
2 Replies
foxpro 🐍
foxpro 🐍13mo ago
Try to pass provider(), not the provider to metaSignal, if you want to read one time without watching for changes
REEEEE
REEEEE13mo ago
Not sure how from works but often the liner gives false positives. The usage could be correct but it doesn't know how exactly you're using it and just warns that since you're assigning the a signal value to a variable, it might not be reactive In this case, I think it should be good