<commit_before>import createApp from 'dojo-app/createApp';
import { AnyAction } from 'dojo-actions/createAction';
import createRoute from 'dojo-routing/createRoute';
import createRouter from 'dojo-routing/createRouter';
import createHashHistory from 'dojo-routing/history/createHashHistory';
import createPanel from 'dojo-widgets/createPanel';
import createTextInput from 'dojo-widgets/createTextInput';
import createWidget from 'dojo-widgets/createWidget';
import createMemoryStore from './utils/createLocalMemoryStore';

import * as storeTodoActions from './actions/storeTodoActions';
import * as uiTodoActions from './actions/uiTodoActions';
import todoRegistryFactory from './registry/createTodoRegistry';
import createTodoList from './widgets/createTodoList';
import createTodoFooter from './widgets/createTodoFooter';

const router = createRouter();
const history = createHashHistory();

history.on('change', (event) => {
	router.dispatch({}, event.value);
});

router.append(createRoute({
	path: '/completed',
	exec (request) {
		uiTodoActions.filter.do({ 'filter': 'completed' });
	}
}));

router.append(createRoute({
	path: '/all',
	exec (request) {
		uiTodoActions.filter.do({ 'filter': 'none' });
	}
}));

router.append(createRoute({
	path: '/active',
	exec (request) {
		uiTodoActions.filter.do({ 'filter': 'active' });
	}
}));

const todoStore = createMemoryStore({
	data: [
		{id: 'todo1', label: 'My First Todo!', completed: false}
	]
});

const widgetStore = createMemoryStore({
	data: [
		{
			id: 'title',
			label: 'todos'
		},
		{
			id: 'new-todo',
			classes: ['new-todo'],
			placeholder: 'What needs to be done?'
		},
		{
			id: 'main-section',
			classes: ['main']
		},
		{
			id: 'todo-list',
			classes: ['todo-list'],
			children: []
		},
		{
			id: 'todo-footer',
			classes: ['footer'],
			completedCount: 0,
			activeCount: 0,
			activeFilter: 'all'
		}
	]
});

todoStore.observe().subscribe((options: any) => {
	todoStore.get().then((items: any) => {
		const todos: any = Array.from(items);
		const completedCount = todos.filter((todo: any) => todo.completed).length;
		const activeCount = todos.filter((todo: any) => !todo.completed).length;
		widgetStore.patch({id: 'todo-footer', completedCount, activeCount});
	});
});

todoStore.observe().subscribe((options: any) => {
	const { deleted, item } = options;
	if (deleted) {
		widgetStore.get('todo-list').then((todoList: any) => {
			const children = todoList.children.filter((id: string) => id !== item);
			return widgetStore.delete(item).patch({id: todoList.id, children});
		});
	} else {
		widgetStore.get('todo-list').then((todoList: any) => {
			const { id, children } = todoList;
			function put() {
				return widgetStore
					.put(item)
					.patch({id, children: [...children, item.id]});
			}

			function patch() {
				return widgetStore
					.patch(item)
					.patch({id, children});
			}

			return children.includes(item.id) ? patch() : put();<commit_msg>use todo store as source of truth<commit_after>import createApp from 'dojo-app/createApp';
import { AnyAction } from 'dojo-actions/createAction';
import createRoute from 'dojo-routing/createRoute';
import createRouter from 'dojo-routing/createRouter';
import createHashHistory from 'dojo-routing/history/createHashHistory';
import createPanel from 'dojo-widgets/createPanel';
import createTextInput from 'dojo-widgets/createTextInput';
import createWidget from 'dojo-widgets/createWidget';
import createMemoryStore from './utils/createLocalMemoryStore';

import * as storeTodoActions from './actions/storeTodoActions';
import * as uiTodoActions from './actions/uiTodoActions';
import todoRegistryFactory from './registry/createTodoRegistry';
import createTodoList from './widgets/createTodoList';
import createTodoFooter from './widgets/createTodoFooter';

const router = createRouter();
const history = createHashHistory();

history.on('change', (event) => {
	router.dispatch({}, event.value);
});

router.append(createRoute({
	path: '/completed',
	exec (request) {
		uiTodoActions.filter.do({ 'filter': 'completed' });
	}
}));

router.append(createRoute({
	path: '/all',
	exec (request) {
		uiTodoActions.filter.do({ 'filter': 'none' });
	}
}));

router.append(createRoute({
	path: '/active',
	exec (request) {
		uiTodoActions.filter.do({ 'filter': 'active' });
	}
}));

const todoStore = createMemoryStore({
	data: [
		{id: 'todo1', label: 'My First Todo!', completed: false}
	]
});

const widgetStore = createMemoryStore({
	data: [
		{
			id: 'title',
			label: 'todos'
		},
		{
			id: 'new-todo',
			classes: ['new-todo'],
			placeholder: 'What needs to be done?'
		},
		{
			id: 'main-section',
			classes: ['main']
		},
		{
			id: 'todo-list',
			classes: ['todo-list'],
			children: []
		},
		{
			id: 'todo-footer',
			classes: ['footer'],
			completedCount: 0,
			activeCount: 0,
			activeFilter: 'all'
		}
	]
});

todoStore.observe().subscribe((options: any) => {
	todoStore.get().then((items: any) => {
		const todos: any = Array.from(items);
		const completedCount = todos.filter((todo: any) => todo.completed).length;
		const activeCount = todos.filter((todo: any) => !todo.completed).length;
		widgetStore.patch({id: 'todo-footer', completedCount, activeCount});
	});
});

todoStore.observe().subscribe((options: any) => {
	const { deleted, item } = options;
	if (deleted) {
		todoStore.get().then((todoItems: any) => {
			const children = Array.from(todoItems)
				.filter((todoItem: any) => todoItem.id !== item)
				.map((item: any) => item.id);
			return widgetStore.delete(item).patch({ id: 'todo-list', children });
		});
	} else {
		widgetStore.get('todo-list').then((todoList: any) => {
			const { id, children } = todoList;
			function put() {
				return widgetStore
					.put(item)
					.patch({id, children: [...children, item.id]});
			}

			function patch() {
				return widgetStore
					.patch(item)
					.patch({id, children});
			}

			return children.includes(item.id) ? patch() : put();
{'fraction_non_alphanumeric': 0.1729159691998661, 'fraction_numerical': 0.001004352192835621, 'mean_word_length': 4.344364937388193, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '559254', 'n_tokens_mistral': 2179, 'n_tokens_neox': 1976, 'n_words': 430}