HumanBreak/packages/legacy-ui/src/tools/common.ts

37 lines
790 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Ref, ref, watch } from 'vue';
import { nextFrame } from '../utils';
interface ChangableValue<T> {
change: Ref<boolean>;
value: Ref<T>;
stop: () => void;
}
/**
* 创建一个监听响应式变量更改的可以用于Changable的监听器
* @param value 要监听的值
*/
export function createChangable<T>(
value: Ref<T>,
key?: keyof T
): ChangableValue<T> {
const change = ref(false);
const stop = watch(value, (n, o) => {
if (key) {
if (n[key] === o[key]) return;
}
if (change.value) {
change.value = false;
nextFrame(() => (change.value = true));
} else {
change.value = true;
}
});
return {
change,
value,
stop
};
}