<commit_before>import {combineLatest, fromEvent, merge, timer} from 'rxjs'
import {switchMap, take, takeUntil} from 'rxjs/operators'
import PropTypes from 'prop-types'
import React from 'react'

const DELAY_MS = 750

export class HoldButton extends React.Component {
    button = React.createRef()
    subscriptions = []

    render() {
        const {type, className, style, tabIndex, disabled, onMouseDown, onClick, children} = this.props
        return (
            <button
                ref={this.button}
                type={type}
                className={className}
                style={style}
                tabIndex={tabIndex}
                disabled={disabled}
                onMouseDown={onMouseDown}
                onClick={onClick}
            >
                {children}
            </button>
        )
    }

    componentDidMount() {
        const {onClickHold} = this.props
        if (onClickHold) {
            const button = this.button.current
            const buttonMouseDown$ = merge(fromEvent(button, 'mousedown'), fromEvent(button, 'touchstart'))
            const buttonMouseEnter$ = fromEvent(button, 'mouseenter')
            const buttonMouseUp$ = merge(fromEvent(button, 'mouseup'), fromEvent(button, 'touchend'))
            const windowMouseUp$ = merge(fromEvent(window, 'mouseup'), fromEvent(window, 'touchend'))

            const trigger$ = combineLatest(buttonMouseDown$, buttonMouseEnter$)
            const cancel$ = windowMouseUp$
            const activate$ = buttonMouseUp$

            const clickHold$ =
                trigger$.pipe(
                    switchMap(() => {
                        return timer(DELAY_MS).pipe(
                            takeUntil(cancel$),
                            switchMap(() =>
                                activate$.pipe(
                                    takeUntil(cancel$),
                                    take(1)
                                )
                            )
                        )
                    })
                )
            
            this.subscriptions.push(
                clickHold$.subscribe(() => {
                    const {onClickHold, disabled} = this.props
                    if (onClickHold && !disabled)
                        onClickHold()
                })
            )
        }
    }

    componentWillUnmount() {
        this.subscriptions.forEach(subscription => subscription.unsubscribe())
    }
}

HoldButton.propTypes = {
    children: PropTypes.any,<commit_msg>Fixed not passing event to handler.<commit_after>import {combineLatest, fromEvent, merge, timer} from 'rxjs'
import {switchMap, take, takeUntil} from 'rxjs/operators'
import PropTypes from 'prop-types'
import React from 'react'

const DELAY_MS = 750

export class HoldButton extends React.Component {
    button = React.createRef()
    subscriptions = []

    render() {
        const {type, className, style, tabIndex, disabled, onMouseDown, onClick, children} = this.props
        return (
            <button
                ref={this.button}
                type={type}
                className={className}
                style={style}
                tabIndex={tabIndex}
                disabled={disabled}
                onMouseDown={onMouseDown}
                onClick={onClick}
            >
                {children}
            </button>
        )
    }

    componentDidMount() {
        const {onClickHold} = this.props
        if (onClickHold) {
            const button = this.button.current
            const buttonMouseDown$ = merge(fromEvent(button, 'mousedown'), fromEvent(button, 'touchstart'))
            const buttonMouseEnter$ = fromEvent(button, 'mouseenter')
            const buttonMouseUp$ = merge(fromEvent(button, 'mouseup'), fromEvent(button, 'touchend'))
            const windowMouseUp$ = merge(fromEvent(window, 'mouseup'), fromEvent(window, 'touchend'))

            const trigger$ = combineLatest(buttonMouseDown$, buttonMouseEnter$)
            const cancel$ = windowMouseUp$
            const activate$ = buttonMouseUp$

            const clickHold$ =
                trigger$.pipe(
                    switchMap(() => {
                        return timer(DELAY_MS).pipe(
                            takeUntil(cancel$),
                            switchMap(() =>
                                activate$.pipe(
                                    takeUntil(cancel$),
                                    take(1)
                                )
                            )
                        )
                    })
                )
            
            this.subscriptions.push(
                clickHold$.subscribe(e => {
                    const {onClickHold, disabled} = this.props
                    if (onClickHold && !disabled)
                        onClickHold(e)
                })
            )
        }
    }

    componentWillUnmount() {
        this.subscriptions.forEach(subscription => subscription.unsubscribe())
    }
}

HoldButton.propTypes = {
    children: PropTypes.any,
{'fraction_non_alphanumeric': 0.09657749803304484, 'fraction_numerical': 0.0015735641227380016, 'mean_word_length': 1.4133839582344565, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2358310', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1196, 'n_words': 250}