Enhance state management: implement version tracking and conditional broadcasting for configuration updates

This commit is contained in:
MayaTheShy
2026-03-22 01:40:04 -04:00
parent b2c635b7ef
commit 2bcb907914

View File

@@ -681,6 +681,18 @@ local LOW_STOCK_ALERTS = {
-- Active alerts (populated by checkAlerts)
local activeAlerts = {}
-------------------------------------------------
-- State version tracking (for delta broadcasting)
-------------------------------------------------
local stateVersion = 0
local lastBroadcastVersion = -1
local configDirty = true
local function bumpStateVersion()
stateVersion = stateVersion + 1
end
-------------------------------------------------
-- Cached data (updated by background scanner)
-------------------------------------------------
@@ -769,6 +781,9 @@ local function adjustCache(itemName, chestName, delta)
-- 3) Update grandTotal incrementally
cache.grandTotal = cache.grandTotal + delta
-- 4) Bump state version for delta broadcasting
bumpStateVersion()
end
--- Rebuild itemList from catalogue if dirty (lazy rebuild)
@@ -3065,9 +3080,11 @@ end
-- Network broadcast (sends state to client displays)
-------------------------------------------------
local function broadcastState()
local function broadcastState(force)
if not networkModem then return end
ensureItemList()
-- Build dynamic state (always included)
local state = {
type = "state",
cache = {
@@ -3088,11 +3105,18 @@ local function broadcastState()
alerts = activeAlerts,
smeltingPaused = smeltingPaused,
disabledRecipes = disabledRecipes,
smeltable = SMELTABLE,
craftable = CRAFTABLE,
craftTurtleOk = craftTurtleName and peripheral.isPresent(craftTurtleName),
}
-- Include static config only when dirty (startup, scan, recipe toggles)
if configDirty then
state.smeltable = SMELTABLE
state.craftable = CRAFTABLE
configDirty = false
end
networkModem.transmit(BROADCAST_CHANNEL, ORDER_CHANNEL, state)
lastBroadcastVersion = stateVersion
end
-------------------------------------------------