
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Container for mealy state machines
From idle
to fetching
by using fetch data
action.
// Just pass the new state as a handler for an action
Machine.create('app', {
'idle': {
'fetch data': 'fetching'
}
});
// Return a string in the handler
Machine.create('app', {
'idle': {
'fetch data': function () {
return 'fetching';
}
}
});
// Return a state object
Machine.create('app', {
'idle': {
'fetch data': function () {
return { name: 'fetching' };
}
}
});
// Yield a string in the handler's generator
Machine.create('app', {
'idle': {
'fetch data': function * () {
yield 'fetching';
// or you can yield a state object
// `yield { name: 'fetching' }`
}
}
});
For the cases where you want to do something as a result of two (or more) actions. From idle
state to done
when fetching in progress
and success
(or fail
) actions are dispatched.
Machine.create('app', {
'idle': {
'fetching in progress': function * () {
const [ data, error ] = yield wait(['success', 'fail']);
// or just `const data = yield wait('success')`
// if we are interested only in one action
return data ? { name: 'done', data } : { name: 'done', error };
}
}
});
This example is a bit silly. We'll probably go with a separate state when data fetching is in progress.
import { Machine } from 'mealy';
const machine = Machine.create('app', {
state: { name: 'idle', todos: [] },
transitions: {
'idle': {
'add new todo': function ({ todos }, todo) {
todos.push(todo);
return { todos };
},
'delete todo': function ({ todos }, index) {
return { todos: todos.splice(index, 1) };
},
'fetch todos': function * () {
yield 'fetching';
try {
const todos = yield call(getTodos, '/api/todos');
} catch (error) {
return { name: 'fetching failed', error };
}
return { name: 'idle', todos };
}
},
'fetching failed': {
'fetch todos': function * () {
yield { name: 'idle', error: null };
this.fetchTodos();
}
}
}
});
machine.fetchTodos();
import React from 'react';
import { connect } from 'mealy/react';
class TodoList extends React.Component {
render() {
const { todos, error, isFetching, fetchTodos, deleteTodo } = this.props;
if (isFetching()) return <p>Loading</p>;
if (error) return (
<div>
Error fetching todos: { error }<br />
<button onClick={ fetchTodos }>try again</button>
</div>
);
return (
<ul>
{ todos.map(({ text}) => <li onClick={ deleteTodo }>{ text }</li>) }
</ul>
);
}
}
export default connect(TodoList)
.with('app')
.map(({ state, isFetching, fetchTodos, deleteTodo } => {
todos: state.todos,
error: state.error,
isFetching,
fetchTodos,
deleteTodo
}));
FAQs
Stent is combining the ideas of redux with the concept of state machines
The npm package stent receives a total of 1,736 weekly downloads. As such, stent popularity was classified as popular.
We found that stent demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.