This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const debounceMachine = setup({ | |
types: { | |
events: {} as { type: "edit"; text: string }, | |
}, | |
actors: { | |
check: fromPromise( | |
// 👇 Use `signal` to abort the promise if the user starts typing | |
({ input, signal }: { input: { text: string }; signal: AbortSignal }) => | |
Promise.resolve(input.text) | |
), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useActor } from "@xstate/react"; | |
import { setup } from "xstate"; | |
const copyMachine = setup({ | |
types: { | |
events: {} as { type: "copy"; text: string }, | |
}, | |
}).createMachine({ | |
initial: "Idle", | |
states: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { assign, fromCallback, setup } from "xstate"; | |
type TickEvent = { type: "tick" }; | |
const timerLogic = fromCallback(({ sendBack }) => { | |
const timer = setInterval(() => { | |
sendBack({ type: "tick" } satisfies TickEvent); | |
}, 1000); | |
return () => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { assign, setup } from "xstate"; | |
type Context = { | |
chunks: Blob[]; | |
mediaRecorder?: MediaRecorder; | |
submit: (contents: { file: File }) => void; | |
}; | |
type Events = | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"state machine": { | |
"prefix": "machine", | |
"description": "Create scaffold for state machine", | |
"body": [ | |
"import { setup } from \"xstate\";", | |
"", | |
"export const machine = setup({}).createMachine({", | |
" id: \"$0\",", | |
" initial: \"Idle\",", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Message = string | ((args: any) => string); | |
type Translation = { en: Message; it: Message }; | |
export type I18n = Record<string, Translation>; | |
export const t = { | |
helloWorld: { | |
en: "Hello World", | |
it: "Ciao mondo", | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Brand, Context, Data, Effect, Layer } from "effect"; | |
type FileMetadata = Readonly<{ type: string; size: number }>; | |
export type ValidFile = FileMetadata & Brand.Brand<"ValidFile">; | |
const ValidFile = Brand.nominal<ValidFile>(); | |
class FileTooLargeError extends Data.TaggedError("FileTooLargeError")< | |
Readonly<{ size: number }> | |
> {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { setup } from "xstate"; | |
const machine = setup({ | |
types: { | |
context: {} as {}, | |
events: {} as { readonly type: "event"; readonly params: {} }, | |
}, | |
actions: {}, | |
}).createMachine({ | |
id: "machine", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Data, Either } from "effect"; | |
import type { z } from "zod"; | |
export class ZodParseError<In> extends Data.TaggedError("ZodParseError")<{ | |
error: z.ZodError<In>; | |
}> {} | |
export const parseZod = | |
<ReqOut, ReqIn>(schema: z.Schema<ReqOut, z.ZodTypeDef, ReqIn>) => | |
<T>(data: T): Either.Either<ZodParseError<ReqIn>, ReqOut> => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension IOExtension<L, R> on IOEither<L, R> { | |
IOEither<L, R> runSideEffect({ | |
final void Function(L l)? onLeft, | |
final void Function(R r)? onRight, | |
}) => | |
chainFirst( | |
(r) => IOEither<L, void>.fromIO( | |
IO(() => onRight?.call(r)), | |
), | |
) |
NewerOlder