Skip to content

Commit

Permalink
fix: visual query auto save lag
Browse files Browse the repository at this point in the history
  • Loading branch information
nextchamp-saqib committed Aug 22, 2023
1 parent 94827bf commit 30ae7db
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
48 changes: 23 additions & 25 deletions frontend/src/notebook/blocks/query/useQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,27 @@ function makeQuery(name) {
resultColumns: [],
})

state.reload = async function () {
return resource.get.fetch().then((doc) => {
state.doc = doc
state.isOwner = state.doc.owner == auth.user.user_id
state.loading = false
state.unsaved = false
})
state.reload = async () => {
return resource.get.fetch().then(() => state.syncDoc())
}

state.syncDoc = async function () {
state.doc = resource.doc
state.isOwner = state.doc.owner == auth.user.user_id
state.loading = false
state.unsaved = false
}

state.convertToNative = async function () {
state.loading = true
await resource.convert_to_native.submit()
await state.reload()
await state.syncDoc()
state.loading = false
}
state.convertToAssisted = async function () {
state.loading = true
await resource.convert_to_assisted.submit()
await state.reload()
await state.syncDoc()
state.loading = false
}

Expand All @@ -66,7 +68,7 @@ function makeQuery(name) {
await state.save()
await resource.run
.submit()
.then(() => state.reload())
.then(() => state.syncDoc())
.catch((e) => {
console.error(e)
})
Expand All @@ -77,7 +79,7 @@ function makeQuery(name) {
state.loading = true
const updatedFields = getUpdatedFields()
await resource.setValue.submit(updatedFields)
await state.reload()
await state.syncDoc()
state.loading = false
}

Expand All @@ -92,24 +94,20 @@ function makeQuery(name) {
return updatedFields
}

watchOnce(
() => state.autosave,
() => {
const saveIfChanged = throttle(function (newVal, oldVal) {
if (!oldVal || !newVal) return
if (state.loading) return
if (JSON.stringify(newVal) == JSON.stringify(oldVal)) return
state.save()
}, 3000)
// TODO: fix the weird bug where the inputs are not selected when auto-saving
watchDebounced(getUpdatedFields, saveIfChanged, { deep: true, debounce: 1000 })
watchOnce(() => state.autosave, setupAutosaveListener)

function setupAutosaveListener() {
const saveIfChanged = function (newVal, oldVal) {
if (!oldVal || !newVal) return
if (JSON.stringify(newVal) == JSON.stringify(oldVal)) return
state.save()
}
)
watchDebounced(getUpdatedFields, saveIfChanged, { deep: true, debounce: 1000 })
}

const setUnsaved = (newVal, oldVal) => {
if (state.unsaved) return
if (!oldVal || !newVal) return
if (state.loading) return
if (JSON.stringify(newVal) == JSON.stringify(oldVal)) return
state.unsaved = true
}
Expand Down Expand Up @@ -142,7 +140,7 @@ function makeQuery(name) {
state.updateDoc = async (doc) => {
state.loading = true
await resource.setValue.submit(doc)
await state.reload()
await state.syncDoc()
state.loading = false
}

Expand Down
4 changes: 1 addition & 3 deletions frontend/src/query/Result/QueryResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ const isNumberColumn = computed(() => {
})
if (settings.auto_execute_query) {
watch(needsExecution, (newVal, oldVal) => newVal && !oldVal && query.execute(), {
immediate: true,
})
watch(needsExecution, (newVal, oldVal) => newVal && !oldVal && query.execute())
}
const executionTime = computed(() => {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/utils/query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export function useQuery(name) {
query.beforeExecuteFns.push(fn)
}
query.execute = async () => {
query.run.loading = true
if (query.beforeExecuteFns.length) {
await Promise.all(query.beforeExecuteFns.map((fn) => fn()))
await query.get.fetch()
}
return query.debouncedRun(null, {
onSuccess() {
Expand All @@ -52,6 +52,7 @@ export function useQuery(name) {
await query.setValue.submit(doc)
}

window.queryTwo = query
return query
}

Expand Down

0 comments on commit 30ae7db

Please sign in to comment.