Back to KB
Difficulty
Intermediate
Read Time
4 min

Building a Universal Drafts System in a VS Code Extension β€” Part 1: Types & Storage

By freeraveΒ·Β·4 min read

Current Situation Analysis

VS Code extensions frequently require persistent, session-spanning draft storage for partial inputs, configuration snippets, or multi-step workflows. Traditional approaches typically rely on one of three patterns: raw vscode.Memento usage, direct workspace.fs JSON file manipulation, or global extension state variables. Each introduces critical failure modes:

  • Raw Memento Abuse: Storing untyped JSON blobs directly in context.globalState or workspaceState leads to schema drift, silent data corruption, and unmanageable key collisions across features.
  • File-Based Storage: Using vscode.workspace.fs for drafts introduces I/O latency, lacks atomicity, and requires manual serialization/deserialization. Concurrent writes from multiple extension instances or webviews frequently corrupt files.
  • Global State Caching: Keeping drafts in-memory (Map/Object) loses data on extension reload or host restart, forcing users to re-enter work.

These methods fail because they conflate type definition, storage I/O, and business logic. They lack versioning, optimistic concurrency control, and a unified upsert contract, resulting in race conditions, bloated storage quotas, and poor developer experience when scaling to multi-workspace or multi-document scenarios.

WOW Moment: Key Findings

We benchmarked three storage strategies across a simulated multi-workspace extension environment (50 concurrent draft operations, 10k iterations). The proposed DraftsService with typed upsert and transactional Memento backing significantly outperforms legacy approaches in latency, integrity, and developer ergonomics.

ApproachWrite Latency (ms)Storage Overhead (KB)Type SafetyConcurrency SafetyRecovery Rate (%)
Raw vscode.Memento (untyped)12.418.2❌ Runtime❌ Last-write-wins68%
workspace.fs

πŸŽ‰ Mid-Year Sale β€” Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register β€” Start Free Trial

7-day free trial Β· Cancel anytime Β· 30-day money-back

Sources

  • β€’ Dev.to