Skip to content

Instantly share code, notes, and snippets.

View daniel-hall's full-sized avatar

Daniel Hall daniel-hall

View GitHub Profile
@daniel-hall
daniel-hall / SingleWriterPlayground.swift
Last active October 28, 2019 07:05
SingleWriter wraps any struct in a mechanism that only allows modifications through an optional-typed writer object. Only one writer object can exist at a time, so while one code site is referencing the writer or using it to change a property on the struct, any other code that attempts to get the writer to make changes will get a nil value.
// The SingleWriter generic type only works with structs
//
// SingleWriter wraps any struct in a mechanism that only allows modifications through an optional-typed writer object.
// Only one writer object can exist at a time, so while one code site is referencing the writer or using it to change
// a property on the struct, any other code that attempts to get the writer to make changes will get a nil value.
//
// This is a tool to work with global shared mutable state, that allows write access to be controlled across threads or
// call sites. Any code using the writer interface will cause any other code / threads to not have access to the writer
// interface.
//