The Real Fullstack

The State Synchronization Bug

Three components show the same cart. Which one is telling the truth?

エンジニア8 分フローシミュレーターコード課題
angularsignalsbrowser-apishttpvue

この課題はまだ日本語に翻訳されていません。英語版を表示しています。

ミッション:Make every view agree in the same tab, across tabs, and with the server's truth at checkout.背景 · 学習目標 ▾

Larkspur's storefront shows the cart in a header badge, on the cart page and in the checkout summary. Each component fetched and stored its own copy. Customers with two tabs open, or who wait while a promotion ends, see conflicting numbers and occasionally pay a different total than displayed.

ミッション: Make every view agree in the same tab, across tabs, and with the server's truth at checkout.

学習目標

  • Identify duplicated client state as a source of inconsistency.
  • Use a single source of truth (signal store) in the app.
  • Synchronise tabs and validate against the server at critical moments.
シミュレーションシミュレーション値
same tab
1cart page adds item → header badge still shows its own copy
second tab
2item removed in tab B; tab A still shows it
server
3price changed on the server; checkout submits the old total
シミュレーションを実行
@Component({ selector: 'app-cart-badge', template: '{{ count }}' })export class CartBadge implements OnInit {  count = 0;                                    // its own copy  ngOnInit() { this.http.get<Cart>('/cart').subscribe((c) => (this.count = c.items.length)); }}@Component({ selector: 'app-cart-page' })export class CartPage {  items: Item[] = [];                           // another copy, updated only here}

簡略化

Three scenarios with deterministic outcomes. Real apps also deal with offline mode and optimistic updates. 教育用モデルです。数値は挙動を示すためのもので、ベンチマークではありません。

コンセプト

関連する課題

参考資料