DEV Community

Oleksandra
Oleksandra

Posted on

How I Fixed Vanishing Pets in vscode-pets

Hacktoberfest: Contribution Chronicles

For my Hacktoberfest contribution I fixed a bug in the vscode-pets repo where extra pets you spawned in the pets panel would vanish whenever you switched workspaces or opened a different folder. Before the fix, only the single default pet from settings would remain. Now, pets you spawn in the webview are synced to the extension host, saved in the extension’s global storage, and restored when the panel or explorer view is recreated, so they persist across workspace changes.

I worked on Issue #809. The problem was easy to reproduce: spawn a few extra pets, switch folders or close/reopen the panel, and those extra pets were gone. My fix makes the webview tell the extension which pets it currently has, the extension saves that list and later sends it back to the webview to recreate the pets. I also added a simple deduplication check (type + name + color) so pets don’t multiply during restores, that was something I ran into while testing and it was very annoying. Lastly, I centralized message handling between the webview and extension to keep the communication clearer and make future messages easier to add.

Working on this taught me a bunch: how the webview and extension host talk to each other, how VS Code globalState works, and how small UX decisions can ripple into bigger problems. Early on I got a lot of little bugs and tried to have a timer to wait before sending the stored pet list to the webview. It did work, but it was brittle: it created a race condition, and felt flaky. After doing a bit more research I replaced the timer with a proper message handshake that you can see in my PR. The webview sends webview-ready when it’s actually loaded, the extension replies with restore-pets, and we perform a simple dedupe.

I’ve worked on this repo before, so getting started was straightforward. That said, I made a few design choices without a strict spec, and I’d like feedback from the repo maintainers on whether this behavior should be global or workspace-specific. A future improvement would be a setting that lets users choose whether pets persist globally or per workspace.

Top comments (0)