Add statesListeners to ComponentOptions and trigger state update events in setState

This commit is contained in:
Astrian Zheng 2025-05-14 16:25:51 +10:00
parent 9d62c64b0f
commit b9cb3f455c
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA

View File

@ -11,10 +11,11 @@ interface ComponentOptions {
onUnmount?: () => void onUnmount?: () => void
onAttributeChanged?: (attrName: string, oldValue: string, newValue: string) => void onAttributeChanged?: (attrName: string, oldValue: string, newValue: string) => void
states?: Record<string, any> states?: Record<string, any>
statesListeners?: { [key: string]: (value: any) => void }
} }
export default (options: ComponentOptions) => { export default (options: ComponentOptions) => {
const { tag, template, style, onMount, onUnmount, onAttributeChanged, states } = options const { tag, template, style, onMount, onUnmount, onAttributeChanged, states, statesListeners } = options
const componentRegistry = new Map() const componentRegistry = new Map()
componentRegistry.set(tag, options) componentRegistry.set(tag, options)
@ -41,7 +42,12 @@ export default (options: ComponentOptions) => {
} }
} }
// TODO: trigger dom updates // TODO: trigger dom updates
// TODO: trigger state update events
// trigger state update events
if (statesListeners && statesListeners[keyPath]) {
statesListeners[keyPath](value)
}
return true return true
}, },
get: (target: Record<string, any>, keyPath: string) => { get: (target: Record<string, any>, keyPath: string) => {