Skip to content

Commit

Permalink
fix: query autosave issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nextchamp-saqib committed Aug 30, 2023
1 parent 080b0e0 commit 0c426d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
10 changes: 4 additions & 6 deletions frontend/src/notebook/blocks/query/builder/QueryBuilder.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script setup>
import UseTooltip from '@/components/UseTooltip.vue'
import useDataSourceTable from '@/datasource/useDataSourceTable'
import { FIELDTYPES, isDimensionColumn } from '@/utils'
import { FIELDTYPES, isDimensionColumn, safeJSONParse } from '@/utils'
import { dateFormats } from '@/utils/format'
import { Sigma } from 'lucide-vue-next'
import { computed, inject, provide, ref } from 'vue'
import { computed, inject, provide, ref, watch } from 'vue'
import useQuery from '../useQuery'
import ColumnExpressionEditor from './ColumnExpressionEditor.vue'
import ColumnSelector from './ColumnSelector.vue'
Expand All @@ -25,10 +25,8 @@ provide('query', query)
const legacyQuery = inject('query')
legacyQuery.beforeExecute?.(async () => await query.save())
const state = computed({
get: () => (typeof query.doc.json == 'string' ? JSON.parse(query.doc.json) : query.doc.json),
set: (value) => (query.doc.json = value),
})
const state = ref(safeJSONParse(query.doc.json))
watch(state, (value) => (query.doc.json = value), { deep: true })
const selectedTables = computed(() => {
const tables = [state.value.table]
Expand Down
35 changes: 19 additions & 16 deletions frontend/src/notebook/blocks/query/useQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function makeQuery(name) {
}

state.syncDoc = async function () {
state.doc = resource.doc
state.doc = { ...resource.doc }
state.isOwner = state.doc.owner == auth.user.user_id
state.loading = false
state.unsaved = false
Expand Down Expand Up @@ -75,12 +75,19 @@ function makeQuery(name) {
state.executing = false
}, 300)

state.save = async () => {
state.loading = true
const updatedFields = getUpdatedFields()
await resource.setValue.submit(updatedFields)
await state.syncDoc()
state.loading = false
watchOnce(() => state.autosave, setupAutosaveListener)
function setupAutosaveListener() {
const saveIfChanged = function (newVal, oldVal) {
if (state.executing) return
if (!oldVal || !newVal) return
if (JSON.stringify(newVal) == JSON.stringify(oldVal)) return
state.save()
}
watchDebounced(getUpdatedFields, saveIfChanged, { deep: true, debounce: 5000 })
window.onbeforeunload = (event) => {
state.unsaved && state.save()
event.preventDefault()
}
}

function getUpdatedFields() {
Expand All @@ -94,15 +101,11 @@ function makeQuery(name) {
return updatedFields
}

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 })
state.save = async () => {
state.loading = true
const updatedFields = getUpdatedFields()
await resource.setValue.submit(updatedFields)
state.loading = false
}

const setUnsaved = (newVal, oldVal) => {
Expand Down

0 comments on commit 0c426d8

Please sign in to comment.