|
20 | 20 | import numpy as np
|
21 | 21 | from PIL import Image
|
22 | 22 |
|
| 23 | +from pyodide.ffi.wrappers import ( |
| 24 | + clear_interval, clear_timeout, set_interval, set_timeout) |
| 25 | + |
23 | 26 | from matplotlib import _api, backend_bases, backend_tools
|
24 | 27 | from matplotlib.backends import backend_agg
|
25 | 28 | from matplotlib.backend_bases import (
|
@@ -79,9 +82,38 @@ def _handle_key(key):
|
79 | 82 | return key
|
80 | 83 |
|
81 | 84 |
|
| 85 | +class TimerJs(backend_bases.TimerBase): |
| 86 | + def __init__(self, *args, **kwargs): |
| 87 | + self._timer: int | None = None |
| 88 | + super().__init__(*args, **kwargs) |
| 89 | + |
| 90 | + def _timer_start(self): |
| 91 | + self._timer_stop() |
| 92 | + if self._single: |
| 93 | + self._timer = set_timeout(self._on_timer, self.interval) |
| 94 | + else: |
| 95 | + self._timer = set_interval(self._on_timer, self.interval) |
| 96 | + |
| 97 | + def _timer_stop(self): |
| 98 | + if self._timer is None: |
| 99 | + return |
| 100 | + elif self._single: |
| 101 | + clear_timeout(self._timer) |
| 102 | + self._timer = None |
| 103 | + else: |
| 104 | + clear_interval(self._timer) |
| 105 | + self._timer = None |
| 106 | + |
| 107 | + def _timer_set_interval(self): |
| 108 | + # Only stop and restart it if the timer has already been started |
| 109 | + if self._timer is not None: |
| 110 | + self._timer_stop() |
| 111 | + self._timer_start() |
| 112 | + |
82 | 113 |
|
83 | 114 | class FigureCanvasWebAggCore(backend_agg.FigureCanvasAgg):
|
84 | 115 | manager_class = _api.classproperty(lambda cls: FigureManagerWebAgg)
|
| 116 | + _timer_cls = TimerJs |
85 | 117 | # Webagg and friends having the right methods, but still
|
86 | 118 | # having bugs in practice. Do not advertise that it works until
|
87 | 119 | # we can debug this.
|
|
0 commit comments