import { Accessor, createSignal, onMount, onCleanup } from "solid-js";
type LiveQueryResult = {
readonly docs: Doc[];
readonly rows: any[];
};
// NOTE: database is an accessor defined outside of this custom hook (via createMemo). createLiveQuery is effectively the inner function of an outer closure.
const createLiveQuery = (key: string, query = {}, initialRows: any[] = []): Accessor<LiveQueryResult> => {
const [result, setResult] = createSignal({
rows: initialRows,
docs: initialRows.map((r) => r.doc),
});
const refreshRows = async () => {
const res = await database().query(key, query);
setResult({ ...res, docs: res.rows.map((r) => r.doc) });
};
onMount(() => {
// this is where I receive the ESLint warning. Specifically on the callback passed to subscribe as the `refreshRows` function has internal reactivity due to using database() under the hood.
const unsubscribe = database().subscribe(() => void refreshRows());
onCleanup(() => {
unsubscribe();
});
});
return result;
};
import { Accessor, createSignal, onMount, onCleanup } from "solid-js";
type LiveQueryResult = {
readonly docs: Doc[];
readonly rows: any[];
};
// NOTE: database is an accessor defined outside of this custom hook (via createMemo). createLiveQuery is effectively the inner function of an outer closure.
const createLiveQuery = (key: string, query = {}, initialRows: any[] = []): Accessor<LiveQueryResult> => {
const [result, setResult] = createSignal({
rows: initialRows,
docs: initialRows.map((r) => r.doc),
});
const refreshRows = async () => {
const res = await database().query(key, query);
setResult({ ...res, docs: res.rows.map((r) => r.doc) });
};
onMount(() => {
// this is where I receive the ESLint warning. Specifically on the callback passed to subscribe as the `refreshRows` function has internal reactivity due to using database() under the hood.
const unsubscribe = database().subscribe(() => void refreshRows());
onCleanup(() => {
unsubscribe();
});
});
return result;
};