Skip to content

Commit df5d4e5

Browse files
committed
fix: debounce editor updates
1 parent a1b3863 commit df5d4e5

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"@microsoft/api-extractor": "^7.42.3",
6666
"@rollup/plugin-replace": "^5.0.5",
6767
"@types/codemirror": "^5.60.15",
68+
"@types/lodash-es": "^4.17.12",
6869
"@types/node": "^20.11.25",
6970
"@types/prettier": "^2.7.3",
7071
"@vitejs/plugin-vue": "^5.0.4",
@@ -79,6 +80,7 @@
7980
"fflate": "^0.8.2",
8081
"hash-sum": "^2.0.0",
8182
"lint-staged": "^15.5.1",
83+
"lodash-es": "^4.17.21",
8284
"monaco-editor-core": "^0.46.0",
8385
"monaco-editor-textmate": "^4.0.0",
8486
"monaco-textmate": "^3.0.1",

pnpm-lock.yaml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/monaco/Monaco.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import parserBabel from 'prettier/plugins/babel'
2121
import parserHtml from 'prettier/plugins/html'
2222
import parserPostcss from 'prettier/plugins/postcss'
2323
import prettier from 'prettier/standalone'
24+
import debounce from 'lodash-es/debounce'
2425
2526
const props = withDefaults(
2627
defineProps<{
@@ -149,8 +150,21 @@ onMounted(async () => {
149150
store.state.showFileExplorer = !store.state.showFileExplorer
150151
})
151152
152-
editorInstance.onDidChangeModelContent(() => {
153+
function onUpdate () {
153154
emit('change', editorInstance.getValue())
155+
}
156+
157+
let debouncedUpdate = debounce(onUpdate, 2000)
158+
159+
watch(() => !!store.state.errors.length, hasErrors => {
160+
debouncedUpdate.cancel()
161+
debouncedUpdate = hasErrors
162+
? debounce(onUpdate, 5000)
163+
: debounce(onUpdate, 2000)
164+
})
165+
166+
editorInstance.onDidChangeModelContent(() => {
167+
debouncedUpdate()
154168
})
155169
156170
editorInstance.onDidBlurEditorWidget(async () => {
@@ -178,6 +192,7 @@ onMounted(async () => {
178192
} catch (err) {}
179193
180194
if (code !== props.value) {
195+
debouncedUpdate.cancel()
181196
emit('change', code)
182197
}
183198
})

0 commit comments

Comments
 (0)