<commit_before>"use strict";

import Q from "q";

export default function PluginBroker() {
    var handlersByType = {
        startup: [],
        accountAvailable: []
    };

    function chainAndRunHandlers(handlers) {
        var chainPromise = (prevPromise, handler) => {
            return prevPromise.then((prevValue) => Q.when(handler(prevValue)));
        };

        handlers.reduce(chainPromise, Q.resolve())
            .catch(e => console.error(e))
            .done();
    }

    this.register = (type, handler) => {
        handlersByType[type].push(handler);
        return this;
    };

    // HOOKS //////////////////////

    this.onStartup = (isAccountAvailable) => {
        var handlers = handlersByType.startup;
        var accountAvailableHandlers = handlersByType.accountAvailable;

        // If user account exists, `accountAvailable` was already published by account service
        // So we need to add these handlers to the startup.
        if (isAccountAvailable) {
            handlers = handlers.concat(accountAvailableHandlers);
        }

        chainAndRunHandlers(handlers);
    };

    this.onAccountAvailable = () => chainAndRunHandlers(handlersByType.accountAvailable);
}

<commit_msg>New plugin broker hook for `renderUI`.<commit_after>"use strict";

import Q from "q";

export default function PluginBroker() {
    var handlersByType = {
        startup: [],
        accountAvailable: [],
        renderUI: []
    };

    function chainAndRunHandlers(handlers) {
        var chainPromise = (prevPromise, handler) => {
            return prevPromise.then((prevValue) => Q.when(handler(prevValue)));
        };

        handlers.reduce(chainPromise, Q.resolve())
            .catch(e => console.error(e))
            .done();
    }

    this.register = (type, handler) => {
        handlersByType[type].push(handler);
        return this;
    };

    // HOOKS //////////////////////

    this.onStartup = (isAccountAvailable) => {
        var handlers = handlersByType.startup;
        var accountAvailableHandlers = handlersByType.accountAvailable;

        // If user account exists, `accountAvailable` was already published by account service
        // So we need to add these handlers to the startup.
        if (isAccountAvailable) {
            handlers = handlers.concat(accountAvailableHandlers);
        }

        chainAndRunHandlers(handlers);
    };

    this.onAccountAvailable = () => chainAndRunHandlers(handlersByType.accountAvailable);
    this.onRenderUI = () => chainAndRunHandlers(handlersByType.renderUI);
}


{'fraction_non_alphanumeric': 0.12718786464410736, 'fraction_numerical': 0.0, 'mean_word_length': 2.429333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '3350394', 'n_tokens_mistral': 709, 'n_tokens_neox': 679, 'n_words': 150}