|
| 1 | +// |
| 2 | +// AsyncLazy.swift |
| 3 | +// https://github.com/RougeWare/Swift-Lazy-Containers |
| 4 | +// |
| 5 | +// Created by Ky on 2024-12-26. |
| 6 | +// Copyright waived. No rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +//@propertyWrapper // Property wrappers currently cannot define an 'async' or 'throws' accessor |
| 14 | +public struct AsyncLazy<Value: Sendable> { |
| 15 | + |
| 16 | + private var _guts: AsyncValueReference<AsyncValueHolder> |
| 17 | + |
| 18 | + |
| 19 | +// init(initializer: @escaping AsyncInitializer<Value>) { |
| 20 | +// self.init(_guts: .init(wrappedValue: .unset(initializer: initializer))) |
| 21 | +// } |
| 22 | + |
| 23 | + |
| 24 | + /// Allows other initializers to have a shared point of initialization |
| 25 | + private init(_guts: AsyncValueReference<AsyncValueHolder>) { |
| 26 | + self._guts = _guts |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + public nonisolated var wrappedValue: Value { |
| 31 | + get async { await _guts.wrappedValue.wrappedValue } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +extension AsyncLazy: AsyncLazyProtocol |
| 38 | +where Value: Sendable |
| 39 | +{ |
| 40 | + public mutating func mutate(_ isolatedMutator: (inout Value) async -> Void) async { |
| 41 | + await _guts.mutate { asyncLazyContainerValueHolder in |
| 42 | + await asyncLazyContainerValueHolder.mutate { value in |
| 43 | + await isolatedMutator(&value) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public func get() async -> Value { |
| 49 | + await _guts.wrappedValue.wrappedValue |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public nonisolated func set(to newValue: sending Value) async { |
| 54 | + await _guts.set(to: .hasValue(value: newValue)) |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public static func preinitialized(_ initialValue: Value) -> Self { |
| 59 | + Self.init(_guts: .init(wrappedValue: .hasValue(value: initialValue))) |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public var isInitialized: Bool { |
| 64 | + get async { await _guts.wrappedValue.hasValue } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public func initializeNow() async { |
| 69 | + await _guts.mutate { wrappedValue in |
| 70 | + await wrappedValue.initializeNow() |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +// MARK: - ValueHolder |
| 78 | + |
| 79 | +/// Takes care of keeping track of the state, value, and initializer of a lazy container, as needed |
| 80 | +//@propertyWrapper // Property wrappers currently cannot define an 'async' or 'throws' accessor |
| 81 | +public enum AsyncLazyContainerValueHolder<Value: Sendable>: Sendable { |
| 82 | + |
| 83 | + /// Indicates that a value has been cached, and contains that cached value |
| 84 | + case hasValue(value: Value) |
| 85 | + |
| 86 | + /// Indicates that the value has not yet been created, and contains its initializer |
| 87 | + case unset(initializer: AsyncInitializer<Value>) |
| 88 | + |
| 89 | + |
| 90 | + /// The value held inside this value holder. |
| 91 | + /// - Attention: Reading this value may mutate the state in order to compute the value. The complexity of that read |
| 92 | + /// operation is equal to the complexity of the initializer. |
| 93 | + public var wrappedValue: Value { |
| 94 | + mutating get async { |
| 95 | + switch self { |
| 96 | + case .hasValue(let value): |
| 97 | + return value |
| 98 | + |
| 99 | + case .unset(let initializer): |
| 100 | + let value = await initializer() |
| 101 | + self = .hasValue(value: value) |
| 102 | + return value |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /// Sets the value asynchronously |
| 109 | + public mutating func set(to newValue: Value) async { |
| 110 | + self = .hasValue(value: newValue) |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + /// Indicates whether this holder actually holds a value. |
| 115 | + /// This will be `true` after reading or writing `wrappedValue`. |
| 116 | + public var hasValue: Bool { |
| 117 | + switch self { |
| 118 | + case .hasValue(value: _): return true |
| 119 | + case .unset(initializer: _): return false |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /// Immediately initializes the value held inside this value holder |
| 125 | + /// |
| 126 | + /// If this holder already contains a value, this does nothing |
| 127 | + mutating func initializeNow() async { |
| 128 | + switch self { |
| 129 | + case .hasValue(_): return |
| 130 | + case .unset(let initializer): |
| 131 | + self = await .hasValue(value: initializer()) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + mutating func mutate(_ isolatedMutator: (inout Value) async -> Void) async { |
| 137 | + var wrappedValue = await self.wrappedValue |
| 138 | + await isolatedMutator(&wrappedValue) |
| 139 | + await self.set(to: wrappedValue) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +public extension AnyLazy { |
| 147 | + |
| 148 | + /// Takes care of keeping track of the state, value, and initializer as needed |
| 149 | + typealias AsyncValueHolder = AsyncLazyContainerValueHolder<Value> |
| 150 | +} |
| 151 | + |
0 commit comments