forked from Haleth/Aurora
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathintegration.lua
More file actions
510 lines (409 loc) · 14.6 KB
/
integration.lua
File metadata and controls
510 lines (409 loc) · 14.6 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
local _, private = ...
-- [[ Lua Globals ]]
-- luacheck: globals pairs type pcall xpcall
-- System Integration Module
-- Handles module coordination, error handling, recovery mechanisms, and performance optimization
local Integration = {}
private.Integration = Integration
-- Integration state
Integration.initialized = false
Integration.modules = {}
Integration.eventHandlers = {}
Integration.errorLog = {}
Integration.performanceMetrics = {}
-- Error handling and recovery
--[[ Integration.RegisterErrorHandler(_moduleName, handler_)
Register an error handler for a specific module.
**Args:**
* `moduleName` - the module name _(string)_
* `handler` - error handler function _(function)_
--]]
function Integration.RegisterErrorHandler(moduleName, handler)
if not moduleName or type(handler) ~= "function" then
return false
end
Integration.errorHandlers = Integration.errorHandlers or {}
Integration.errorHandlers[moduleName] = handler
private.debug("Integration", "Registered error handler for", moduleName)
return true
end
--[[ Integration.HandleError(_moduleName, error, context_)
Handle an error from a module with recovery attempt.
**Args:**
* `moduleName` - the module where error occurred _(string)_
* `error` - the error message or object _(any)_
* `context` - additional context information _(table)_
**Returns:**
* `recovered` - whether error was recovered _(boolean)_
--]]
function Integration.HandleError(moduleName, error, context)
-- Log the error
local errorEntry = {
module = moduleName,
error = error,
context = context or {},
timestamp = _G.time(),
}
Integration.errorLog[#Integration.errorLog + 1] = errorEntry
private.debug("Integration", "Error in", moduleName, ":", error)
-- Try module-specific error handler
if Integration.errorHandlers and Integration.errorHandlers[moduleName] then
local success, recovered = pcall(Integration.errorHandlers[moduleName], error, context)
if success and recovered then
private.debug("Integration", "Error recovered by module handler")
return true
end
end
-- Generic recovery attempts
if context and context.recoverable then
private.debug("Integration", "Attempting generic recovery")
return Integration.AttemptRecovery(moduleName, error, context)
end
return false
end
--[[ Integration.AttemptRecovery(_moduleName, error, context_)
Attempt generic error recovery.
**Args:**
* `moduleName` - the module name _(string)_
* `error` - the error _(any)_
* `context` - error context _(table)_
**Returns:**
* `recovered` - whether recovery succeeded _(boolean)_
--]]
function Integration.AttemptRecovery(moduleName, error, context)
-- Attempt to reload module configuration
if moduleName == "Config" then
local Config = private.Config
if Config and Config.recover then
local success, _ = pcall(Config.recover)
if success then
private.debug("Integration", "Config recovery successful")
return true
end
end
end
-- Attempt to reinitialize module
if private[moduleName] and private[moduleName].initialize then
local success = pcall(private[moduleName].initialize)
if success then
private.debug("Integration", "Module reinitialization successful")
return true
end
end
return false
end
--[[ Integration.GetErrorLog()
Get the error log.
**Returns:**
* `errorLog` - array of error entries _(table)_
--]]
function Integration.GetErrorLog()
return Integration.errorLog
end
--[[ Integration.ClearErrorLog()
Clear the error log.
--]]
function Integration.ClearErrorLog()
Integration.errorLog = {}
private.debug("Integration", "Error log cleared")
end
-- Event coordination system
--[[ Integration.RegisterEventHandler(_event, moduleName, handler_)
Register an event handler for a module.
**Args:**
* `event` - the event name _(string)_
* `moduleName` - the module name _(string)_
* `handler` - event handler function _(function)_
--]]
function Integration.RegisterEventHandler(event, moduleName, handler)
if not event or not moduleName or type(handler) ~= "function" then
return false
end
if not Integration.eventHandlers[event] then
Integration.eventHandlers[event] = {}
end
Integration.eventHandlers[event][moduleName] = handler
private.debug("Integration", "Registered event handler:", event, "for", moduleName)
return true
end
--[[ Integration.UnregisterEventHandler(_event, moduleName_)
Unregister an event handler.
**Args:**
* `event` - the event name _(string)_
* `moduleName` - the module name _(string)_
--]]
function Integration.UnregisterEventHandler(event, moduleName)
if Integration.eventHandlers[event] then
Integration.eventHandlers[event][moduleName] = nil
return true
end
return false
end
--[[ Integration.DispatchEvent(_event, ..._)
Dispatch an event to all registered handlers.
**Args:**
* `event` - the event name _(string)_
* `...` - event arguments _(any)_
**Returns:**
* `handled` - number of handlers that processed the event _(number)_
* `failed` - number of handlers that failed _(number)_
--]]
function Integration.DispatchEvent(event, ...)
if not Integration.eventHandlers[event] then
return 0, 0
end
local handled = 0
local failed = 0
for moduleName, handler in pairs(Integration.eventHandlers[event]) do
local success, err = xpcall(handler, function(msg)
return msg .. "\n" .. _G.debugstack(2)
end, ...)
if success then
handled = handled + 1
else
failed = failed + 1
Integration.HandleError(moduleName, err, {
event = event,
recoverable = false
})
end
end
return handled, failed
end
-- Performance optimization
--[[ Integration.StartPerformanceTracking(_operation_)
Start tracking performance for an operation.
**Args:**
* `operation` - the operation name _(string)_
**Returns:**
* `trackingId` - tracking identifier _(number)_
--]]
function Integration.StartPerformanceTracking(operation)
local trackingId = _G.debugprofilestop()
Integration.performanceMetrics[trackingId] = {
operation = operation,
startTime = trackingId,
endTime = nil,
duration = nil,
}
return trackingId
end
--[[ Integration.EndPerformanceTracking(_trackingId_)
End performance tracking for an operation.
**Args:**
* `trackingId` - the tracking identifier _(number)_
**Returns:**
* `duration` - operation duration in milliseconds _(number)_
--]]
function Integration.EndPerformanceTracking(trackingId)
local metric = Integration.performanceMetrics[trackingId]
if not metric then
return nil
end
metric.endTime = _G.debugprofilestop()
metric.duration = metric.endTime - metric.startTime
private.debug("Integration", "Performance:", metric.operation, "took", metric.duration, "ms")
return metric.duration
end
--[[ Integration.GetPerformanceMetrics()
Get all performance metrics.
**Returns:**
* `metrics` - table of performance metrics _(table)_
--]]
function Integration.GetPerformanceMetrics()
local metrics = {}
for _, metric in pairs(Integration.performanceMetrics) do
if metric.duration then
metrics[#metrics + 1] = {
operation = metric.operation,
duration = metric.duration,
}
end
end
return metrics
end
--[[ Integration.OptimizeMemoryUsage()
Optimize memory usage by cleaning up unused resources.
**Returns:**
* `freed` - estimated memory freed in KB _(number)_
--]]
function Integration.OptimizeMemoryUsage()
local beforeMemory = _G.collectgarbage("count")
-- Clear old error log entries (keep last 100)
if #Integration.errorLog > 100 then
local newLog = {}
for i = #Integration.errorLog - 99, #Integration.errorLog do
newLog[#newLog + 1] = Integration.errorLog[i]
end
Integration.errorLog = newLog
end
-- Clear old performance metrics (keep last 50)
local metricsArray = {}
for id, metric in pairs(Integration.performanceMetrics) do
if metric.duration then
metricsArray[#metricsArray + 1] = {id = id, metric = metric}
end
end
if #metricsArray > 50 then
-- Sort by end time and keep most recent
table.sort(metricsArray, function(a, b)
return (a.metric.endTime or 0) > (b.metric.endTime or 0)
end)
local newMetrics = {}
for i = 1, 50 do
newMetrics[metricsArray[i].id] = metricsArray[i].metric
end
Integration.performanceMetrics = newMetrics
end
-- Run garbage collection
_G.collectgarbage("collect")
local afterMemory = _G.collectgarbage("count")
local freed = beforeMemory - afterMemory
private.debug("Integration", "Memory optimization freed", freed, "KB")
return freed
end
-- Module coordination
--[[ Integration.RegisterModule(_moduleName, module_)
Register a module with the integration system.
**Args:**
* `moduleName` - the module name _(string)_
* `module` - the module table _(table)_
--]]
function Integration.RegisterModule(moduleName, module)
if not moduleName or not module then
return false
end
Integration.modules[moduleName] = module
private.debug("Integration", "Registered module:", moduleName)
return true
end
--[[ Integration.GetModule(_moduleName_)
Get a registered module.
**Args:**
* `moduleName` - the module name _(string)_
**Returns:**
* `module` - the module table or nil _(table|nil)_
--]]
function Integration.GetModule(moduleName)
return Integration.modules[moduleName]
end
--[[ Integration.InitializeAllModules()
Initialize all registered modules in proper order.
**Returns:**
* `initialized` - number of modules initialized _(number)_
* `failed` - number of modules that failed _(number)_
--]]
function Integration.InitializeAllModules()
local initialized = 0
local failed = 0
-- Define initialization order
local initOrder = {
"Config",
"Analytics",
"Compatibility",
"Theme",
}
for _, moduleName in pairs(initOrder) do
local module = Integration.modules[moduleName]
if module and module.initialize then
local trackingId = Integration.StartPerformanceTracking("Initialize " .. moduleName)
local success, err = xpcall(module.initialize, function(msg)
return msg .. "\n" .. _G.debugstack(2)
end)
Integration.EndPerformanceTracking(trackingId)
if success then
initialized = initialized + 1
private.debug("Integration", "Initialized module:", moduleName)
else
failed = failed + 1
Integration.HandleError(moduleName, err, {
phase = "initialization",
recoverable = true
})
end
end
end
return initialized, failed
end
--[[ Integration.WireModules()
Wire all modules together with proper event handling.
**Returns:**
* `success` - whether wiring succeeded _(boolean)_
--]]
function Integration.WireModules()
private.debug("Integration", "Wiring modules together")
-- Register Config module events
Integration.RegisterEventHandler("CONFIG_CHANGED", "Theme", function(key, value)
local Aurora = private.Aurora
if Aurora and Aurora.Theme then
-- Handle alpha changes
if key == "alpha" and Aurora.Theme.SetGlobalAlpha then
Aurora.Theme.SetGlobalAlpha(value)
end
-- Handle color changes
if key == "customHighlight" and private.updateHighlightColor then
private.updateHighlightColor()
end
end
end)
-- Register Analytics events
Integration.RegisterEventHandler("CONFIG_CHANGED", "Analytics", function(key, value)
local Analytics = private.Analytics
if Analytics and Analytics.trackConfigChange then
Analytics.trackConfigChange(key, value)
end
end)
-- Register Compatibility events
Integration.RegisterEventHandler("ADDON_LOADED", "Compatibility", function(addonName)
local Compatibility = private.Compatibility
if Compatibility and Compatibility.checkConflicts then
Compatibility.checkConflicts()
end
end)
private.debug("Integration", "Module wiring complete")
return true
end
--[[ Integration.Initialize()
Initialize the integration system.
**Returns:**
* `success` - whether initialization succeeded _(boolean)_
--]]
function Integration.Initialize()
if Integration.initialized then
return true
end
private.debug("Integration", "Initializing integration system")
-- Register core modules
Integration.RegisterModule("Config", private.Config)
Integration.RegisterModule("Analytics", private.Analytics)
Integration.RegisterModule("Compatibility", private.Compatibility)
Integration.RegisterModule("Theme", private.Aurora and private.Aurora.Theme)
-- Wire modules together
Integration.WireModules()
-- Set up periodic optimization
_G.C_Timer.NewTicker(300, function() -- Every 5 minutes
Integration.OptimizeMemoryUsage()
-- Optimize theme engine if available
local Aurora = private.Aurora
if Aurora and Aurora.Theme and Aurora.Theme.OptimizeFrameProcessing then
Aurora.Theme.OptimizeFrameProcessing()
end
end)
Integration.initialized = true
private.debug("Integration", "Integration system initialized")
return true
end
--[[ Integration.GetStatus()
Get integration system status.
**Returns:**
* `status` - status information table _(table)_
--]]
function Integration.GetStatus()
return {
initialized = Integration.initialized,
moduleCount = 0, -- Count registered modules
errorCount = #Integration.errorLog,
eventHandlerCount = 0, -- Count event handlers
performanceMetricsCount = 0, -- Count metrics
}
end