josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
818 stars 108 forks source link

Debounced onChange renders onBlur useless #275

Closed ZbigniewRA closed 1 year ago

ZbigniewRA commented 1 year ago

Debounced onChange events in text-mode of this JSON editor cause issues.

Consider a form with a control and a Save button. Normally when we get onBlur on a control we know that editing stopped, and Save button handler can safely access the value.

But since in JSON editor onChange is debounced it can arrive after onBlur. So in Save button handler we can't really know if we see all of user's edits, or not yet. So Save can potentially ignore last edits user has made. Other than adding a sleep in onSave handler we have no way of waiting for completion of user interaction (as onBlur is no longer fit for this purpose).

I think that onBlur should first call onChange immediately if some changes are pending (and cancel the debounced onChange).

josdejong commented 1 year ago

Very good point, thanks for bringing up this issue. I fixed it via e8270e99354cc965dea209a23f60dd5c9000ca57, making sure debounced changes are committed before an onBlur and before destroying the editor.

josdejong commented 1 year ago

Fixed now in v0.17.4

ZbigniewRA commented 1 year ago

Wow, that was fast. Thanks a lot!