tasmania
v0.2.0
Published
A typed action-based state machine
Downloads
1
Readme
tasmania
A strongly typed state machine
Install
yarn add tasmania
API
tasmania
exports the following members:
import {
createReducer,
State,
Transitions
} from 'tasmania';
createReducer<T, D>(transitions: Transitions<T, D>): Reducer<T, D>
It's the single factory function for creating state reducers, it takes a single argument which defines the state transitions.
T
represents a Transitions definition andD
may be any type of data carried over.
createReducer<
{
A: 'B'
B: 'END'
END: null
},
{ ended: boolean }
>({
A: () => {
return { type: 'B', data: { ended: false } }
},
B: () => {
return { type: 'END', data: { ended: true } }
},
END: endState,
})
State<T, D>
(type)
A structure reprenseting the machine state, with the signature { type: keyof T, data: D }
.
T
represents a Transitions definition andD
may be any type of data carried over.
Transitions<T, D>
(type)
A structure representing the machine transitions definition, with the signature { [K: keyof T]: (data: D) => { type: keyof T, data: D } | void }
.
T
represents a Transitions definition andD
may be any type of data carried over.
Usage
Here's a throughout usage example.
Setup
First we need to import the createReducer
function from this library.
import { createReducer } from 'tasmania';
Simple state machine
Let's now create a simple but complete state machine that will track hypothetical matter state transitions.
import { createReducer } from 'tasmania';
enum State {
SOLID = 'SOLID',
LIQUID = 'LIQUID',
GAS = 'GAS',
ETHEREAL = 'ETHEREAL',
}
function run() {
const reduce = createReducer<
{
[State.SOLID]: Exclude<State, State.SOLID>;
[State.LIQUID]: Exclude<State, State.LIQUID>;
[State.GAS]: Exclude<State, State.GAS>;
[State.ETHEREAL]: null; // Halts here
},
State[]
>({
[State.SOLID]: (data) => {
if (Math.random() > 0.95) {
return {
type: State.ETHEREAL,
data: [...data, State.ETHEREAL],
};
}
if (Math.random() > 0.5) {
return {
type: State.LIQUID,
data: [...data, State.LIQUID],
};
}
return {
type: State.GAS,
data: [...data, State.GAS],
};
},
[State.LIQUID]: (data) => {
if (Math.random() > 0.95) {
return {
type: State.ETHEREAL,
data: [...data, State.ETHEREAL],
};
}
if (Math.random() > 0.5) {
return {
type: State.GAS,
data: [...data, State.GAS],
};
}
return {
type: State.SOLID,
data: [...data, State.SOLID],
};
},
[State.GAS]: (data) => {
if (Math.random() > 0.95) {
return {
type: State.ETHEREAL,
data: [...data, State.ETHEREAL],
};
}
if (Math.random() > 0.5) {
return {
type: State.LIQUID,
data: [...data, State.LIQUID],
};
}
return {
type: State.SOLID,
data: [...data, State.SOLID],
};
},
[State.ETHEREAL]: () => {
return;
},
});
console.log(
reduce({
type: Object.values(State)[
Math.floor(Math.random() * (Object.keys(State).length - 1))
],
data: [],
}),
);
}
run();