|
| 1 | +/* |
| 2 | + * Copyright Square Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/// System for managing configuration options for Workflow runtime behaviors. |
| 18 | +/// - important: These interfaces are subject to breaking changes without corresponding semantic |
| 19 | +/// versioning changes. |
| 20 | +@_spi(RuntimeConfig) |
| 21 | +public enum Runtime { |
| 22 | + @TaskLocal |
| 23 | + static var _currentConfiguration: Configuration? |
| 24 | + |
| 25 | + static var _bootstrapConfiguration = BootstrappableConfiguration() |
| 26 | + |
| 27 | + /// Bootstrap the workflow runtime with the given configuration. |
| 28 | + /// This can only be called once per process and must be called from the main thread. |
| 29 | + /// |
| 30 | + /// - Parameter configuration: The runtime configuration to use. |
| 31 | + @MainActor |
| 32 | + public static func bootstrap( |
| 33 | + _ configureBlock: (inout Configuration) -> Void |
| 34 | + ) { |
| 35 | + MainActor.preconditionIsolated( |
| 36 | + "The Workflow runtime must be bootstrapped from the main actor." |
| 37 | + ) |
| 38 | + guard !_isBootstrapped else { |
| 39 | + fatalError("The Workflow runtime can only be bootstrapped once.") |
| 40 | + } |
| 41 | + |
| 42 | + var config = _bootstrapConfiguration.currentConfiguration |
| 43 | + defer { _bootstrapConfiguration._bootstrapConfig = config } |
| 44 | + configureBlock(&config) |
| 45 | + } |
| 46 | + |
| 47 | + static var configuration: Configuration { |
| 48 | + _currentConfiguration ?? _bootstrapConfiguration.currentConfiguration |
| 49 | + } |
| 50 | + |
| 51 | + /// Executes the given closure with the current runtime configuration applied as a task-local. |
| 52 | + /// |
| 53 | + /// - Parameters: |
| 54 | + /// - operation: The closure to execute with the current runtime configuration |
| 55 | + /// - Returns: The result of executing the closure |
| 56 | + static func withCurrentConfiguration<T>( |
| 57 | + operation: (Configuration) -> T |
| 58 | + ) -> T { |
| 59 | + Runtime |
| 60 | + .$_currentConfiguration |
| 61 | + .withValue(configuration) { |
| 62 | + operation(Runtime._currentConfiguration!) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // MARK: - |
| 67 | + |
| 68 | + private static var _isBootstrapped: Bool { |
| 69 | + _bootstrapConfiguration._bootstrapConfig != nil |
| 70 | + } |
| 71 | + |
| 72 | + /// The current runtime configuration that may have been set via `bootstrap()`. |
| 73 | + private static var _currentBootstrapConfiguration: Configuration { |
| 74 | + _bootstrapConfiguration.currentConfiguration |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +extension Runtime { |
| 79 | + /// Configuration options for the Workflow runtime. |
| 80 | + public struct Configuration: Equatable { |
| 81 | + /// The default runtime configuration. |
| 82 | + static let `default` = Configuration() |
| 83 | + |
| 84 | + /// Note: this doesn't control anything yet, but is here as a placeholder |
| 85 | + public var renderOnlyIfStateChanged: Bool = false |
| 86 | + } |
| 87 | + |
| 88 | + struct BootstrappableConfiguration { |
| 89 | + var _bootstrapConfig: Configuration? |
| 90 | + let _defaultConfig: Configuration = .default |
| 91 | + |
| 92 | + /// The current runtime configuration that may have been set via `Runtime.bootstrap()`. |
| 93 | + var currentConfiguration: Configuration { |
| 94 | + _bootstrapConfig ?? _defaultConfig |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments