Skip to content

Commit

Permalink
Only update views on patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Feb 4, 2018
1 parent dbed27d commit b4f68e4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 65 deletions.
128 changes: 64 additions & 64 deletions examples/todo-mvc/src/store.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
import model from '../../../src';

export const SHOW_ALL = 'show_all';
export const SHOW_COMPLETED = 'show_completed';
export const SHOW_ACTIVE = 'show_active';

// const filterType = types.union(...[SHOW_ALL, SHOW_COMPLETED, SHOW_ACTIVE].map(types.literal))
const TODO_FILTERS = {
[SHOW_ALL]: () => true,
[SHOW_ACTIVE]: todo => !todo.completed,
[SHOW_COMPLETED]: todo => todo.completed,
};

const Todo = model('Todo', {
initial: () => ({
text: '',
completed: false,
id: 0,
}),
actions: self => ({
remove: () => self.getRoot().removeTodo(self),
edit: text => (self.text = text),
complete: () => (self.completed = !self.completed),
}),
});

const TodoStore = model('TodoStore', {
initial: () => ({
todos: [],
filter: SHOW_ALL,
}),
views: self => ({
completedCount: () =>
self.todos.reduce(
(count, todo) => (todo.completed ? count + 1 : count),
0
),
activeCount: () => self.todos.length - self.completedCount,
filteredTodos: () => self.todos.filter(TODO_FILTERS[self.filter]),
}),
actions: self => ({
addTodo: text =>
self.todos.unshift(
Todo({
text,
id:
self.todos.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) +
1,
})
),
removeTodo: todo => self.todos.splice(self.todos.indexOf(todo) >>> 0, 1),
completeAll: () => {
const areAllMarked = self.todos.every(todo => todo.completed);
self.todos.forEach(todo => (todo.completed = !areAllMarked));
},
clearCompleted: () =>
self.todos
.filter(todo => todo.completed)
.forEach(todo => self.removeTodo(todo)),
setFilter: filter => (self.filter = filter),
}),
});

export default TodoStore;
import model from '../../../src';

export const SHOW_ALL = 'show_all';
export const SHOW_COMPLETED = 'show_completed';
export const SHOW_ACTIVE = 'show_active';

// const filterType = types.union(...[SHOW_ALL, SHOW_COMPLETED, SHOW_ACTIVE].map(types.literal))
const TODO_FILTERS = {
[SHOW_ALL]: () => true,
[SHOW_ACTIVE]: todo => !todo.completed,
[SHOW_COMPLETED]: todo => todo.completed,
};

const Todo = model('Todo', {
initial: () => ({
text: '',
completed: false,
id: 0,
}),
actions: self => ({
remove: () => self.getRoot().removeTodo(self),
edit: text => (self.text = text),
complete: () => (self.completed = !self.completed),
}),
});

const TodoStore = model('TodoStore', {
initial: () => ({
todos: [],
filter: SHOW_ALL,
}),
views: self => ({
completedCount: () =>
self.todos.reduce(
(count, todo) => (todo.completed ? count + 1 : count),
0
),
activeCount: () => self.todos.length - self.completedCount,
filteredTodos: () => self.todos.filter(TODO_FILTERS[self.filter]),
}),
actions: self => ({
addTodo: text =>
self.todos.unshift(
Todo({
text,
id:
self.todos.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) +
1,
})
),
removeTodo: todo => self.todos.splice(self.todos.indexOf(todo) >>> 0, 1),
completeAll: () => {
const areAllMarked = self.todos.every(todo => todo.completed);
self.todos.forEach(todo => (todo.completed = !areAllMarked));
},
clearCompleted: () =>
self.todos
.filter(todo => todo.completed)
.forEach(todo => self.removeTodo(todo)),
setFilter: filter => (self.filter = filter),
}),
});

export default TodoStore;
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const model = (name, { initial, actions, views }) => {
}

// update the cache on change
emitter.on('*', () => {
emitter.on('patch', () => {
for (let key of keys) {
cache[key] = boundViews[key]();
}
Expand Down

0 comments on commit b4f68e4

Please sign in to comment.