<commit_before>// LICENSE : MIT
"use strict";
import {EventEmitter} from "events"
import ActionEmitter from "./ActionEmitter";
import assert from "assert";
const STORE_GROUPS_KEY = "__CHANGE__";
const DISPATCH_AFTER = "__DISPATCH_AFTER__";
export default class StoreGroups extends EventEmitter {
    constructor(dispatcher) {
        super();
        // assert
        assert(dispatcher !== undefined, "should initialize with ActionEmitter");
        assert(dispatcher instanceof ActionEmitter, "should be instance of ActionEmitter");
        assert(typeof this.getStores === "function", "should implement `getStores(): ReduceStore[].");
        this.dispatcher = dispatcher;
        this.dispatcher.onAction(this.dispatch.bind(this));
        this.stores = this.getStores();
        this._setupOnChangeHandler();

    }

    _setupOnChangeHandler() {
        let isChanged = false;
        const onChange = () => {
            isChanged = true;
        };
        const afterChange = () => {
            if (isChanged) {
                this.emitChange();
            }
            isChanged = false;
        };
        this.stores.forEach(store => {
            store.onChange(onChange);
        });
        this.on(DISPATCH_AFTER, () => {
            afterChange();
        });
    }

    // some store change
    emitChange() {
        this.emit(STORE_GROUPS_KEY);
    }

    // must implement
    getStores() {
        assert("should implement `getStores(): ReduceStore[] in " + this.constructor.name);
    }

    getState() {
        assert("should implement `getState(): any in " + this.constructor.name);
    }

    onChange(changeHandler) {
        this.on(STORE_GROUPS_KEY, changeHandler);
        // unbind function
        return this.removeListener.bind(this, STORE_GROUPS_KEY, changeHandler);
    }

    dispatch(action) {
        this.stores.forEach(store => {
            var prevState = store.getState();
            var newState = store.reduce(prevState, action);
            if (prevState !== newState) {
                store.setState(newState);
            }<commit_msg>add comment<commit_after>// LICENSE : MIT
"use strict";
import {EventEmitter} from "events"
import ActionEmitter from "./ActionEmitter";
import assert from "assert";
const STORE_GROUPS_KEY = "__CHANGE__";
// TODO: should be public
const DISPATCH_BEFORE = "__DISPATCH_BEFORE__";
const DISPATCH_AFTER = "__DISPATCH_AFTER__";
export default class StoreGroups extends EventEmitter {
    constructor(dispatcher) {
        super();
        // assert
        assert(dispatcher !== undefined, "should initialize with ActionEmitter");
        assert(dispatcher instanceof ActionEmitter, "should be instance of ActionEmitter");
        assert(typeof this.getStores === "function", "should implement `getStores(): ReduceStore[].");
        this.dispatcher = dispatcher;
        this.dispatcher.onAction(this.dispatch.bind(this));
        this.stores = this.getStores();
        this._setupOnChangeHandler();

    }

    _setupOnChangeHandler() {
        let isChanged = false;
        const onChange = () => {
            isChanged = true;
        };
        const afterChange = () => {
            if (isChanged) {
                this.emitChange();
            }
            isChanged = false;
        };
        this.stores.forEach(store => {
            store.onChange(onChange);
        });
        this.on(DISPATCH_AFTER, () => {
            afterChange();
        });
    }

    // some store change
    emitChange() {
        this.emit(STORE_GROUPS_KEY);
    }

    // must implement
    getStores() {
        assert("should implement `getStores(): ReduceStore[] in " + this.constructor.name);
    }

    getState() {
        assert("should implement `getState(): any in " + this.constructor.name);
    }

    onChange(changeHandler) {
        this.on(STORE_GROUPS_KEY, changeHandler);
        // unbind function
        return this.removeListener.bind(this, STORE_GROUPS_KEY, changeHandler);
    }

    dispatch(action) {
        this.emit(DISPATCH_BEFORE);
        this.stores.forEach(store => {
            var prevState = store.getState();
            var newState = store.reduce(prevState, action);
            if (prevState !== newState) {
                store.setState(newState);
            }
{'fraction_non_alphanumeric': 0.11960830030310096, 'fraction_numerical': 0.0, 'mean_word_length': 2.30763299922899, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '777544', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1195, 'n_words': 251}