<commit_before>import ids from './ids';
import globals from './globals';
import { StoreError } from './error';
const build2 = (actions, dispatcher) => {
	const _dispatcher = dispatcher || globals.get('defaultDispatcher');
	const _id = ids.nextStoreId();
	const _history = [];
	let _undoHistory = [];
	let _loading = false;
	let _key = null;

	class Store {
		constructor(initialStateValue) {
			this._componentListeners = {};
			_history.push({
				action: null,
				state: {
					value: initialStateValue,
					error: null
				}
			});

			this.isStore = true;

			for(const action in actions) {
				_dispatcher.on(
					this,
					action,
					actions[action].dependencies || []
				);
			}
		}

		listen(componentKey, cb) {
			if(!this._componentListeners.hasOwnProperty(componentKey)) {
				this._componentListeners[componentKey] = cb;
			}
			else {
				throw new Error(
					`component (key=${componentKey}) is already listening to the store ` +
					`(id=${_id}, key=${_key})`
				);
			}
		}

		connectToState(componentKey, setState) {
			this.listen(componentKey, (resolve, reject) => {
				const key = this.key() || this.id();
				const newState = {};
				newState[key] = this.all();
				setState(newState, resolve);
			});
			return this.all();
		}

		ignore(componentKey) {
			if(this._componentListeners.hasOwnProperty(componentKey)) {
				delete this._componentListeners[componentKey];
			}
		}

		handleAction(action, data) {
			if(!actions[action]) {
				return Promise.reject(new StoreError({
					message: `Store "${this.key()}" does not have an action "${action}"`
				}));
			}
			if(!actions[action].run) {
				const message = (
					`Store "${this.key()}" does not have a run method for action "${action}"`
				);
				return Promise.reject(new StoreError({ message, recoverable: false }));
			}
			const historyAction = { name: action, data };
			const p = new Promise((resolve, reject) => {
				actions[action].run.call(this, resolve, reject, data);
			});
			return p.then((result) => {
				if(result !== this.value()) {
					_history.push({
						action: historyAction,
						state: {
							value: result,
							error: null
						}
					});
					this.change(action);
					_undoHistory = [];
				}
			})
			.catch((error) => {
				let recoverable = false;
				if(error instanceof StoreError) {
					recoverable = error.recoverable;
				}
				else if(error.message && !(error instanceof Error)) {
					error = new StoreError(error);
					recoverable = error.recoverable;
				}

				const currentState = this.state();
				_history.push({
					action: historyAction,
					state: {
						value: currentState.value,
						error: error
					}
				});
				this.change(action);
				if(!recoverable) {
					throw error;
				}
				_undoHistory = [];
			});
		}

		change(action) {
			const keys = Object.keys(this._componentListeners);
			return Promise.all(
				keys.map(
					(key) => new Promise(
						(resolve, reject) => this._componentListeners[key].call(
							this,
							resolve,
							reject,
							{ action }
						)
					)
				)
			);
		}

		state() {
			return _history.length ? _history[_history.length - 1].state : undefined;
		}

		value() {
			return this.state() ? this.state().value : undefined;
		}

		setValue(newValue, newError, action) {
			action = action || 'setValue';
			_history.push({
				action,
				state: {
					value: newValue,
					error: newError
				}
			});
			this.change(action);
		}

		loading() {
			return _loading;
		}

		error() {
			return this.state() ? this.state().error : undefined;
		}

		all() {
			return {
				value: this.value(),
				loading: this.loading(),
				error: this.error()
			};
		}

		setKey(key) {
			_key = key;
		}

		key() {
			return _key;
		}

		id() {
			return _id;
		}

		history() {
			return _history;<commit_msg>Add ability to set loading status<commit_after>import ids from './ids';
import globals from './globals';
import { StoreError } from './error';
const build2 = (actions, dispatcher) => {
	const _dispatcher = dispatcher || globals.get('defaultDispatcher');
	const _id = ids.nextStoreId();
	const _history = [];
	let _undoHistory = [];
	let _loading = false;
	let _key = null;

	class Store {
		constructor(initialStateValue) {
			this._componentListeners = {};
			_history.push({
				action: null,
				state: {
					value: initialStateValue,
					error: null
				}
			});

			this.isStore = true;

			for(const action in actions) {
				_dispatcher.on(
					this,
					action,
					actions[action].dependencies || []
				);
			}
		}

		listen(componentKey, cb) {
			if(!this._componentListeners.hasOwnProperty(componentKey)) {
				this._componentListeners[componentKey] = cb;
			}
			else {
				throw new Error(
					`component (key=${componentKey}) is already listening to the store ` +
					`(id=${_id}, key=${_key})`
				);
			}
		}

		connectToState(componentKey, setState) {
			this.listen(componentKey, (resolve, reject) => {
				const key = this.key() || this.id();
				const newState = {};
				newState[key] = this.all();
				setState(newState, resolve);
			});
			return this.all();
		}

		ignore(componentKey) {
			if(this._componentListeners.hasOwnProperty(componentKey)) {
				delete this._componentListeners[componentKey];
			}
		}

		handleAction(action, data) {
			if(!actions[action]) {
				return Promise.reject(new StoreError({
					message: `Store "${this.key()}" does not have an action "${action}"`
				}));
			}
			if(!actions[action].run) {
				const message = (
					`Store "${this.key()}" does not have a run method for action "${action}"`
				);
				return Promise.reject(new StoreError({ message, recoverable: false }));
			}
			const historyAction = { name: action, data };
			const p = new Promise((resolve, reject) => {
				actions[action].run.call(this, resolve, reject, data);
			});
			return p.then((result) => {
				if(result !== this.value()) {
					_history.push({
						action: historyAction,
						state: {
							value: result,
							error: null
						}
					});
					this.change(action);
					_undoHistory = [];
				}
			})
			.catch((error) => {
				let recoverable = false;
				if(error instanceof StoreError) {
					recoverable = error.recoverable;
				}
				else if(error.message && !(error instanceof Error)) {
					error = new StoreError(error);
					recoverable = error.recoverable;
				}

				const currentState = this.state();
				_history.push({
					action: historyAction,
					state: {
						value: currentState.value,
						error: error
					}
				});
				this.change(action);
				if(!recoverable) {
					throw error;
				}
				_undoHistory = [];
			});
		}

		change(action) {
			const keys = Object.keys(this._componentListeners);
			return Promise.all(
				keys.map(
					(key) => new Promise(
						(resolve, reject) => this._componentListeners[key].call(
							this,
							resolve,
							reject,
							{ action }
						)
					)
				)
			);
		}

		state() {
			return _history.length ? _history[_history.length - 1].state : undefined;
		}

		value() {
			return this.state() ? this.state().value : undefined;
		}

		setValue(newValue, newError, action) {
			action = action || 'setValue';
			_history.push({
				action,
				state: {
					value: newValue,
					error: newError
				}
			});
			this.change(action);
		}

		loading() {
			return _loading;
		}

		setLoading(value) {
			_loading = value;
		}

		error() {
			return this.state() ? this.state().error : undefined;
		}

		all() {
			return {
				value: this.value(),
				loading: this.loading(),
				error: this.error()
			};
		}

		setKey(key) {
			_key = key;
		}

		key() {
			return _key;
		}

		id() {
			return _id;
		}

		history() {
			return _history;
{'fraction_non_alphanumeric': 0.14932362122788762, 'fraction_numerical': 0.0005202913631633715, 'mean_word_length': 2.6030927835051547, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '5848090', 'n_tokens_mistral': 3291, 'n_tokens_neox': 2454, 'n_words': 533}