Skip to content

Instantly share code, notes, and snippets.

View davidkpiano's full-sized avatar
🎹
Working on XState Dev Tools

David Khourshid davidkpiano

🎹
Working on XState Dev Tools
View GitHub Profile
@Andarist
Andarist / gist:0294519c570a52fb14f4cdd3d589c880
Created February 23, 2023 08:49
Reverse mapped types brain dump
mapped types syntax and intro
https://www.typescriptlang.org/play?#code/C4TwDgpgBA8gRgKygXigbwFBW1AZge3wC4oBnYAJwEsA7AcwBosc4BDCkmgVwFs4IKGAL4YMoSFACSAEwg1gVUAB4AKgD4U6ZtgDaAaSi0oAawgh8uKCoC6JFfuvDR46AGVWPaKhlyFy+AhqGAD0wTgAegD8omLg0AAKFPg8VKRUuCCqGqiYOFD6hjQmZhZWtlAAFACUKBqJyakQqg5BIrES9SlpuFQQ0pqdqemZAUGhEdEYsgDGADbs0NP4NORQFGDTJIPdvdKiSyvAeFzAXBReaxsAdGwU1SFheU-PUFGiuCdnEFfAABZyFQqADdWLMasgNLkcCDZtooOM3kIqkA
a tweet that prompted the idea for the talk
https://twitter.com/kentcdodds/status/1608187990215655424
but since this isn't possible with satisfies (it doesn't participate in inference, in an example like that it only provides contextual types) the answer was to use a function with a reverse mapped type
@kirillrogovoy
kirillrogovoy / xstate-backend.md
Created December 13, 2022 10:13
Using XState on the back end

State machine code with no implementation:

const trainingMachine = createMachine<TrainingContext>({
  predictableActionArguments: true,
  id: 'training',
  initial: 'initial',
  context: {},
  states: {
    initial: {
      on: {
export const make = Effect.struct({
ref: SubscriptionRef.make(0),
});
export interface Counter extends Effect.Success<typeof make> {}
export const Counter = Tag<Counter>();
export const CounterLive = Layer.fromEffect(Counter)(make);
export const increment = Effect.serviceWithEffect(Counter, ({ ref }) =>
ref.update((count) => count + 1)
// WIP, just finding all the boxes and glue, implementation is woefully incomplete
import type { DOMAttributes } from "react";
import { assign, createMachine, interpret } from "@xstate/fsm";
import invariant from "tiny-invariant";
type CustomElement<T> = Partial<
T & DOMAttributes<T> & { children: any; class: string }
>;
@steveruizok
steveruizok / cache.ts
Last active March 3, 2026 16:27
weak map gist
export class Cache<T extends object, K> {
items = new WeakMap<T, K>()
get<P extends T>(item: P, cb: (item: P) => K) {
if (!this.items.has(item)) {
this.items.set(item, cb(item))
}
return this.items.get(item)!
}
@redbar0n
redbar0n / XState-boilerplate-refactor.mdx
Last active April 20, 2024 13:59
The MVC-widget - Through refactoring boilerplate out from an XState example

What if you could have vertically sliced MVC-widgets, that looked something like this?

Using React and XState.

//  A vertically sliced "MVC-widget"

//  VIEW:

export default function Users() {
@Slooowpoke
Slooowpoke / .ts
Last active May 18, 2024 20:31
MST + Xstate
/* eslint-disable no-param-reassign */
import {
getMembers, types
} from 'mobx-state-tree'
import {
createMachine
} from 'xstate'
import { interpret } from 'xstate/lib/interpreter'
// Pieced together from:

@xstate/test model generation with zones

What is a zone?

A zone is an isolated portion of an @xstate/test model. A zone defines the events it uses, the event that causes it to be entered and the states within the zone

The zones entryEvent is added as an event handler to the zones parent initial state. If this is a root zone passed to buildTestModel this is the root state, if it's a sub zone (a state pointing at a zone), that is the initial state of the zone that state is contained in

Each state in a zone must contain a test property that validates that the state has been entered. At least one of the states requires it to be marked as the initial state of the zone with initial: true An event handler in a zone state can only refer to events defined inside of the zone

@temoncher
temoncher / strongly-typed-fsm.ts
Last active March 5, 2022 07:23
Builder pattern for strongly typed Finite State Machines in TypeScript
type State = string;
type Message = string;
type StatesList = readonly State[];
type MessagesConfig = Record<State, Record<Message, State>>;
type OneOf<S extends StatesList> = S[number];
type Send<
SBConfig extends StateBuilderConfig<StatesList, State, State>,
@hew
hew / _readme.md
Last active September 29, 2021 12:28
Thoughts on standardizing REST with machines

Thoughts on standardizing REST with machines