3 min read
I pitched teaching debounce to stop waiting

One of the things I try to avoid the most is losing my users’ data. So every time something changes in one of my apps, a little voice says save it now, save it now, save it now.
The problem is that the little voice would write to disk about a hundred times a second if I let it. So you reach for debounce: wait for things to calm down, then save once. Sensible. Grown up.
Except debounce works like an elevator whose door keeps reopening because someone else is running up to it. As long as people keep arriving, it never leaves. If changes never really stop, nothing gets saved, and then the app crashes and takes everything with it. That’s the exact thing I was trying to avoid. Great.
Three tries
The real case is the link preview cache in my (hopefully) upcoming Wishlists app. Every link that loads changes the cache, and I want those saved.1
- Try one: batch on a timer. It worked, but it kept ticking forever, even when nothing was happening.
- Try two:
throttleanddebouncetogether, merged into one stream. It worked too, but it took two streams, a merge, and extra tests just to keep them from stepping on each other. - Try three: the thing I actually wanted all along, as a single operator.
Watching it happen
I’m very familiar with debounce, but throttle never really clicked for me. I learn best with visuals, so I built a little test app to actually see it. You tap a button and each operator reacts to the same taps on its own timeline. Filled dots got through, hollow dots were ignored.

This is where I went “huh.” Throttle fires right away, but it never goes back for the taps it skipped, while debounce just waits and emits once at the very end. I wanted a bit of both: don’t fire on the first tap, flush every so often while taps keep coming, and flush once more when they stop.
That’s the fourth lane in the first screenshot. It waits for quiet like debounce, but it won’t hold a value longer than a maximum wait. The elevator leaves anyway.
The pitch
lodash has had this for years and calls it maxWait. swift-async-algorithms doesn’t have it, so I added it:
for await _ in saves.debounce(for: .seconds(2), maxWait: .seconds(10)) {
try write(latest)
}
The latest value comes out after the quiet period, or maxWait after the first value that hasn’t come out yet, whichever happens first. The existing debounce doesn’t change at all.
- Issue: apple/swift-async-algorithms#488
- Pull request: apple/swift-async-algorithms#489
- Proposal:
Evolution/0019-debounce-max-wait.md - Pitch on the Swift Forums: Pitch: swift-async-algorithms debounce(for:maxWait:)
I’m not the first to run into this. People have hit debounce never yielding and throttle not emitting the latest element before.
Now we wait
So I made a pitch for swift-async-algorithms, and I posted it to the Swift Forums too. It’ll be fun to see where it goes.
In the meantime, Wishlists already runs on my fork, so it’s getting tested for real. If it gets reviewed and merged, everybody’s apps get a little safer. And the elevator finally leaves.
Footnotes
-
Yes, it’s a cache, and losing it wouldn’t be a huge deal. The previews can be fetched again. But I like building components and tech that I can reuse over and over, so from the start I wanted a solid solution and not a throwaway one. ↩