garth
garth
SSolidJS
Created by garth on 7/19/2023 in #support
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.
4 replies