lucyc is a native command-line compiler, and the same compiler is built to WebAssembly for easy use in JavaScript projects. At run time, sending an event is a lookup and a function call.state enabled {
toggle => disabled
}
initial state disabled {
toggle => enabled
}
export default function createMachine({ context = {} } = {}) {
const listeners = new Set();
const states = {
enabled: {
toggle() {
machine.state = 'disabled';
}
},
disabled: {
toggle() {
machine.state = 'enabled';
}
}
};
function notify() {
for(const listener of listeners) {
listener(machine);
}
}
const machine = {
state: 'disabled',
context,
send(event) {
if(typeof event === 'string') {
event = { type: event };
}
const handler = states[machine.state][event.type];
if(handler) {
handler(event);
notify();
}
},
subscribe(listener) {
listeners.add(listener);
return () => listeners.delete(listener);
},
start() {
notify();
return machine;
},
stop() {
listeners.clear();
}
};
return machine;
}
See the install page for detailed instructions on the various ways you can use Lucy, such as with popular JavaScript build tools.
To use the command-line compiler install with:
curl -sSf https://lucy.spooky.click/install.sh | bash