import { Component } from 'react';
import PropTypes from 'prop-types';
class SoftObserver extends Component {
    constructor(...args) {
        // @ts-ignore
        super(...args);
        this.updateChildNode = ref => {
            if (this.childNode === ref)
                return; // node hasn't changed
            // if there's an existing observer disconnect it
            if (this.observer != null) {
                this.observer.disconnect();
                this.observer = null;
            }
            this.childNode = ref;
            if (this.childNode != null) {
                this.beginObserve();
            }
        };
        this.beginObserve = () => {
            throw new Error('SoftObserver has no default observation method');
        };
        this.name = 'SoftObserver';
        this.childNode = null;
        this.observer = null;
    }
    componentDidMount() {
        if (this.childNode == null) {
            throw new Error(`${this.name} did not receive a ref`);
        }
    }
    componentWillUnmount() {
        if (this.observer != null) {
            this.observer.disconnect();
        }
    }
    render() {
        return this.props.children(this.updateChildNode);
    }
}
SoftObserver.propTypes = {
    children: PropTypes.func.isRequired
};
export { SoftObserver };
//# sourceMappingURL=SoftObserver.js.map