<commit_before>import {FefDebounceHelper} from './fef-debounce-helper';

const DEBOUNCED = 'debounced';
const THROTTLED = 'throttled';
const DEBOUNCETIME = 100;
const THROTTLERATE = 100;

class ResizeListener {
    constructor() {
        this.listeners = [];
        this.lastId = 0;
        this.wasListening = false;
        this.debouncedListener = FefDebounceHelper.debounce(() => this.notifySubscribers(DEBOUNCED), DEBOUNCETIME);
        this.throttledListener = FefDebounceHelper.throttle(() => this.notifySubscribers(THROTTLED), THROTTLERATE);
    }
    
    subscribe(callback = () => {}, type = DEBOUNCED) {
        let id = this.lastId++;
        
        this.listeners.push({
            id: id,
            callback: callback,
            type: type
        });
        
        this.updateEventListeners();
        return id;
    }
    
    unsubscribe(idToRemove) {
        this.listeners = this.listeners.filter(listener => listener.id !== idToRemove);
        this.updateEventListeners();
    }
    
    updateEventListeners() {
        if (this.listeners.length > 0 && !this.wasListening) {
            // Wasn't listening before, should be now --> start listening
            this.startListening();
        } else if (this.listeners.length === 0 && this.wasListening) {
            // Was listening, shouldn't anymore --> stop listening
            this.stopListening();
        }
    }
    
    startListening() {
        window.addEventListener('resize', this.debouncedListener, { passive: true });
        window.addEventListener('resize', this.throttledListener, { passive: true });
        this.wasListening = true;
    }
    
    stopListening() {
        window.removeEventListener('resize', this.debouncedListener, false);
        window.removeEventListener('resize', this.throttledListener, false);
        this.wasListening = false;
    }
    
    notifySubscribers(type) {
        let filtered = this.listeners.filter(listener => listener.type === type);
        
        if (filtered.length > 0) {
            filtered.forEach(listener => listener.callback());
        }
    }
}

let rl = new ResizeListener();

export class FefResizeListener {
    static subscribe(callback = () => {}, type = DEBOUNCED) {
        return rl.subscribe(callback, type);
    }

    static unsubscribe(idToRemove) {
        rl.unsubscribe(idToRemove);
    }
}<commit_msg>add helper functions so consts don't need to be exported<commit_after>import {FefDebounceHelper} from './fef-debounce-helper';

const DEBOUNCED = 'debounced';
const THROTTLED = 'throttled';
const DEBOUNCETIME = 100;
const THROTTLERATE = 100;

class ResizeListener {
    constructor() {
        this.listeners = [];
        this.lastId = 0;
        this.wasListening = false;
        this.debouncedListener = FefDebounceHelper.debounce(() => this.notifySubscribers(DEBOUNCED), DEBOUNCETIME);
        this.throttledListener = FefDebounceHelper.throttle(() => this.notifySubscribers(THROTTLED), THROTTLERATE);
    }
    
    subscribe(callback = () => {}, type = DEBOUNCED) {
        let id = this.lastId++;
        
        this.listeners.push({
            id: id,
            callback: callback,
            type: type
        });
        
        this.updateEventListeners();
        return id;
    }
    
    unsubscribe(idToRemove) {
        this.listeners = this.listeners.filter(listener => listener.id !== idToRemove);
        this.updateEventListeners();
    }
    
    updateEventListeners() {
        if (this.listeners.length > 0 && !this.wasListening) {
            // Wasn't listening before, should be now --> start listening
            this.startListening();
        } else if (this.listeners.length === 0 && this.wasListening) {
            // Was listening, shouldn't anymore --> stop listening
            this.stopListening();
        }
    }
    
    startListening() {
        window.addEventListener('resize', this.debouncedListener, { passive: true });
        window.addEventListener('resize', this.throttledListener, { passive: true });
        this.wasListening = true;
    }
    
    stopListening() {
        window.removeEventListener('resize', this.debouncedListener, false);
        window.removeEventListener('resize', this.throttledListener, false);
        this.wasListening = false;
    }
    
    notifySubscribers(type) {
        let filtered = this.listeners.filter(listener => listener.type === type);
        
        if (filtered.length > 0) {
            filtered.forEach(listener => listener.callback());
        }
    }
}

let rl = new ResizeListener();

export class FefResizeListener {
    static subscribe(callback = () => {}, type = DEBOUNCED) {
        return rl.subscribe(callback, type);
    }

    static subscribeDebounced(callback = () => {}) {
        return rl.subscribe(callback, DEBOUNCED);
    }

    static subscribeThrottled(callback = () => {}) {
        return rl.subscribe(callback, THROTTLED);
    }

    static unsubscribe(idToRemove) {
        rl.unsubscribe(idToRemove);
    }
}
{'fraction_non_alphanumeric': 0.12308610061642473, 'fraction_numerical': 0.003976933784052496, 'mean_word_length': 2.4452054794520546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4256317', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1456, 'n_words': 275}