From b9cb3f455ccfb073a5f4511a6db7e8f88e972f1e Mon Sep 17 00:00:00 2001 From: Astrian Zheng Date: Wed, 14 May 2025 16:25:51 +1000 Subject: [PATCH] Add statesListeners to ComponentOptions and trigger state update events in setState --- src/main.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index 20f3f77..e39e653 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,10 +11,11 @@ interface ComponentOptions { onUnmount?: () => void onAttributeChanged?: (attrName: string, oldValue: string, newValue: string) => void states?: Record + statesListeners?: { [key: string]: (value: any) => void } } 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() componentRegistry.set(tag, options) @@ -41,7 +42,12 @@ export default (options: ComponentOptions) => { } } // TODO: trigger dom updates - // TODO: trigger state update events + + // trigger state update events + if (statesListeners && statesListeners[keyPath]) { + statesListeners[keyPath](value) + } + return true }, get: (target: Record, keyPath: string) => {