Skip to content

Save strokes off the main thread in DrawingCanvasViewModel. - #136

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_1022860221
Nov 24, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
test_1022860221

Conversation

@copybara-service

Copy link
Copy Markdown

Save strokes off the main thread in DrawingCanvasViewModel.

Every finished stroke, undo, redo, erase, and clear called saveStrokes() on the main thread, where OfflineNotesRepository.updateNoteStrokes re-serialized every stroke on the canvas (JNI encode, GZIP, and JSON) before the next frame. This made drawing slower as a note accumulated strokes.

Launch saves on the injected @ApplicationScope (backed by Dispatchers.Default) instead, so serialization happens off the main thread and a save still finishes if the drawing screen closes. A Mutex runs saves one at a time, and a save is skipped if a newer one was requested while it waited, so rapid strokes don't queue redundant full serializations.

Also remove onCleared's save, which launched into the already-canceled viewModelScope and never ran; each edit now queues its own save on applicationScope.

Every finished stroke, undo, redo, erase, and clear called `saveStrokes()` on the main thread, where `OfflineNotesRepository.updateNoteStrokes` re-serialized every stroke on the canvas (JNI encode, GZIP, and JSON) before the next frame. This made drawing slower as a note accumulated strokes.

Launch saves on the injected `@ApplicationScope` (backed by `Dispatchers.Default`) instead, so serialization happens off the main thread and a save still finishes if the drawing screen closes. A `Mutex` runs saves one at a time, and a save is skipped if a newer one was requested while it waited, so rapid strokes don't queue redundant full serializations.

Also remove `onCleared`'s save, which launched into the already-canceled `viewModelScope` and never ran; each edit now queues its own save on `applicationScope`.

PiperOrigin-RevId: 1029163215
@copybara-service
copybara-service Bot merged commit ccecace into main Nov 24, 2026
5 checks passed
@copybara-service
copybara-service Bot deleted the test_1022860221 branch November 24, 2026 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant