fix: finalize presentation event immutability
This commit is contained in:
@@ -2,78 +2,52 @@ class_name PresentationEvent
|
||||
extends RefCounted
|
||||
|
||||
## A display-only, immutable translation of one authoritative GameEvent.
|
||||
## Getter calls return a fresh source snapshot so presentation code cannot mutate
|
||||
## the source domain event or this adapter's captured event data.
|
||||
##
|
||||
## GDScript's leading underscore is only a convention: declared fields remain
|
||||
## writable. The backing properties below are virtual so _set can absorb every
|
||||
## write after construction.
|
||||
const BACKING_PROPERTIES := [&"_source_event", &"_kind", &"_notification", &"_accent"]
|
||||
## Snapshot data belongs to this class, not an instance, so direct writes cannot
|
||||
## create mutable instance state. Getter calls return a fresh source snapshot.
|
||||
## RefCounted teardown cannot safely call script methods, so the static store is
|
||||
## intentionally process-lifetime; entries contain only frozen presentation data.
|
||||
const FORMER_BACKING_PROPERTIES := [
|
||||
&"_backing_values",
|
||||
&"_is_initialized",
|
||||
&"_source_event",
|
||||
&"_kind",
|
||||
&"_notification",
|
||||
&"_accent",
|
||||
]
|
||||
|
||||
var _backing_values: Dictionary[StringName, Variant] = {}
|
||||
var _is_initialized := false
|
||||
|
||||
var _source_event: GameEvent:
|
||||
set(value):
|
||||
_set(&"_source_event", value)
|
||||
get:
|
||||
return _captured_source_snapshot()
|
||||
|
||||
var _kind: StringName:
|
||||
set(value):
|
||||
_set(&"_kind", value)
|
||||
get:
|
||||
return _backing_values.get(&"_kind", &"") as StringName
|
||||
|
||||
var _notification: String:
|
||||
set(value):
|
||||
_set(&"_notification", value)
|
||||
get:
|
||||
return _backing_values.get(&"_notification", "") as String
|
||||
|
||||
var _accent: Color:
|
||||
set(value):
|
||||
_set(&"_accent", value)
|
||||
get:
|
||||
return _backing_values.get(&"_accent", Color.WHITE) as Color
|
||||
static var _snapshots: Dictionary[int, Dictionary] = {}
|
||||
|
||||
var source_event: GameEvent:
|
||||
get:
|
||||
return _copy_event(_source_event)
|
||||
return _copy_event(_snapshot()[&"source_event"] as GameEvent)
|
||||
|
||||
var kind: StringName:
|
||||
get:
|
||||
return _kind
|
||||
return _snapshot()[&"kind"] as StringName
|
||||
|
||||
var notification: String:
|
||||
get:
|
||||
return _notification
|
||||
return _snapshot()[&"notification"] as String
|
||||
|
||||
var accent: Color:
|
||||
get:
|
||||
return _accent
|
||||
return _snapshot()[&"accent"] as Color
|
||||
|
||||
func _init(source: GameEvent, presentation_kind: StringName, text: String = "", color: Color = Color.WHITE) -> void:
|
||||
_source_event = _copy_event(source)
|
||||
_kind = presentation_kind
|
||||
_notification = text if not text.is_empty() else source.message
|
||||
_accent = color
|
||||
_is_initialized = true
|
||||
_snapshots[get_instance_id()] = {
|
||||
&"source_event": _copy_event(source),
|
||||
&"kind": presentation_kind,
|
||||
&"notification": text if not text.is_empty() else source.message,
|
||||
&"accent": color,
|
||||
}
|
||||
|
||||
func _set(property: StringName, value: Variant) -> bool:
|
||||
if property not in BACKING_PROPERTIES:
|
||||
return false
|
||||
if not _is_initialized:
|
||||
_backing_values[property] = value
|
||||
return true
|
||||
## Former fields are intentionally undeclared. _set absorbs legacy direct writes
|
||||
## without creating per-instance storage or changing the captured snapshot.
|
||||
func _set(property: StringName, _value: Variant) -> bool:
|
||||
return property in FORMER_BACKING_PROPERTIES
|
||||
|
||||
func _get(property: StringName) -> Variant:
|
||||
if property in BACKING_PROPERTIES:
|
||||
return _backing_values.get(property)
|
||||
return null
|
||||
|
||||
func _captured_source_snapshot() -> GameEvent:
|
||||
return _copy_event(_backing_values.get(&"_source_event") as GameEvent)
|
||||
func _snapshot() -> Dictionary:
|
||||
return _snapshots.get(get_instance_id(), {}) as Dictionary
|
||||
|
||||
static func _copy_event(event: GameEvent) -> GameEvent:
|
||||
return GameEvent.new(
|
||||
|
||||
Reference in New Issue
Block a user