Mealy
finite-state machine whose output values are determined both by its current state and the current input
// services.js
// mocked data
export const todos = [
{
description: 'I am a todo'
},
{
description: 'I am another todo'
}
]
export const getTodos = filter => data =>
new Promise((resolve, reject) => {
// get is an abstraction that could be in a http.js file
const result = await get(filter || 'notCompleted')
if (result.status === 200) {
// MUTATION IS GOING TO HAPPEN INSIDE THE STATE MACHINE
resolve(result.data)
} else {
reject(new Error('Error while getting todo'))
}
})
export const postTodo = todo => data =>
new Promise((resolve, reject) => {
const { id } = todo
// post is an abstraction that could be in a http.js file
const result = await post(todo)
/**
* MUTATION IS GOING TO HAPPEN INSIDE THE STATE MACHINE
* If the todo is saved, let's change the data inside state machine
* and return it to the state machine in order to be saved
*/
if (result.status === 201) {
data.push(result.data)
resolve(data)
} else {
reject(new Error('Error while saving todo'))
}
})
export const putTodo = todo => data =>
new Promise((resolve, reject) => {
const { id } = todo
// put is an abstraction that could be in a http.js file
const result = await put(todo)
if (result.status === 200) {
/**
* MUTATION IS GOING TO HAPPEN INSIDE THE STATE MACHINE
* If the todo is changed, let's change the data inside state machine
* and return it to the state machine in order to be saved
*/
data[id] = result.data
resolve(data)
} else {
reject(new Error('Error while changing todo'))
}
})Last updated
Was this helpful?