Skip to content

Commit

Permalink
fix: debounce changes in code mode, fixing a race condition in React …
Browse files Browse the repository at this point in the history
…(see #23)
  • Loading branch information
josdejong committed Oct 13, 2021
1 parent fdfc6e7 commit bc2c559
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dev": "svelte-kit dev",
"preview": "svelte-kit preview",
"build": "npm run package",
"build:watch": "npm run package -- --watch",
"package": "svelte-kit package && rollup --config rollup.config.bundle.js",
"test": "mocha",
"check": "svelte-check --tsconfig ./tsconfig.json",
Expand Down
11 changes: 10 additions & 1 deletion src/lib/components/modes/codemode/CodeMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { getContext, onDestroy, onMount } from 'svelte'
import {
CHECK_VALID_JSON_DELAY,
CODE_MODE_ONCHANGE_DELAY,
JSON_STATUS_INVALID,
JSON_STATUS_REPAIRABLE,
JSON_STATUS_VALID,
Expand Down Expand Up @@ -79,7 +80,7 @@
ace,
readOnly,
indentation,
onChange: onChangeAceEditorValue
onChange: onChangeAceEditorValueDebounced
})
if (resizeObserver) {
Expand Down Expand Up @@ -529,6 +530,14 @@
const updateCancelUndoRedoDebounced = debounce(updateCanUndoRedo, 0) // just on next tick
// debounce the input: when pressing Enter at the end of a line, two change
// events are fired: one with the new Return character, and a second with
// indentation added on the new line. This causes a race condition when used
// for example in React. Debouncing the onChange events also results in not
// firing a change event with every character that a user types, but only as
// soon as the user stops typing.
const onChangeAceEditorValueDebounced = debounce(onChangeAceEditorValue, CODE_MODE_ONCHANGE_DELAY)
function emitOnChange() {
if (onChange) {
onChange(text)
Expand Down
3 changes: 2 additions & 1 deletion src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const ACTIVE_SEARCH_RESULT = Symbol('search:active-result')
export const VALIDATION_ERROR = Symbol('validation:error')

export const SCROLL_DURATION = 300 // ms
export const DEBOUNCE_DELAY = 300
export const DEBOUNCE_DELAY = 300 // ms
export const CODE_MODE_ONCHANGE_DELAY = 300 // ms
export const SEARCH_UPDATE_THROTTLE = 300 // ms
export const CHECK_VALID_JSON_DELAY = 300 // ms
export const AUTO_SCROLL_INTERVAL = 50 // ms
Expand Down

0 comments on commit bc2c559

Please sign in to comment.