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
import { entity } from 'durable-functions';
import { State, interpret } from 'xstate';
import { getMachine } from './machines';
import { updateEntity, waitForMachineToResolve } from '../Shared/utilities';
export default entity(async (context) => {
let machine = getMachine(context);
switch (context.df.operationName) {
case 'send':
@samwightt
samwightt / channelManager.ts
Last active May 18, 2023 22:15
Channel Manager
/**
* This is a small library I wrote when I was doing R&D work and needed a way to communicate
* between an iFrame on the same domain and its parent tab. The existing browser API kinda sucked
* and had a lot of issues, and it wasn't particularly enjoyable to use. So I made this small library to solve that.
*
* The library allows you to communicate using *channels*, which are just streams of events with a given name.
* You can subscribe to events of a particular type. Each event type has its own event queue, and each subscriber
* must subscribe to a particular event type. This keeps things simple and fast.
*
* Events are buffered and sent asychronously. There are two ways to send events: firing and blocking.
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
export default class extends Component {
@service('job-state') jobState;
}
@bvaughn
bvaughn / attaching-manual-event-listeners-in-passive-effect.js
Last active July 6, 2022 23:34
Attaching manual event listeners in a passive effect
// Simplistic (probably most common) approach.
//
// This approach assumes either that:
// 1) passive effects are always run asynchronously, after paint, or
// 2) passive effects never attach handlers for bubbling events
//
// If both of the above are wrong (as can be the case) then problems might occur!
useEffect(() => {
const handleDocumentClick = (event: MouseEvent) => {
// It's possible that a "click" event rendered the component with this effect,
@jamiebuilds
jamiebuilds / tradeoffs-in-value-derived-types-in-typescript.md
Last active December 16, 2022 17:21
Value-derived types in TypeScript are super powerful, but you should be thoughtful in how/when you use them

Tradeoffs in value-derived types in TypeScript

Many of the more "advanced" typescript features can be used for creating "value-derived" types.

At its simplest form:

let vehicle = { name: "Van", wheels: 4 }
import { shallowReactive, watchEffect } from '@vue/composition-api'
import { Interpreter, EventObject, Typestate, Actor, State } from 'xstate'
export function useActor<
TContext,
TEvent extends EventObject,
TTypestate extends Typestate<TContext> = { value: any; context: TContext }
>(
service:
| Actor<TContext, TEvent>
@sindresorhus
sindresorhus / esm-package.md
Last active March 3, 2026 20:37
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@jbeast
jbeast / events.ejs.t
Last active December 19, 2020 08:27
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 {
const parseStates = (states, arr) => {
for (let index in states) {
let statename = states[index].id;
if (undefined == statename) {
statename = index;
}
if (states[index].states) {
arr.push(statename + " { ");