liveBook · Manning

Chapter 15: Isolating timelines

What you will learn in chapter 15 of Grokking Simplicity

The introduction presents timeline diagrams as a way to see actions over time and reason about bugs, especially in distributed systems.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction presents timeline diagrams as a way to see actions over time and reason about bugs, especially in distributed systems.

Key terms: timeline diagrams, sequences of actions over time, distributed system

15.1 There’s a bug!

The section introduces a shopping-cart total bug that appears when customers click quickly but not during a slow clickthrough.

Key terms: shopping cart, slow clickthrough

15.2 Now we can try to click twice fast

This section reproduces an add-to-cart bug caused by two quick clicks and explains why the code and use case diagram do not reveal what happens when operations overlap.

Key terms: buy button, add shoes twice fast, use case diagram, running at the same time

15.3 The timeline diagram shows what happens over time

This section introduces timeline diagrams as a way to see how actions from quick successive clicks can interact over time.

Key terms: timeline, timeline diagram, actions

15.4 The two fundamentals of timeline diagrams

This section gives two rules for drawing timeline diagrams: place sequential actions together and potentially concurrent or out-of-order actions apart.

Key terms: actions, calculations, timeline, in sequence, parallel, separate timelines, asynchronous callbacks, out of order

15.5 Two tricky details about the order of actions

The section explains two JavaScript execution-order details that affect which actions appear in a timeline diagram and where they appear.

Key terms: ++, +=, actions, calculation, global variable, arguments, function, order of execution, timeline diagram

15.6 Drawing the add-to-cart timeline: Step 1

This section begins the add-to-cart timeline diagram by identifying actions in the code while setting calculations aside.

Key terms: actions, calculations, asynchronous callbacks, timeline diagram

15.7 Asynchronous calls require new timelines

The section introduces a timeline diagram for asynchronous code, where callbacks can run in a different order from the order in which the code is written.

Key terms: asynchronous callbacks, timeline, actions, local vars

15.8 Different languages, different threading models

The section compares four threading models by showing how each creates timelines and constrains their ordering and shared resources.

Key terms: single-threaded, synchronous, blocks, timeline, single-threaded, asynchronous, callback, input/output, multi-threaded, interleaving, locks, message-passing processes

15.9 Building the timeline step-by-step

This section builds a timeline diagram line by line, placing ordinary actions and asynchronous callbacks on separate timelines.

Key terms: original timeline, asynchronous callback, new timeline, actions, dotted line, callback, ajax function

15.10 Drawing the add-to-cart timeline: Step 2

Step 2 of drawing the add-to-cart timeline places every identified action on a diagram in order and gives each asynchronous callback a new timeline.

Key terms: actions, timeline, ajax callbacks, asynchronous callbacks

15.11 Timeline diagrams capture the two kinds of sequential code

Explains how timeline diagrams distinguish sequential actions that may be interleaved from actions that cannot be interleaved.

Key terms: interleave, timeline diagram, actions, box

15.12 Timeline diagrams capture the uncertain ordering of parallel code

The section explains how side-by-side timeline diagrams represent uncertain ordering between parallel actions.

Key terms: parallel code, timelines, possible orderings, threading model

15.13 Principles of working with timelines

The section explains how reducing timelines, shortening them, and limiting shared resources make possible action orderings easier to reason about.

Key terms: timelines, possible orderings, shorter timelines, steps, shared resources, coordinate

15.14 JavaScript’s single-thread

Explains why JavaScript’s single main thread prevents synchronous actions from interleaving, while asynchronous calls still create timeline uncertainty.

Key terms: one main thread, synchronous actions, interleaving, asynchronous calls, runtime, synchronous or asynchronous operations

15.15 JavaScript’s asynchronous queue

This section explains how the browser’s single-threaded event loop processes jobs added to JavaScript’s job queue by events.

Key terms: job queue, event loop, single thread, job, event data, callback, events

15.16 AJAX and the event queue

This section explains how browser AJAX requests continue asynchronously and deliver responses through event callbacks.

Key terms: AJAX, _ajax suffix, AJAX request, networking engine, job queue, asynchronous, callback, load, error, event

