at main 628 B view raw
1// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers 2// 3// SPDX-License-Identifier: MPL-2.0 4 5import Foundation 6 7public actor AsyncThunk<Value> { 8 private var storage: Value? 9 private var thunk: (() async -> Value)? 10 11 public init(thunk: @escaping @Sendable () async -> Value) { 12 self.thunk = thunk 13 } 14 15 public init(value: Value) { 16 self.storage = value 17 } 18 19 public func value() async -> Value { 20 if let storage { return storage } 21 if let thunk { 22 let computed = await thunk() 23 self.storage = computed 24 self.thunk = nil 25 return computed 26 } 27 fatalError("AsyncThunk has neither thunk nor storage.") 28 } 29}