Skip to content

Instantly share code, notes, and snippets.

@Mazesch
Mazesch / Example_XState.ts
Created December 5, 2019 10:26
XState example for a simple 3-state machine
import { Machine, State, actions, assign, send, sendParent, interpret, spawn} from 'xstate';
const example1Machine = Machine(
{
id: 'mainRegion',
initial: 'green',
states: {
green: {
type: 'atomic',
id: 'greenState',
@Mazesch
Mazesch / wizardMachine.ts
Created February 27, 2020 12:18
Raw generated code from the wizardMachine
import { Machine, State, actions, assign, send, sendParent, interpret, spawn, EventObject} from 'xstate';
export const wizardMachine = Machine(
{
id: 'mainregion',
context: {
one: null,
two: null,
three: null
@Mazesch
Mazesch / wizardMachine2.ts
Created February 27, 2020 12:31
The reworked wizardMachine
import { resetStatus, getStatus, getNotStatus} from "./utils";
import { Machine, State, actions, assign, send, sendParent, interpret, spawn, EventObject} from 'xstate';
export const wizardMachine = Machine(
{
id: 'wizard',
context: {
one: null,
two: null,
/** Add another import */
import { resetStatus, getStatus, getNotStatus} from "./utils";
/** [...] */
/** Add functionality for the called operations */
actions: {
assignOne:
assign({ one: function (context : any, event : EventObject) { return event.value; } }),
assignTwo:
# Class for defining callbacks for statechart out-events
class Callback:
statechart = None
oven = None
def __init__(self, statechart, oven):
self.statechart = statechart
self.oven = oven
def heat_on(self):
self.oven.heatOn()
def heat_off(self):
# Set statechart observer for out-events
class Reached_Temperature_Observer( Observer ):
def __init__(self, aws):
self.aws = aws
def next(self, value=None):
if self.aws is not None:
self.aws.publish("reachedTemperature", None)
else:
print("Could not publish '" + "reachedTemperature" + "' because not connection was established.")
self.statechart.ready_observable.subscribe( Reached_Temperature_Observer(self.aws) )
import math
class Oven:
def __init__(self):
self.temperature = 0
self.heating = False
def heatOn(self):
self.heating = True
def customCallback(self, client, userdata, message):
dictionary = json.loads(message.payload)
if dictionary["event"] and dictionary["value"]:
if dictionary["event"] == "setTargetTemperature":
value = float(dictionary["value"])
self.raise_set_target_temp(value, False)
print('Received: "' + str(json.loads(message.payload)) + '" from topic "' + "'" + (message.topic) + "'")
@Mazesch
Mazesch / MultiStepForm.ts
Created March 16, 2023 11:02
This XState machine can be used for a multi-step form. For further information please take a look at this blogpost: (To be added!)
import { createMachine, Machine, State, actions, assign, send, sendParent, interpret, spawn, EventObject, DoneInvokeEvent} from 'xstate';
import { Subject } from 'rxjs';
const {choose} = actions;
export class StatechartContext {
operationCallback : OperationCallback;
constructor(operationCallback : OperationCallback = new class{
assignBeneficiaryInfoToContext(): void {throw "Method 'assignBeneficiaryInfoToContext' does not have an implementation in DefaultOperationCallback."};
assignDateToContext(): void {throw "Method 'assignDateToContext' does not have an implementation in DefaultOperationCallback."};