| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Defines or updates a global variable accessible across Kestrun scripts. |
| | | 4 | | |
| | | 5 | | .DESCRIPTION |
| | | 6 | | Stores a value in the Kestrun global variable table. Variables may be marked |
| | | 7 | | as read-only to prevent accidental modification. |
| | | 8 | | If the variable already exists, its value is updated. If it does not exist, |
| | | 9 | | it is created. |
| | | 10 | | .PARAMETER Server |
| | | 11 | | The Kestrun server instance. |
| | | 12 | | .PARAMETER Global |
| | | 13 | | If specified, the variable is stored in the global shared state. |
| | | 14 | | .PARAMETER Name |
| | | 15 | | Name of the variable to create or update. |
| | | 16 | | .PARAMETER Value |
| | | 17 | | Value to assign to the variable. |
| | | 18 | | .PARAMETER AllowsValueType |
| | | 19 | | If specified, allows the variable to hold value types (e.g., int, bool). |
| | | 20 | | .PARAMETER ThreadSafe |
| | | 21 | | If specified, the value is converted to a thread-safe representation. |
| | | 22 | | .EXAMPLE |
| | | 23 | | Set-KrSharedState -Name "MyVariable" -Value "Hello, World!" |
| | | 24 | | This creates a global variable "MyVariable" with the value "Hello, World!". |
| | | 25 | | .EXAMPLE |
| | | 26 | | Set-KrSharedState -Name "MyNamespace.MyVariable" -Value @{item=42} |
| | | 27 | | This creates a global variable "MyNamespace.MyVariable" with the value @{item=42}. |
| | | 28 | | .NOTES |
| | | 29 | | This function is part of the Kestrun.SharedState module and is used to define or update global variables. |
| | | 30 | | #> |
| | | 31 | | function Set-KrSharedState { |
| | | 32 | | [KestrunRuntimeApi('Definition')] |
| | | 33 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | | 34 | | [CmdletBinding(defaultParameterSetName = 'Server')] |
| | | 35 | | param( |
| | | 36 | | [Parameter(ValueFromPipeline = $true, ParameterSetName = 'Server')] |
| | | 37 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 38 | | |
| | | 39 | | [Parameter(Mandatory = $true, ParameterSetName = 'Global')] |
| | | 40 | | [switch]$Global, |
| | | 41 | | |
| | | 42 | | [Parameter(Mandatory)] |
| | | 43 | | [string]$Name, |
| | | 44 | | |
| | | 45 | | [Parameter(Mandatory)] |
| | | 46 | | [object]$Value, |
| | | 47 | | |
| | | 48 | | [Parameter()] |
| | | 49 | | [switch]$AllowsValueType, |
| | | 50 | | |
| | | 51 | | [Parameter()] |
| | | 52 | | [switch]$ThreadSafe |
| | | 53 | | |
| | | 54 | | ) |
| | | 55 | | begin { |
| | 1 | 56 | | if (-not $Global.IsPresent) { |
| | | 57 | | # Ensure the server instance is resolved |
| | 0 | 58 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | process { |
| | 1 | 62 | | if ($ThreadSafe.IsPresent) { |
| | 0 | 63 | | $Value = ConvertTo-KrThreadSafeValue -Value $Value |
| | | 64 | | } |
| | 1 | 65 | | if ($Global.IsPresent) { |
| | | 66 | | # Retrieve from server instance |
| | 1 | 67 | | return [Kestrun.SharedState.GlobalStore]::Set($Name, |
| | | 68 | | $Value, |
| | | 69 | | $AllowsValueType.IsPresent # Allow value types if specified |
| | | 70 | | ) |
| | | 71 | | } |
| | | 72 | | # Define or update the variable; throws if it was already read-only |
| | 0 | 73 | | $null = $Server.SharedState.Set( |
| | | 74 | | $Name, |
| | | 75 | | $Value, |
| | | 76 | | $AllowsValueType.IsPresent # Allow value types if specified |
| | | 77 | | ) |
| | | 78 | | } |
| | | 79 | | } |