Patterns and streams

This part collects patterns over the language: iterators and generators, mixins, branded nominal types, result types, the options object, a builder and an event emitter. The part ends with streams, workers, and the deserialization that turns unknown JSON into a trusted record.

12.1 Iterators and generators the iterator protocol, a generator function, and a pipeline of yield steps Open 12.2 Mixins shared behaviour as small mixin functions over a class, explicit and composable Open 12.3 Branding nominal types a branded string that the compiler keeps apart from a plain one Open 12.4 Result types an ok and an error shape, both branches handled, chained without nesting Open 12.5 Options objects one options object with defaults in place of a long argument list Open 12.6 Builder small setters chained into one build call that validates at the end Open 12.7 Event emitters a listener list, one emit to all of them, and the removal when done Open 12.8 Streams a big file in small chunks, a pipe, a transform, and backpressure Open 12.9 Workers heavy work off the main thread, and messages between the worker and it Open 12.10 Deserialization unknown JSON into a trusted record, with small guards for fields and arrays Open

Lessons