All files / src/gestures/drag/utils lock.ts

94.12% Statements 32/34
87.5% Branches 14/16
100% Functions 6/6
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53    33x 66x 66x 125x 120x   125x 120x 120x   5x       33x 33x   33x     68x 68x 2x 66x 9x   57x 57x 57x 56x 56x 56x       1x 1x     68x     33x     37x 37x 36x 36x    
export type Lock = (() => void) | false
 
export function createLock(name: string) {
    let lock: null | string = null
    return (): Lock => {
        const openLock = (): void => {
            lock = null
        }
        if (lock === null) {
            lock = name
            return openLock
        }
        return false
    }
}
 
const globalHorizontalLock = createLock("dragHorizontal")
const globalVerticalLock = createLock("dragVertical")
 
export function getGlobalLock(
    drag: boolean | "x" | "y" | "lockDirection"
): Lock {
    let lock: Lock = false
    if (drag === "y") {
        lock = globalVerticalLock()
    } else if (drag === "x") {
        lock = globalHorizontalLock()
    } else {
        const openHorizontal = globalHorizontalLock()
        const openVertical = globalVerticalLock()
        if (openHorizontal && openVertical) {
            lock = () => {
                openHorizontal()
                openVertical()
            }
        } else {
            // Release the locks because we don't use them
            Iif (openHorizontal) openHorizontal()
            Iif (openVertical) openVertical()
        }
    }
    return lock
}
 
export function isDragActive() {
    // Check the gesture lock - if we get it, it means no drag gesture is active
    // and we can safely fire the tap gesture.
    const openGestureLock = getGlobalLock(true)
    if (!openGestureLock) return true
    openGestureLock()
    return false
}