<commit_before>// uses typings/angular

/// <reference path='../autosaveAction/autosaveAction.service.ts' />

module rl.utilities.services.autosave {
	'use strict';

	import __autosaveAction = rl.utilities.services.autosaveAction;

	export var moduleName: string = 'rl.utilities.services.autosave';
	export var factoryName: string = 'autosaveFactory';

	export interface IAutosaveService {
		autosave(...data: any[]): boolean;
		contentForm: ng.IFormController;
	}

	class AutosaveService implements IAutosaveService {
		private hasValidator: boolean;

		constructor(private autosaveService: __autosaveAction.IAutosaveActionService
				, private save: {(...data: any[]): ng.IPromise<void>}
				, public contentForm?: ng.IFormController
				, private validate?: {(): boolean}) {
			this.hasValidator = validate != null;

			if (this.contentForm == null) {
				this.contentForm = this.nullForm();
			}
		}

		autosave: { (...data: any[]): boolean } = (...data: any[]): boolean => {
			if (this.contentForm.$pristine) {
				return true;
			}

			var valid: boolean = true;
			if (this.hasValidator) {
				valid = this.validate();
				if (valid === undefined) {
					valid = true;
				}
			}

			if (valid) {
				var promise: ng.IPromise<void> = this.save(...data);

				if (!_.isUndefined(promise)) {
					this.autosaveService.trigger(this.save(...data).then((): void => {
						if (this.contentForm != null) {
							this.contentForm.$setPristine();
						}
					}));
				}

				return true;
			} else {
				return false;
			}
		}

		private nullForm(): ng.IFormController {
			return <any>{
				$pristine: false,
				$setPristine(): void {
					return;
				},
			};
		}
	}

	export interface IAutosaveServiceFactory {
		getInstance(save: {(): ng.IPromise<void>}, contentForm?: ng.IFormController, validate?: {(): boolean}): IAutosaveService;
	}

	autosaveServiceFactory.$inject = [__autosaveAction.serviceName];
	function autosaveServiceFactory(autosaveService: __autosaveAction.IAutosaveActionService): IAutosaveServiceFactory {
		'use strict';
		return {
			getInstance(save: { (): ng.IPromise<void> }, contentForm?: ng.IFormController, validate?: { (): boolean }): IAutosaveService {
				return new AutosaveService(autosaveService, save, contentForm, validate);
			}
		};
	}

	angular.module(moduleName, [__autosaveAction.moduleName])
		.factory(factoryName, autosaveServiceFactory);
}
<commit_msg>Should use the promise.<commit_after>// uses typings/angular

/// <reference path='../autosaveAction/autosaveAction.service.ts' />

module rl.utilities.services.autosave {
	'use strict';

	import __autosaveAction = rl.utilities.services.autosaveAction;

	export var moduleName: string = 'rl.utilities.services.autosave';
	export var factoryName: string = 'autosaveFactory';

	export interface IAutosaveService {
		autosave(...data: any[]): boolean;
		contentForm: ng.IFormController;
	}

	class AutosaveService implements IAutosaveService {
		private hasValidator: boolean;

		constructor(private autosaveService: __autosaveAction.IAutosaveActionService
				, private save: {(...data: any[]): ng.IPromise<void>}
				, public contentForm?: ng.IFormController
				, private validate?: {(): boolean}) {
			this.hasValidator = validate != null;

			if (this.contentForm == null) {
				this.contentForm = this.nullForm();
			}
		}

		autosave: { (...data: any[]): boolean } = (...data: any[]): boolean => {
			if (this.contentForm.$pristine) {
				return true;
			}

			var valid: boolean = true;
			if (this.hasValidator) {
				valid = this.validate();
				if (valid === undefined) {
					valid = true;
				}
			}

			if (valid) {
				var promise: ng.IPromise<void> = this.save(...data);

				if (!_.isUndefined(promise)) {
					this.autosaveService.trigger(promise.then((): void => {
						if (this.contentForm != null) {
							this.contentForm.$setPristine();
						}
					}));
				}

				return true;
			} else {
				return false;
			}
		}

		private nullForm(): ng.IFormController {
			return <any>{
				$pristine: false,
				$setPristine(): void {
					return;
				},
			};
		}
	}

	export interface IAutosaveServiceFactory {
		getInstance(save: {(): ng.IPromise<void>}, contentForm?: ng.IFormController, validate?: {(): boolean}): IAutosaveService;
	}

	autosaveServiceFactory.$inject = [__autosaveAction.serviceName];
	function autosaveServiceFactory(autosaveService: __autosaveAction.IAutosaveActionService): IAutosaveServiceFactory {
		'use strict';
		return {
			getInstance(save: { (): ng.IPromise<void> }, contentForm?: ng.IFormController, validate?: { (): boolean }): IAutosaveService {
				return new AutosaveService(autosaveService, save, contentForm, validate);
			}
		};
	}

	angular.module(moduleName, [__autosaveAction.moduleName])
		.factory(factoryName, autosaveServiceFactory);
}

{'fraction_non_alphanumeric': 0.1444050843925818, 'fraction_numerical': 0.0, 'mean_word_length': 3.738400789733465, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': '3390868', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1551, 'n_words': 299}