15.17 A complete asynchronous example

Traces how a buy-button click moves through JavaScript’s event queue and an asynchronous AJAX request before the cart display is updated.

Key terms: callback, event loop, queue, AJAX request, networking engine

15.18 Simplifying the timeline

The section shows how JavaScript’s single-threaded runtime lets you simplify a completed timeline diagram.

Key terms: single thread, consolidate all actions on a single timeline, dotted lines, consolidate timelines, one new timeline

15.19 Reading our finished timeline

Shows how to read a finished timeline diagram to distinguish guaranteed from uncertain action orderings and check whether the code behaves correctly in every case.

Key terms: timeline diagrams, main timeline, callback timelines, certain, uncertain orderings, possible orderings, loading spinner, timing issues, bug

15.20 Simplifying the add-to-cart timeline diagram: Step 3

The section simplifies the add-to-cart timeline diagram using two ordered JavaScript-specific consolidations while preserving possible interleaving.

Key terms: consolidate actions, consolidate timelines, single thread, ajax call, callback, separate boxes, possible interleaving, timeline

15.21 Review: Drawing the timeline (steps 1–3)

Reviews how to draw and simplify an add-to-cart timeline diagram while preserving the timing uncertainty caused by asynchronous actions.

Key terms: actions, sequential, parallel, timeline, simplify, single-threaded model, asynchronous action, callbacks, consolidate actions, consolidate timelines

15.22 Summary: Drawing timeline diagrams

This section summarizes how to identify actions, draw and simplify timeline diagrams, and evaluate possible action orderings.

Key terms: actions, composite actions, atomic actions, sequence, parallel, timelines, dotted lines, constrained order, simplify the timeline, interleaved

15.23 Timeline diagrams side-by-side can reveal problems

Placing a timeline for each button click side by side reveals possible interleavings that a single-click diagram cannot show.

Key terms: timeline, interleave, event queue, dotted line

15.24 Two slow clicks get the right result

The section uses a timeline diagram to show that one ordering of two slow clicks produces the right result.

Key terms: timeline diagram, interleavings, two slow clicks

15.25 Two fast clicks can get the wrong result

The section shows that fast clicks can produce a wrong result when their timelines interleave, and explains why checking every possible ordering is impractical.

Key terms: click handler timelines, interleaving, possible orderings, timelines

15.26 Timelines that share resources can cause problems

The section identifies shared resources as the cause of problems when add-to-cart timelines interleave.

Key terms: timelines, sharing resources, global variables, total, cart, DOM

15.27 Converting a global variable to a local one

This section replaces a shared global total with a local variable to make more timeline orderings produce the correct result.

Key terms: global variable, local variable, total, timeline, possible orderings

15.28 Converting a global variable to an argument

The section replaces a shared cart global with an argument, explains why the remaining ordered access is safe, and identifies what still makes the function an action.

Key terms: implicit input, cart global variable, argument, timelines, second timeline, dotted line, global mutable state, cart, calc_cart_total(), calculation

15.29 Making our code more reusable

This section makes the asynchronously calculated cart total reusable by passing it to a callback instead of updating the DOM directly.

Key terms: calc_cart_total(), implicit output, callback, update_total_dom(), replace body with callback

15.30 Principle: In an asynchronous context, we use a final callback instead of a return value as our explicit output

Explains why asynchronous results cannot be returned to a caller in the usual way and how a final callback provides an explicit output while keeping actions outside the function.

Key terms: asynchronous calls, callback, return, event loop, final callback, explicit output, extract actions, synchronous functions, asynchronous functions

15.31 Conclusion

The conclusion recaps how timeline diagrams, JavaScript threading knowledge, and reduced resource sharing helped find and eliminate a bug.

Key terms: timeline diagrams, JavaScript threading model, timelines, reducing shared resources

15.32 Summary

15.33 Up next…

The section explains why the DOM remains a shared resource that two add-to-cart timelines must coordinate to use safely.

Key terms: shared resource, DOM, add-to-cart timelines, coordinating

Where this is used later

Concepts explained in this chapter

← Chapter 14: Functional tools for nested data Chapter 16: Sharing resources between timelines →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.