We’ve spent the past few weeks working on a real-world library in order to explore the ins-and-outs of protocol-oriented programming (part 1, part 2, part 3). We saw that we can improve the designs of our APIs by scrapping our protocols and using concrete data types, a process we explored in depth in our series on protocol witnesses.
While the library we’ve built is already incredibly powerful and flexible, it has a serious limitation: it can’t snapshot values that are asynchronous. This is a pretty big limitation since many values can’t be snapshot until a callback is invoked or a delegate method is called, and in fact it completely prevents us from writing snapshot tests for web views!
This was a real problem we encountered in using this library because we take snapshots of web views to write tests for this very site, which is written in Swift and completely open source. Web views have this exact problem, where they require waiting for a delegate method to be called and a callback to be invoked to produce a snapshot image.
The solution turned out to be incredibly simple and we did it in a matter of minutes. The best part was that our refactor was completely guided by some of the functional programming ideas we’ve covered on this very site. In fact, a type we’ve discussed a number of times on this series came to the rescue.
So today we want to show why this async snapshot thing really is a problem with the current library design, and show how a to solve this in a really nice, simple way.