Thread-safe access to a lazily retrieved value, with optional validity checking
Swift’s Structured Concurrency provides many primitives, but lacks higher-level mechanics such as serial queues. This package is an investigation into how to reimplement that.
A recent Twitter thread inspired the investigation: https://twitter.com/layoutSubviews/status/1486925222137954304
Swift Twitter, help me, you’re my only hope:
With Swift Concurrency, is there an equivalent to a dispatch serial queue,
i.e. something on which
@Sendable () async -> Voidoperations can be enqueued, and which executes them serially?
Locally cache an auth token, providing concurrent access, lazy fetching, and re-fetching if no longer valid.
- Using an
- Generic over a
- Has a single getter method/property to get an up to date
- The value is fetched by a caller-defined async function
- Lazily fetch the first value upon first request
- While fetching, all subsequent requests must also await the fetch value
- Once fetched, cache it in-memory for faster performance in subsequent requests
- Include a way to invalidate the locally cached value, requiring the value to be re-fetched if invalid
- No concurrency warnings!
More discussion about this on the Swift Forums: https://forums.swift.org/t/rfc-using-swift-concurrency-as-a-serial-blocking-queue/55135