add input event observer and hook the value setter

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent ad2ac811a3
commit c96052d8a4
4 changed files with 139 additions and 4 deletions

View File

@@ -1,4 +1,9 @@
import { Mirror, throttleOptions, listenerHandler } from './types';
import {
Mirror,
throttleOptions,
listenerHandler,
hookResetter,
} from './types';
export function on(
type: string,
@@ -17,13 +22,14 @@ export const mirror: Mirror = {
getNode(id) {
return mirror.map[id];
},
// TODO: use a weakmap to get rid of manually memory management
removeNodeFromMap(n) {
const id = n.__sn && n.__sn.id;
delete mirror.map[id];
},
};
// copy from underscore
// copy from underscore and modified
export function throttle<T>(
func: (arg: T) => void,
wait: number,
@@ -56,3 +62,20 @@ export function throttle<T>(
}
};
}
export function hookSetter<T>(
target: T,
key: string | number | symbol,
d: PropertyDescriptor,
): hookResetter {
const original = Object.getOwnPropertyDescriptor(target, key);
Object.defineProperty(target, key, {
set(value) {
d.set!.call(this, value);
if (original && original.set) {
original.set.call(this, value);
}
},
});
return () => hookSetter(target, key, original || {});
}