[
    Heading(
        2,
        Inline {
            text: "What's happening",
            spans: [],
            links: [],
        },
    ),
    Para(
        Inline {
            text: "The server sends a heartbeat every 10 seconds, but \u{202f}\u{202f}\u{2066}stream.ts\u{2069}\u{202f}\u{202f} only reconnects on an \u{202f}\u{202f}\u{2066}error\u{2069}\u{202f}\u{202f} event. When the machine sleeps, the TCP connection goes half-open: the client never sees an error, so it waits forever. See the retry notes for the long version, which is deliberately wordy so that this paragraph has to wrap onto several lines at every width.",
            spans: [
                (
                    35,
                    45,
                    Strong,
                ),
                (
                    51,
                    60,
                    CodePad,
                ),
                (
                    60,
                    69,
                    Code,
                ),
                (
                    69,
                    78,
                    CodePad,
                ),
                (
                    101,
                    110,
                    CodePad,
                ),
                (
                    110,
                    115,
                    Code,
                ),
                (
                    115,
                    124,
                    CodePad,
                ),
                (
                    196,
                    202,
                    Em,
                ),
                (
                    249,
                    264,
                    Link,
                ),
            ],
            links: [
                (
                    249,
                    264,
                    "https://example.com/retry",
                ),
            ],
        },
    ),
    Para(
        Inline {
            text: "Two smaller issues make it worse:",
            spans: [],
            links: [],
        },
    ),
    List {
        start: None,
        items: [
            [
                Para(
                    Inline {
                        text: "\u{202f}\u{202f}\u{2066}backoff.ts\u{2069}\u{202f}\u{202f} resets its delay on every message, so a flapping server is retried at full speed.",
                        spans: [
                            (
                                0,
                                9,
                                CodePad,
                            ),
                            (
                                9,
                                19,
                                Code,
                            ),
                            (
                                19,
                                28,
                                CodePad,
                            ),
                            (
                                49,
                                54,
                                Em,
                            ),
                        ],
                        links: [],
                    },
                ),
            ],
            [
                Para(
                    Inline {
                        text: "The sidebar store ignores \u{202f}\u{202f}\u{2066}session.updated\u{2069}\u{202f}\u{202f} events that arrive before the first list fetch.",
                        spans: [
                            (
                                26,
                                35,
                                CodePad,
                            ),
                            (
                                35,
                                50,
                                Code,
                            ),
                            (
                                50,
                                59,
                                CodePad,
                            ),
                        ],
                        links: [],
                    },
                ),
                List {
                    start: None,
                    items: [
                        [
                            Para(
                                Inline {
                                    text: "A nested point with \u{202f}\u{202f}\u{2066}code\u{2069}\u{202f}\u{202f} in it.",
                                    spans: [
                                        (
                                            20,
                                            29,
                                            CodePad,
                                        ),
                                        (
                                            29,
                                            33,
                                            Code,
                                        ),
                                        (
                                            33,
                                            42,
                                            CodePad,
                                        ),
                                    ],
                                    links: [],
                                },
                            ),
                        ],
                    ],
                },
            ],
        ],
    },
    Heading(
        3,
        Inline {
            text: "The fix",
            spans: [],
            links: [],
        },
    ),
    Para(
        Inline {
            text: "Treat silence as failure. If no event or heartbeat arrives for 25 seconds, close the stream and reconnect with backoff:",
            spans: [],
            links: [],
        },
    ),
    Code(
        "const QUIET_MS = 25_000\n\nfunction watchdog(stream: EventStream, reconnect: () => void) {\n  let timer = setTimeout(fire, QUIET_MS) // restarted on every message\n  function fire() {\n    stream.close()\n    reconnect()\n  }\n  stream.on(\"message\", () => {\n    clearTimeout(timer)\n    timer = setTimeout(fire, QUIET_MS)\n  })\n}",
    ),
    Code(
        "plain text fence, no language\n  keeps its   spacing",
    ),
    Quote(
        [
            Para(
                Inline {
                    text: "Quoted advice: the reset must wait for a healthy minute. It spans two lines.",
                    spans: [],
                    links: [],
                },
            ),
        ],
    ),
    List {
        start: Some(
            1,
        ),
        items: [
            [
                Para(
                    Inline {
                        text: "Watchdog: closes and reopens the stream after 25 s of silence.",
                        spans: [
                            (
                                0,
                                8,
                                Strong,
                            ),
                        ],
                        links: [],
                    },
                ),
            ],
            [
                Para(
                    Inline {
                        text: "Backoff: the delay only resets after a minute of healthy traffic.",
                        spans: [
                            (
                                0,
                                7,
                                Strong,
                            ),
                        ],
                        links: [],
                    },
                ),
            ],
            [
                Para(
                    Inline {
                        text: "Sidebar: early events are queued until the list loads.",
                        spans: [
                            (
                                0,
                                7,
                                Strong,
                            ),
                        ],
                        links: [],
                    },
                ),
            ],
        ],
    },
    List {
        start: None,
        items: [
            [
                Para(
                    Inline {
                        text: "watchdog added",
                        spans: [],
                        links: [],
                    },
                ),
            ],
            [
                Para(
                    Inline {
                        text: "reconnect test",
                        spans: [],
                        links: [],
                    },
                ),
            ],
            [
                Para(
                    Inline {
                        text: "docs",
                        spans: [],
                        links: [],
                    },
                ),
            ],
        ],
    },
    Table(
        Table {
            align: [
                Start,
                End,
                Center,
            ],
            head: [
                Inline {
                    text: "Case",
                    spans: [],
                    links: [],
                },
                Inline {
                    text: "Delay",
                    spans: [],
                    links: [],
                },
                Inline {
                    text: "Resets",
                    spans: [],
                    links: [],
                },
            ],
            rows: [
                [
                    Inline {
                        text: "flapping",
                        spans: [],
                        links: [],
                    },
                    Inline {
                        text: "1s, 2s, 4s",
                        spans: [],
                        links: [],
                    },
                    Inline {
                        text: "no",
                        spans: [],
                        links: [],
                    },
                ],
                [
                    Inline {
                        text: "healthy minute",
                        spans: [],
                        links: [],
                    },
                    Inline {
                        text: "1s",
                        spans: [],
                        links: [],
                    },
                    Inline {
                        text: "yes",
                        spans: [],
                        links: [],
                    },
                ],
                [
                    Inline {
                        text: "capped",
                        spans: [],
                        links: [],
                    },
                    Inline {
                        text: "30s",
                        spans: [],
                        links: [],
                    },
                    Inline {
                        text: "no",
                        spans: [],
                        links: [],
                    },
                ],
            ],
            open: false,
        },
    ),
    Rule,
    Mermaid(
        "graph TD\n  A[Event arrives] --> B{Quiet for 25 s?}\n  B -- yes --> C[Close stream]\n  C --> D[Reconnect with backoff]\n  B -- no --> A",
    ),
    Mermaid(
        "sequenceDiagram\n  participant C as Client\n  participant S as Server\n  C->>S: open stream\n  S-->>C: heartbeat\n  Note over C: 25 s of silence\n  C->>S: reconnect",
    ),
    Mermaid(
        "graph LR\n  this is not valid",
    ),
    Para(
        Inline {
            text: "Done. All 14 tests in \u{202f}\u{202f}\u{2066}src/net\u{2069}\u{202f}\u{202f} pass.",
            spans: [
                (
                    22,
                    31,
                    CodePad,
                ),
                (
                    31,
                    38,
                    Code,
                ),
                (
                    38,
                    47,
                    CodePad,
                ),
            ],
            links: [],
        },
    ),
]
-- highlights (ts)
0..6 "const " Kind(Keyword)
6..15 "QUIET_MS " Kind(Number)
15..17 "= " Kind(Operator)
17..23 "25_000" Kind(Number)
25..34 "function " Kind(Keyword)
34..42 "watchdog" Kind(Function)
42..43 "(" Kind(Punctuation)
43..49 "stream" Kind(Parameter)
49..51 ": " Kind(Keyword)
51..62 "EventStream" Kind(Type)
62..64 ", " Kind(Punctuation)
64..73 "reconnect" Kind(Parameter)
73..78 ": () " Kind(Punctuation)
78..81 "=> " Kind(Operator)
81..85 "void" Kind(Keyword)
85..88 ") {" Kind(Punctuation)
89..91 "  " Kind(Punctuation)
91..95 "let " Kind(Keyword)
95..101 "timer " Kind(Variable)
101..103 "= " Kind(Operator)
103..113 "setTimeout" Kind(Function)
113..114 "(" Kind(Punctuation)
114..118 "fire" Kind(Variable)
118..120 ", " Kind(Punctuation)
120..128 "QUIET_MS" Kind(Number)
128..130 ") " Kind(Punctuation)
130..159 "// restarted on every message" Kind(Comment)
160..162 "  " Kind(Comment)
162..171 "function " Kind(Keyword)
171..175 "fire" Kind(Function)
175..179 "() {" Kind(Punctuation)
180..184 "    " Kind(Punctuation)
184..190 "stream" Kind(Variable)
190..191 "." Kind(Punctuation)
191..196 "close" Kind(Function)
196..198 "()" Kind(Punctuation)
199..203 "    " Kind(Punctuation)
203..212 "reconnect" Kind(Function)
212..214 "()" Kind(Punctuation)
215..218 "  }" Kind(Punctuation)
219..221 "  " Kind(Punctuation)
221..227 "stream" Kind(Variable)
227..228 "." Kind(Punctuation)
228..230 "on" Kind(Function)
230..231 "(" Kind(Punctuation)
231..240 "\"message\"" Kind(String)
240..245 ", () " Kind(Punctuation)
245..248 "=> " Kind(Operator)
248..249 "{" Kind(Punctuation)
250..254 "    " Kind(Punctuation)
254..266 "clearTimeout" Kind(Function)
266..267 "(" Kind(Punctuation)
267..272 "timer" Kind(Variable)
272..273 ")" Kind(Punctuation)
274..278 "    " Kind(Punctuation)
278..284 "timer " Kind(Variable)
284..286 "= " Kind(Operator)
286..296 "setTimeout" Kind(Function)
296..297 "(" Kind(Punctuation)
297..301 "fire" Kind(Variable)
301..303 ", " Kind(Punctuation)
303..311 "QUIET_MS" Kind(Number)
311..312 ")" Kind(Punctuation)
313..317 "  })" Kind(Punctuation)
318..319 "}" Kind(Punctuation)
