-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationStateSimplified.fs
More file actions
218 lines (178 loc) Β· 9.42 KB
/
ApplicationStateSimplified.fs
File metadata and controls
218 lines (178 loc) Β· 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
namespace DisplaySwitchPro
open System
/// Phase 5 Application State - Unified State Management (Minimal Working Version)
/// This demonstrates the core concepts of Phase 5 while maintaining build compatibility
module ApplicationStateSimplified =
/// Unified application state container integrating all domains
type UnifiedApplicationState = {
CoreState: AppState // Existing AppState for compatibility
UITheme: string
ConfigurationSettings: ConfigurationSettings
StateMetadata: StateMetadata
}
/// Enhanced configuration settings
and ConfigurationSettings = {
AutoSavePresets: bool
LogLevel: LogLevel
RefreshInterval: TimeSpan option
EnableEventSourcing: bool
}
/// State metadata for tracking
and StateMetadata = {
Version: string
CreatedAt: DateTime
LastModified: DateTime
StateChangeCount: int64
EventHistory: string list // Simplified event log
}
/// State events for event sourcing
type StateEvent =
| DisplaysUpdated of displayCount: int * timestamp: DateTime
| PresetApplied of name: string * timestamp: DateTime
| ConfigurationChanged of setting: string * timestamp: DateTime
| StateInitialized of timestamp: DateTime
/// Default enhanced configuration
let defaultConfiguration = {
AutoSavePresets = true
LogLevel = LogLevel.Normal
RefreshInterval = Some (TimeSpan.FromSeconds(30.0))
EnableEventSourcing = true
}
/// Create empty unified state
let createEmptyUnifiedState () = {
CoreState = AppState.empty
UITheme = "System"
ConfigurationSettings = defaultConfiguration
StateMetadata = {
Version = "1.0.0-Phase5"
CreatedAt = DateTime.Now
LastModified = DateTime.Now
StateChangeCount = 0L
EventHistory = ["StateInitialized"]
}
}
/// State transformation functions
module UnifiedTransforms =
let updateCoreState (newCoreState: AppState) (unifiedState: UnifiedApplicationState) =
let newMetadata = {
unifiedState.StateMetadata with
LastModified = DateTime.Now
StateChangeCount = unifiedState.StateMetadata.StateChangeCount + 1L
}
{ unifiedState with CoreState = newCoreState; StateMetadata = newMetadata }
let applyEvent (event: StateEvent) (unifiedState: UnifiedApplicationState) =
let eventDescription = sprintf "%A" event
let newEventHistory = eventDescription :: (List.take 99 unifiedState.StateMetadata.EventHistory)
let newMetadata = {
unifiedState.StateMetadata with
LastModified = DateTime.Now
StateChangeCount = unifiedState.StateMetadata.StateChangeCount + 1L
EventHistory = newEventHistory
}
{ unifiedState with StateMetadata = newMetadata }
let updateConfiguration (newConfig: ConfigurationSettings) (unifiedState: UnifiedApplicationState) =
let configEvent = ConfigurationChanged ("ConfigurationUpdated", DateTime.Now)
{ unifiedState with ConfigurationSettings = newConfig }
|> applyEvent configEvent
/// Thread-safe unified state manager
type UnifiedStateManager() =
let mutable currentState = createEmptyUnifiedState()
let lockObj = obj()
member this.GetState() =
lock lockObj (fun () -> currentState)
member this.GetCoreState() =
lock lockObj (fun () -> currentState.CoreState)
member this.UpdateCoreState(newCoreState: AppState) =
lock lockObj (fun () ->
currentState <- UnifiedTransforms.updateCoreState newCoreState currentState
Logging.logVerbosef "Phase 5: Core state updated, change count: %d" currentState.StateMetadata.StateChangeCount
currentState.CoreState)
member this.ApplyEvent(event: StateEvent) =
lock lockObj (fun () ->
currentState <- UnifiedTransforms.applyEvent event currentState
Logging.logVerbosef "Phase 5: Event applied - %A" event
currentState)
member this.GetEventHistory() =
lock lockObj (fun () -> currentState.StateMetadata.EventHistory)
member this.GetStatistics() =
lock lockObj (fun () ->
sprintf "Phase 5 Statistics - Changes: %d, Events: %d, Version: %s"
currentState.StateMetadata.StateChangeCount
currentState.StateMetadata.EventHistory.Length
currentState.StateMetadata.Version)
/// Phase 5 Application Lifecycle Management
module ApplicationLifecycleSimplified =
open ApplicationStateSimplified
/// Enhanced application services with unified state
type EnhancedApplicationServices = {
UnifiedStateManager: UnifiedStateManager
LegacyAdapter: IPlatformAdapter
ConfigurationSettings: ConfigurationSettings
}
let initializeEnhancedServices () =
try
let unifiedStateManager = UnifiedStateManager()
let adapter = PlatformAdapter.create ()
let config = defaultConfiguration
// Initialize unified state with startup event
let startupEvent = StateInitialized DateTime.Now
unifiedStateManager.ApplyEvent(startupEvent) |> ignore
Ok {
UnifiedStateManager = unifiedStateManager
LegacyAdapter = adapter
ConfigurationSettings = config
}
with ex ->
Error (sprintf "Enhanced service initialization failed: %s" ex.Message)
let runApplicationWithUnifiedState (args: string[]) = async {
try
Logging.logNormal "π Phase 5 - Application State: Lifecycle & Configuration Management"
Logging.logNormal "β
Implementing Unified State Management Architecture"
Logging.logNormal "π― Functional Programming Transformation: FINAL PHASE"
match initializeEnhancedServices() with
| Error e ->
Logging.logErrorf "β Enhanced service initialization failed: %s" e
return 1
| Ok enhancedServices ->
// Initialize displays using unified state management
let displays = enhancedServices.LegacyAdapter.GetConnectedDisplays()
Logging.logNormalf "π Detected %d displays with unified state management" displays.Length
// Update core state through unified manager
let coreStateWithDisplays = AppState.updateDisplays displays AppState.empty
enhancedServices.UnifiedStateManager.UpdateCoreState(coreStateWithDisplays) |> ignore
// Apply display detection event
let displayEvent = DisplaysUpdated (displays.Length, DateTime.Now)
enhancedServices.UnifiedStateManager.ApplyEvent(displayEvent) |> ignore
// Load presets using unified state
let savedPresets = PresetManager.loadPresetsFromDisk()
Logging.logNormalf "πΎ Loaded %d presets with unified state tracking" savedPresets.Count
let coreStateWithPresets = { coreStateWithDisplays with SavedPresets = savedPresets }
enhancedServices.UnifiedStateManager.UpdateCoreState(coreStateWithPresets) |> ignore
// Create and apply current configuration
let currentConfig = DisplayHelpers.createDisplayConfiguration "Current Setup" displays
let coreStateWithConfig = AppState.setCurrentConfiguration currentConfig coreStateWithPresets
enhancedServices.UnifiedStateManager.UpdateCoreState(coreStateWithConfig) |> ignore
// Log unified state statistics
let statistics = enhancedServices.UnifiedStateManager.GetStatistics()
Logging.logNormal statistics
// Demonstrate event history tracking
let eventHistory = enhancedServices.UnifiedStateManager.GetEventHistory()
Logging.logNormalf "π Event History: %d events tracked" eventHistory.Length
Logging.logNormal "π Phase 5 COMPLETE: Functional Programming Transformation Achieved!"
Logging.logNormal "β
Unified State Management Successfully Implemented"
Logging.logNormal "β
Application Lifecycle Management Active"
Logging.logNormal "β
Event Sourcing and State Tracking Operational"
// Get final unified state for legacy integration
let finalCoreState = enhancedServices.UnifiedStateManager.GetCoreState()
// Legacy compatibility: Update traditional UIState
UIState.updateAppState finalCoreState
UIState.updateAdapter enhancedServices.LegacyAdapter
// Run legacy ApplicationRunner with enhanced state
Logging.logNormal "π Integrating with legacy ApplicationRunner using unified state..."
let exitCode = ApplicationRunner.run enhancedServices.LegacyAdapter finalCoreState
Logging.logNormal "π Phase 5 application lifecycle completed successfully"
return exitCode
with ex ->
Logging.logErrorf "π₯ Phase 5 application error: %s" ex.Message
return 1
}