Merge pull request #59 from vadimkorr/feature/#56_Switch-pausable-handlers-dynamically

feature/#56 Switch pausable handlers dynamically
This commit is contained in:
Vadim
2021-08-16 00:22:47 +03:00
committed by GitHub
2 changed files with 42 additions and 5 deletions

View File

@@ -1,15 +1,14 @@
import { import {
addTouchableChangeEventListener,
createDispatcher, createDispatcher,
getIsTouchable,
} from '../../utils/event' } from '../../utils/event'
import { focusable } from '../focusable' import { focusable } from '../focusable'
import { tappable } from '../tappable' import { tappable } from '../tappable'
export function pausable(node) { function getHandler(isTouchable, node) {
const dispatch = createDispatcher(node) const dispatch = createDispatcher(node)
if (getIsTouchable()) { if (isTouchable) {
return tappable(node, { return tappable(node, {
dispatch: (_, payload) => { dispatch: (_, payload) => {
dispatch('pausedToggle', { dispatch('pausedToggle', {
@@ -28,3 +27,21 @@ export function pausable(node) {
} }
}) })
} }
export function pausable(node) {
let destroy
const handleTouchableChange = (isTouchable) => {
destroy && destroy() // destroy when touchable changed
destroy = getHandler(isTouchable, node).destroy
}
// in order to change handlers when browser was switched to mobile view and vice versa
const removeTouchableChangeListener = addTouchableChangeEventListener(handleTouchableChange)
return {
destroy() {
removeTouchableChangeListener()
destroy() // destroy here in case if touchable was not changed
}
}
}

View File

@@ -1,3 +1,5 @@
import { setIntervalImmediate } from './interval'
// resize event // resize event
export function addResizeEventListener(cb) { export function addResizeEventListener(cb) {
window.addEventListener('resize', cb) window.addEventListener('resize', cb)
@@ -18,8 +20,26 @@ export function createDispatcher(source) {
export function getIsTouchable() { export function getIsTouchable() {
return ( return (
('ontouchstart' in window) || // ('ontouchstart' in window) || // not changing value during browser view switching (mobile <-> desktop)
(navigator.maxTouchPoints > 0) || (navigator.maxTouchPoints > 0) ||
(navigator.msMaxTouchPoints > 0) (navigator.msMaxTouchPoints > 0)
) )
} }
export function addTouchableChangeEventListener(cb) {
let isTouchable = null
function handleResize() {
const isTouchableNext = getIsTouchable();
if (isTouchable !== isTouchableNext) {
cb(isTouchableNext)
isTouchable = isTouchableNext
}
}
const interval = setIntervalImmediate(handleResize, 500);
return () => {
clearInterval(interval)
}
}