# perf: debounce composer draft localStorage writes · gitcafe/t3code

[View on GitCafe](https://git.cafe/gitcafe/t3code/commit/90e0c2ab15e7034d8e6e0688b3996fd905c03017)

Repository: [gitcafe/t3code](https://git.cafe/gitcafe/t3code)

Visibility: public

Requested revision: 90e0c2ab15e7034d8e6e0688b3996fd905c03017

Requested commit: 90e0c2ab15e7034d8e6e0688b3996fd905c03017

Commit: 90e0c2ab15e7034d8e6e0688b3996fd905c03017

Tree: de4fbfce739db30ed26723fd7ea0e9a0cbfbadf0

Author: Chukwudi Nwobodo

Committer: Julius Marminge

## Message

```
perf: debounce composer draft localStorage writes

Every keystroke triggered full JSON serialization of all composer drafts
(including base64 image attachments) and a synchronous localStorage
write. At normal typing speed this caused 5+ writes/sec, blocking the
main thread and creating noticeable input lag.

Wrap the Zustand persist storage with a 300ms debounce. In-memory state
updates remain immediate; only the serialization and storage write are
deferred. A beforeunload handler flushes pending writes to prevent data
loss. The removeItem method cancels any pending setItem to avoid
resurrecting cleared drafts.

Adds unit tests for the DebouncedStorage utility covering debounce
timing, rapid writes, removeItem cancellation, flush, and edge cases.

```

## Parents

- [7a3d4d1f31e4316eb832f820012bf575aa57669a](https://git.cafe/gitcafe/t3code/commit/7a3d4d1f31e4316eb832f820012bf575aa57669a?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/t3code/tree/90e0c2ab15e7034d8e6e0688b3996fd905c03017?format=markdown)
