1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 10:54:44 +00:00

fix(packages/vue): add mutex for useRenderCount to avoid infinite rerender

This commit is contained in:
2025-02-23 00:44:58 +07:00
parent 8c5252986e
commit a07ac35db9
2 changed files with 17 additions and 5 deletions

View File

@@ -32,7 +32,7 @@ describe('useRenderCount', () => {
await nextTick(); await nextTick();
// Will trigger a render // Will trigger a render
expect(component.vm.count).toBe(2); expect(component.vm.count).toBe(1);
expect(component.text()).toBe('1'); expect(component.text()).toBe('1');
component.vm.visibleCount++; component.vm.visibleCount++;
@@ -40,7 +40,7 @@ describe('useRenderCount', () => {
await nextTick(); await nextTick();
// Will trigger a single render for both updates // Will trigger a single render for both updates
expect(component.vm.count).toBe(3); expect(component.vm.count).toBe(2);
expect(component.text()).toBe('3'); expect(component.text()).toBe('3');
}); });
@@ -70,6 +70,6 @@ describe('useRenderCount', () => {
await nextTick(); await nextTick();
// Will trigger a single render for both updates // Will trigger a single render for both updates
expect(count.value).toBe(2); expect(count.value).toBe(1);
}); });
}); });

View File

@@ -1,6 +1,7 @@
import { onMounted, onUpdated, readonly, type ComponentInternalInstance } from 'vue'; import { onMounted, onUpdated, readonly, type ComponentInternalInstance } from 'vue';
import { useCounter } from '../useCounter'; import { useCounter } from '../useCounter';
import { getLifeCycleTarger } from '../..'; import { getLifeCycleTarger } from '../..';
import { SyncMutex } from '@robonen/stdlib';
/** /**
* @name useRenderCount * @name useRenderCount
@@ -19,11 +20,22 @@ import { getLifeCycleTarger } from '../..';
* @since 0.0.1 * @since 0.0.1
*/ */
export function useRenderCount(instance?: ComponentInternalInstance) { export function useRenderCount(instance?: ComponentInternalInstance) {
const mutex = new SyncMutex();
const { count, increment } = useCounter(0); const { count, increment } = useCounter(0);
const target = getLifeCycleTarger(instance); const target = getLifeCycleTarger(instance);
onMounted(increment, target); const incrementEffect = () => {
onUpdated(increment, target); if (mutex.isLocked) {
mutex.unlock();
return;
}
mutex.lock();
increment();
};
onMounted(incrementEffect, target);
onUpdated(incrementEffect, target);
return readonly(count); return readonly(count);
} }