mirror of
https://github.com/pyapp-kit/superqt.git
synced 2026-01-06 20:31:09 +01:00
add restart_timer argument to GenericSignalThrottler.flush (#206)
This commit is contained in:
@@ -139,18 +139,28 @@ class GenericSignalThrottler(QObject):
|
|||||||
"""Cancel any pending emissions."""
|
"""Cancel any pending emissions."""
|
||||||
self._hasPendingEmission = False
|
self._hasPendingEmission = False
|
||||||
|
|
||||||
def flush(self) -> None:
|
def flush(self, restart_timer: bool = True) -> None:
|
||||||
"""Force emission of any pending emissions."""
|
"""
|
||||||
self._maybeEmitTriggered()
|
Force emission of any pending emissions.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
restart_timer : bool
|
||||||
|
Whether to restart the timer after flushing.
|
||||||
|
Defaults to True.
|
||||||
|
"""
|
||||||
|
self._maybeEmitTriggered(restart_timer=restart_timer)
|
||||||
|
|
||||||
def _emitTriggered(self) -> None:
|
def _emitTriggered(self) -> None:
|
||||||
self._hasPendingEmission = False
|
self._hasPendingEmission = False
|
||||||
self.triggered.emit()
|
self.triggered.emit()
|
||||||
self._timer.start()
|
self._timer.start()
|
||||||
|
|
||||||
def _maybeEmitTriggered(self) -> None:
|
def _maybeEmitTriggered(self, restart_timer=True) -> None:
|
||||||
if self._hasPendingEmission:
|
if self._hasPendingEmission:
|
||||||
self._emitTriggered()
|
self._emitTriggered()
|
||||||
|
if not restart_timer:
|
||||||
|
self._timer.stop()
|
||||||
|
|
||||||
Kind = Kind
|
Kind = Kind
|
||||||
EmissionPolicy = EmissionPolicy
|
EmissionPolicy = EmissionPolicy
|
||||||
|
|||||||
@@ -27,6 +27,41 @@ def test_debounced(qtbot):
|
|||||||
assert mock2.call_count == 10
|
assert mock2.call_count == 10
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("qapp")
|
||||||
|
def test_stop_timer_simple():
|
||||||
|
mock = Mock()
|
||||||
|
|
||||||
|
@qdebounced(timeout=5)
|
||||||
|
def f1() -> str:
|
||||||
|
mock()
|
||||||
|
|
||||||
|
f1()
|
||||||
|
assert f1._timer.isActive()
|
||||||
|
mock.assert_not_called()
|
||||||
|
f1.flush(restart_timer=False)
|
||||||
|
assert not f1._timer.isActive()
|
||||||
|
mock.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("qapp")
|
||||||
|
def test_stop_timer_no_event_pending():
|
||||||
|
mock = Mock()
|
||||||
|
|
||||||
|
@qdebounced(timeout=5)
|
||||||
|
def f1() -> str:
|
||||||
|
mock()
|
||||||
|
|
||||||
|
f1()
|
||||||
|
assert f1._timer.isActive()
|
||||||
|
mock.assert_not_called()
|
||||||
|
f1.flush()
|
||||||
|
assert f1._timer.isActive()
|
||||||
|
mock.assert_called_once()
|
||||||
|
f1.flush(restart_timer=False)
|
||||||
|
assert not f1._timer.isActive()
|
||||||
|
mock.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
def test_debouncer_method(qtbot):
|
def test_debouncer_method(qtbot):
|
||||||
class A(QObject):
|
class A(QObject):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user