<commit_before>define([
    'bluebird',
    'kb/common/router',
    'kb/common/lang'
], function (Promise, Router, lang) {
    function factory(config) {
        var runtime = config.runtime,
            router = Router.make(config);

        function doRoute() {
            var handler;
            try {
                handler = router.findCurrentRoute();
            } catch (ex) {
                console.error(ex);
                if (ex instanceof Router.NotFoundException) {
                    runtime.send('app', 'route-not-found', ex);
                    return;
                } else {
                    throw ex;
                }
            }
            if (handler.route.authorization) {
                if (!runtime.getService('session').isLoggedIn()) {
                    var loginParams = {};
                    if (handler.request.path) {
                        loginParams.nextrequest = JSON.stringify(handler.request);
                    }
                    runtime.send('app', 'navigate', {
                        path: 'login',
                        // TODO: path needs to be the path + params
                        params: loginParams
                    });
                    return;
                }
            }
            var route = {
                routeHandler: handler
            };
            if (handler.route.redirect) {
                runtime.send('app', 'route-redirect', route);
            } else if (handler.route.widget) {
                runtime.send('app', 'route-widget', route);
            } else if (handler.route.handler) {
                runtime.send('app', 'route-handler', route);
            }
            //runtime.send('app', 'new-route', {
            //    routeHandler: handler
            //});
        }
        function installRoute(route) {
            return Promise.try(function () {
                if (route.widget) {
                    // ADD ROUTE WIDGET HERE...
                    router.addRoute(route);
                    return true;
//                    router.addRoute({
//                        path: route.path,
//                        queryParams: route.queryParams,
//                        config: {
//                        },
//                        widget: route.widget
//                    });
                } else if (route.redirectHandler) {
                    router.addRoute(route);
                    return true;
                } else {
                    throw new lang.UIError({
                        type: 'ConfiguratonError',
                        name: 'RouterConfigurationError',
                        source: 'installRoute',
                        message: 'invalid route',
                        suggestion: 'Fix the plugin which specified this route.',
                        data: route
                    });
                }
            });
        }
        function installRoutes(routes) {
            if (!routes) {
                return;
            }
            return routes.map(function (route) {
                return installRoute(route);
            });
        }
        function pluginHandler(pluginConfig) {
            return installRoutes(pluginConfig);
        }

        runtime.recv('app', 'do-route', function () {
            doRoute();
        });


        runtime.recv('app', 'new-route', function (data) {
            if (data.routeHandler.route.redirect) {
                send('app', 'route-redirect', data);
            } else if (data.routeHandler.route.widget) {
                send('app', 'route-widget', data);
            } else if (data.routeHandler.route.handler) {
                send('app', 'route-handler', data);
            }
        });

        runtime.recv('app', 'route-redirect', function (data) {
            runtime.send('app', 'navigate', {
                path: data.routeHandler.route.redirect.path,
                params: data.routeHandler.route.redirect.params
            });
        });
        
        //runtime.recv('app', 'route-handler', function (data) {
        //    
        //})

        runtime.recv('app', 'navigate', function (data) {
            router.navigateTo(data);
        });

        runtime.recv('app', 'redirect', function (data) {
            router.redirectTo(data.url, data.new_window);
        });

        window.addEventListener('hashchange', function (e) {
            // $(window).on('hashchange', function () {
            // NB this is called AFTER it has changed. The browser will do nothing by
            // default
            doRoute();
        });


        return {
            pluginHandler: pluginHandler
        };
    }
    return {
        make: function (config) {
            return factory(config);
        }
    };
});<commit_msg>add start/stop methods to router to avoid warning, and move event  registration and unregistration into them.<commit_after>define([
    'bluebird',
    'kb/common/router',
    'kb/common/lang'
], function (Promise, Router, lang) {
    function factory(config) {
        var runtime = config.runtime,
            router = Router.make(config),
            receivers = [],
            eventListeners = [];

        function doRoute() {
            var handler;
            try {
                handler = router.findCurrentRoute();
            } catch (ex) {
                console.error(ex);
                if (ex instanceof Router.NotFoundException) {
                    runtime.send('app', 'route-not-found', ex);
                    return;
                } else {
                    throw ex;
                }
            }
            if (handler.route.authorization) {
                if (!runtime.getService('session').isLoggedIn()) {
                    var loginParams = {};
                    if (handler.request.path) {
                        loginParams.nextrequest = JSON.stringify(handler.request);
                    }
                    runtime.send('app', 'navigate', {
                        path: 'login',
                        // TODO: path needs to be the path + params
                        params: loginParams
                    });
                    return;
                }
            }
            var route = {
                routeHandler: handler
            };
            if (handler.route.redirect) {
                runtime.send('app', 'route-redirect', route);
            } else if (handler.route.widget) {
                runtime.send('app', 'route-widget', route);
            } else if (handler.route.handler) {
                runtime.send('app', 'route-handler', route);
            }
            //runtime.send('app', 'new-route', {
            //    routeHandler: handler
            //});
        }
        function installRoute(route) {
            return Promise.try(function () {
                if (route.widget) {
                    // ADD ROUTE WIDGET HERE...
                    router.addRoute(route);
                    return true;
//                    router.addRoute({
//                        path: route.path,
//                        queryParams: route.queryParams,
//                        config: {
//                        },
//                        widget: route.widget
//                    });
                } else if (route.redirectHandler) {
                    router.addRoute(route);
                    return true;
                } else {
                    throw new lang.UIError({
                        type: 'ConfiguratonError',
                        name: 'RouterConfigurationError',
                        source: 'installRoute',
                        message: 'invalid route',
                        suggestion: 'Fix the plugin which specified this route.',
                        data: route
                    });
                }
            });
        }
        function installRoutes(routes) {
            if (!routes) {
                return;
            }
            return routes.map(function (route) {
                return installRoute(route);
            });
        }
        function pluginHandler(pluginConfig) {
            return installRoutes(pluginConfig);
        }

        function start() {
            runtime.recv('app', 'do-route', function () {
                doRoute();
            });

            runtime.recv('app', 'new-route', function (data) {
                if (data.routeHandler.route.redirect) {
                    send('app', 'route-redirect', data);
                } else if (data.routeHandler.route.widget) {
                    send('app', 'route-widget', data);
                } else if (data.routeHandler.route.handler) {
                    send('app', 'route-handler', data);
                }
            });

            runtime.recv('app', 'route-redirect', function (data) {
                runtime.send('app', 'navigate', {
                    path: data.routeHandler.route.redirect.path,
                    params: data.routeHandler.route.redirect.params
                });
            });

            runtime.recv('app', 'navigate', function (data) {
                router.navigateTo(data);
            });

            runtime.recv('app', 'redirect', function (data) {
                router.redirectTo(data.url, data.new_window);
            });

            eventListeners.push({
                target: window,
                type: 'hashchange',
                listener: function (e) {
                    // $(window).on('hashchange', function () {
                    // NB this is called AFTER it has changed. The browser will do nothing by
                    // default
                    doRoute();
                }
            });
            eventListeners.forEach(function(listener) {
                listener.target.addEventListener(listener.type, listener.listener);
            });
        }

        function stop() {
            receivers.forEach(function (receiver) {
                if (receiver) {
                    runtime.drop(receiver);
                }
            });
            eventListeners.forEach(function (listener) {
                listener.target.removeEventListener(listener.type, listener.listener);
            });
        }

        return {
            pluginHandler: pluginHandler,
            start: start,
            stop: stop
        };
    }
    return {
        make: function (config) {
            return factory(config);
        }
    };
});
{'fraction_non_alphanumeric': 0.11405936813973466, 'fraction_numerical': 0.0, 'mean_word_length': 1.1031714171015656, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '919558', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2349, 'n_words': 498}