Enhance state management: implement version tracking and conditional broadcasting for configuration updates
This commit is contained in:
@@ -681,6 +681,18 @@ local LOW_STOCK_ALERTS = {
|
|||||||
-- Active alerts (populated by checkAlerts)
|
-- Active alerts (populated by checkAlerts)
|
||||||
local activeAlerts = {}
|
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)
|
-- Cached data (updated by background scanner)
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
@@ -769,6 +781,9 @@ local function adjustCache(itemName, chestName, delta)
|
|||||||
|
|
||||||
-- 3) Update grandTotal incrementally
|
-- 3) Update grandTotal incrementally
|
||||||
cache.grandTotal = cache.grandTotal + delta
|
cache.grandTotal = cache.grandTotal + delta
|
||||||
|
|
||||||
|
-- 4) Bump state version for delta broadcasting
|
||||||
|
bumpStateVersion()
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Rebuild itemList from catalogue if dirty (lazy rebuild)
|
--- Rebuild itemList from catalogue if dirty (lazy rebuild)
|
||||||
@@ -3065,9 +3080,11 @@ end
|
|||||||
-- Network broadcast (sends state to client displays)
|
-- Network broadcast (sends state to client displays)
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
local function broadcastState()
|
local function broadcastState(force)
|
||||||
if not networkModem then return end
|
if not networkModem then return end
|
||||||
ensureItemList()
|
ensureItemList()
|
||||||
|
|
||||||
|
-- Build dynamic state (always included)
|
||||||
local state = {
|
local state = {
|
||||||
type = "state",
|
type = "state",
|
||||||
cache = {
|
cache = {
|
||||||
@@ -3088,11 +3105,18 @@ local function broadcastState()
|
|||||||
alerts = activeAlerts,
|
alerts = activeAlerts,
|
||||||
smeltingPaused = smeltingPaused,
|
smeltingPaused = smeltingPaused,
|
||||||
disabledRecipes = disabledRecipes,
|
disabledRecipes = disabledRecipes,
|
||||||
smeltable = SMELTABLE,
|
|
||||||
craftable = CRAFTABLE,
|
|
||||||
craftTurtleOk = craftTurtleName and peripheral.isPresent(craftTurtleName),
|
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)
|
networkModem.transmit(BROADCAST_CHANNEL, ORDER_CHANNEL, state)
|
||||||
|
lastBroadcastVersion = stateVersion
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user