|
const loadTodos = () => { |
|
console.log(`loadTodos starting live query`) |
|
db.extensions.live.query<Todo>("select * from todos order by text").then((todos) => { |
|
try { |
|
setTodos(todos.initialResults.rows) |
|
const callback = (newTodos: LiveQueryResults<Todo>) => { |
|
console.log(`live query todos got new rows`, newTodos.rows) |
|
setTodos(newTodos.rows) |
|
} |
|
todos.subscribe(callback) |
|
return () => todos.unsubscribe(callback) |
|
} catch (e) { |
|
console.error(`Error setting up live query for todos`, e) |
|
} |
|
}) |
|
} |
Hi! Your project is very interesting to me and I think I want to write something fun using it. I was looking at the code and wanted to clarify a little thing. I know that devs can return cleanup function from useEffect, but what's confusing to me is that you return it from db.extensions.live.query asynchronous callback.
Did you mean to write something like this?
const loadTodos = () => {
let unsubscriber: () => void = () => {};
console.log(`loadTodos starting live query`)
db.extensions.live.query<Todo>("select * from todos order by text").then((todos) => {
try {
setTodos(todos.initialResults.rows)
const callback = (newTodos: LiveQueryResults<Todo>) => {
console.log(`live query todos got new rows`, newTodos.rows)
setTodos(newTodos.rows)
}
todos.subscribe(callback)
unsubscriber = () => todos.unsubscribe(callback)
} catch (e) {
console.error(`Error setting up live query for todos`, e)
}
})
return () => unsubscriber()
}
synchrotron/examples/todo-app/src/db/electric.tsx
Lines 15 to 30 in a0ba9fe
Hi! Your project is very interesting to me and I think I want to write something fun using it. I was looking at the code and wanted to clarify a little thing. I know that devs can return cleanup function from
useEffect, but what's confusing to me is that you return it fromdb.extensions.live.queryasynchronous callback.Did you mean to write something like this?