| 1 | export default class DevicePixelRatioObserver { |
| 2 | mediaQueryList: MediaQueryList | null = null |
| 3 | |
| 4 | constructor(readonly onDevicePixelRatioChanged: () => void) { |
| 5 | this._onChange = this._onChange.bind(this) |
| 6 | this.createMediaQueryList() |
| 7 | } |
| 8 | |
| 9 | createMediaQueryList() { |
| 10 | this.removeMediaQueryList() |
| 11 | let mqString = `(resolution: ${window.devicePixelRatio}dppx)`; |
| 12 | |
| 13 | this.mediaQueryList = matchMedia(mqString); |
| 14 | this.mediaQueryList.addEventListener('change', this._onChange) |
| 15 | } |
| 16 | removeMediaQueryList() { |
| 17 | this.mediaQueryList?.removeEventListener('change', this._onChange) |
| 18 | this.mediaQueryList = null |
| 19 | } |
| 20 | _onChange(event: MediaQueryListEvent) { |
| 21 | this.onDevicePixelRatioChanged() |
| 22 | this.createMediaQueryList() |
| 23 | } |
| 24 | destroy() { |
| 25 | this.removeMediaQueryList() |
| 26 | } |
| 27 | } |