<commit_before>app.core.view = function() {
	'use strict';
	
	var viewCache = {};
	var self = this;
	
	// Default settings
	if (!app.config.viewEngine) app.config.viewEngine = {};
	if (!app.config.viewEngine.defaultSelector) app.config.viewEngine.defaultSelector = '#main';
	if (!app.config.viewEngine.fileExtension) app.config.viewEngine.fileExtension = 'html';

	/**
	 * Load the required partials
	 * @param Array partials
	 * @param Function callback
	 */
	var loadPartials = function(partials, callback) {
		if (!partials) {
			return callback();
		}
	
		var loadFunctions = [];
	
		for (var i in partials) {
			loadFunctions.push(function(parallelCallback) {
				// Is the view cached?
				if (viewCache[partials[i]]) {
					app.core.log.debug('Registered partial from cache [' + i + ' => ' + partials[i] + ']', 'Zeus/View');
					Handlebars.registerPartial(i, viewCache[partials[i]]);
		
					return parallelCallback();
				}
		
				// Load the view file
				$.get(app.config.path + '/app/views/' + partials[i] + '.html', function(source) {
					app.core.log.debug('Registered partial [' + i + ' => ' + partials[i] + ']', 'Zeus/View');
					viewCache[partials[i]] = Handlebars.compile(source);
					Handlebars.registerPartial(i, viewCache[partials[i]]);
					parallelCallback();
				});
			});
		};
	
		// Load the above functions in parallel
		async.parallelLimit(loadFunctions, app.config.viewEngine.parallelLimit, callback);
	};
	
	/**
	 * Compile and output or pass along the template to the callback
	 * @param String source
	 * @param Array data
	 * @param Mixed callback CSS selector or callback
	 * @param Function thenCallback
	 */
	var compileView = function(file, source, data, selector, thenCallback) {
		if (typeof source === 'string') {
			app.core.log.debug('Compiled view [' + file + ']', 'Zeus/View');
			viewCache[file] = Handlebars.compile(source);
		}

		if (!data) var data = { partials: false };
	
		loadPartials(data.partials, function() {
			// Remove the partial options before passing the data to the view
			delete data.partials;
	
			var template = viewCache[file](data);
	
			if (typeof selector === 'string') {
				$(selector).html(template);
				app.core.router.assignEvents(selector);
				if (thenCallback) thenCallback(selector);
			} else {
				selector(template);
			}
		});
	};
	
	/**
	 * Render a view template
	 * @param String file
	 * @param Array data
	 * @param Function callback
	 * @param Function thenCallback
	 */
	this.render = function(file, data, selector, thenCallback) {
		if (!selector) var selector = app.config.viewEngine.defaultSelector;
		
		if (viewCache[file]) {
			app.core.log.debug('Loaded view [' + file + '] from cache', 'Zeus/View');
			compileView(file, viewCache[file], data, selector, thenCallback);
		} else {
			$.get(app.config.path + '/app/views/' + file + '.' + app.config.viewEngine.fileExtension, function(source) {
				app.core.log.debug('Loaded view [' + file + ']', 'Zeus/View');
				compileView(file, source, data, selector, thenCallback);
			});
		}
	};

	/**
	 * Preload and cache compiled view files
	 */
	app.core.hooks.on('preloadAssets', function(data, next) {
		var preloadFunctions = [];
	
		// Loop through all requested files
		app.config.preloadViews.forEach(function(file) {
			// Add a loading function to the preloadFunctions array
			preloadFunctions.push(function(callback) {
				// Load the view html file
				$.get(app.config.path + '/app/views/' + file + '.' + app.config.viewEngine.fileExtension, function(source) {
					app.core.log.debug('Preloaded view [' + file + ']', 'Zeus/View');
	
					// Compile and cache the view
					compileView(file, source, false, function() {
						callback();
					});
				});
			});
		});
	<commit_msg>Fixed bug in view preloading<commit_after>app.core.view = function() {
	'use strict';
	
	var viewCache = {};
	var self = this;
	
	// Default settings
	if (!app.config.viewEngine) app.config.viewEngine = {};
	if (!app.config.viewEngine.defaultSelector) app.config.viewEngine.defaultSelector = '#main';
	if (!app.config.viewEngine.fileExtension) app.config.viewEngine.fileExtension = 'html';

	/**
	 * Load the required partials
	 * @param Array partials
	 * @param Function callback
	 */
	var loadPartials = function(partials, callback) {
		if (!partials) {
			return callback();
		}
	
		var loadFunctions = [];
	
		for (var i in partials) {
			loadFunctions.push(function(parallelCallback) {
				// Is the view cached?
				if (viewCache[partials[i]]) {
					app.core.log.debug('Registered partial from cache [' + i + ' => ' + partials[i] + ']', 'Zeus/View');
					Handlebars.registerPartial(i, viewCache[partials[i]]);
		
					return parallelCallback();
				}
		
				// Load the view file
				$.get(app.config.path + '/app/views/' + partials[i] + '.html', function(source) {
					app.core.log.debug('Registered partial [' + i + ' => ' + partials[i] + ']', 'Zeus/View');
					viewCache[partials[i]] = Handlebars.compile(source);
					Handlebars.registerPartial(i, viewCache[partials[i]]);
					parallelCallback();
				});
			});
		};
	
		// Load the above functions in parallel
		async.parallelLimit(loadFunctions, app.config.viewEngine.parallelLimit, callback);
	};
	
	/**
	 * Compile and output or pass along the template to the callback
	 * @param String source
	 * @param Array data
	 * @param Mixed callback CSS selector or callback
	 * @param Function thenCallback
	 */
	var compileView = function(file, source, data, selector, thenCallback) {
		if (typeof source === 'string') {
			app.core.log.debug('Compiled view [' + file + ']', 'Zeus/View');
			viewCache[file] = Handlebars.compile(source);
		}

		if (!data) var data = { partials: false };
	
		loadPartials(data.partials, function() {
			// Remove the partial options before passing the data to the view
			delete data.partials;
	
			var template = viewCache[file](data);
	
			if (typeof selector === 'string') {
				$(selector).html(template);
				app.core.router.assignEvents(selector);
				if (thenCallback) thenCallback(selector);
			} else {
				selector(template);
			}
		});
	};
	
	/**
	 * Render a view template
	 * @param String file
	 * @param Array data
	 * @param Function callback
	 * @param Function thenCallback
	 */
	this.render = function(file, data, selector, thenCallback) {
		if (!selector) var selector = app.config.viewEngine.defaultSelector;
		
		if (viewCache[file]) {
			app.core.log.debug('Loaded view [' + file + '] from cache', 'Zeus/View');
			compileView(file, viewCache[file], data, selector, thenCallback);
		} else {
			$.get(app.config.path + '/app/views/' + file + '.' + app.config.viewEngine.fileExtension, function(source) {
				app.core.log.debug('Loaded view [' + file + ']', 'Zeus/View');
				compileView(file, source, data, selector, thenCallback);
			});
		}
	};

	/**
	 * Preload and cache compiled view files
	 */
	app.core.hooks.on('preloadAssets', function(data, next) {
		var preloadFunctions = [];
	
		// Loop through all requested files
		app.config.preloadViews.forEach(function(file) {
			// Add a loading function to the preloadFunctions array
			preloadFunctions.push(function(callback) {
				// Load the view html file
				$.get(app.config.path + '/app/views/' + file + '.' + app.config.viewEngine.fileExtension, function(source) {
					app.core.log.debug('Preloaded view [' + file + ']', 'Zeus/View');
	
					// Compile and cache the view
					viewCache[file] = Handlebars.compile(source);
					
					callback();
				});
			});
		});
	
{'fraction_non_alphanumeric': 0.15307620445749365, 'fraction_numerical': 0.0, 'mean_word_length': 3.4264619019492026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1826384', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2285, 'n_words': 554}