Skip to content

Instantly share code, notes, and snippets.

@jbeast
Last active December 19, 2020 08:27
Show Gist options
  • Save jbeast/9e73379c5b947a6aadc4151f763a4505 to your computer and use it in GitHub Desktop.
Save jbeast/9e73379c5b947a6aadc4151f763a4505 to your computer and use it in GitHub Desktop.
Hygen generator for an XState machine (in TypeScript)
---
to: src/lib/machines/<%= name %>/<%= name %>Events.ts
---
import { <%= Name %>Event } from "./<%= name %>Types";
/**
* Event creator for an {@link InitEvent}
*/
export function init(message: string): <%= Name %>Event {
return {
type: "INIT",
message,
}
}
---
to: src/lib/machines/<%= name %>/<%= name %>Machine.ts
---
import { Machine, MachineOptions, send } from "xstate";
import { assign } from "@xstate/immer";
import { <%= Name %>Context, <%= Name %>Schema, State, <%= Name %>Event } from "./<%= name %>Types";
import { init } from "./<%= name %>Events";
enum Action {
ASSIGN_MESSAGE = "assignMessage",
}
enum Activity {}
enum Delay {}
enum Guard {}
enum Service {}
/**
* <%= Name %> Machine Options
*/
const machineOptions: Partial<MachineOptions<
<%= Name %>Context,
<%= Name %>Event
>> = {
actions: {
// Assign the message from the event to context
[Action.ASSIGN_MESSAGE]: assign((ctx, e) => {
if (e.type !== "INIT") {
return;
}
ctx.message = e.message;
}),
},
activities: {},
delays: {},
guards: {},
services: {},
}
/**
* <%= Name %> State Machine
*/
export const create<%= Name %>Machine = () =>
Machine<<%= Name %>Context, <%= Name %>Schema, <%= Name %>Event>(
{
id: "<%= name %>",
context: {
message: "",
},
initial: State.INIT,
states: {
[State.INIT]: {
entry: send(init("<%= Name %> Machine initiated!")),
on: {
INIT: {
actions: Action.ASSIGN_MESSAGE
}
}
}
},
},
machineOptions
);
---
to: src/lib/machines/<%= name %>/<%= name %>Types.ts
---
import { ActorRef, Interpreter } from "xstate";
/**
* The type of an interpreted <%= Name %> Machine
*/
export type <%= Name %>MachineType = Interpreter<
<%= Name %>Context,
<%= Name %>Schema,
<%= Name %>Event
>;
/**
* Type to be used for the `ref` when <%= Name %> Machine is spawned in another machine
* @see {@link https://xstate.js.org/docs/guides/actors.html#spawning-actors}
*/
export type <%= Name %>MachineActorRef = ActorRef<<%= Name %>Event, <%= Name %>MachineType["state"]>;
/**
* Available finite states for a <%= Name %> Machine
*/
export enum State {
INIT = "init",
}
/**
* State Schema for a <%= Name %> Machine
*/
export interface <%= Name %>Schema {
states: {
[State.INIT]: {};
};
}
/**
* Context for a <%= Name %> Machine
*/
export interface <%= Name %>Context {
/**
* A friendly greeting from <%= Name %> Machine
*/
message: string
}
/**
* An example event type
*/
type InitEvent = { type: "INIT", message: string };
export type <%= Name %>Event =
| InitEvent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment