Skip to content

Instantly share code, notes, and snippets.

@cowboyd
cowboyd / use-websocket.ts
Created October 31, 2023 15:35
Effection WebSocket Resource
import { type Stream, type Operation, action, each, once, resource, spawn, createChannel, createSignal } from "effection";
export interface WebSocketHandle extends Stream<MessageEvent, CloseEvent> {
send(value: string): Operation<void>;
close(code?: number, reason?: string): Operation<void>;
}
export function useWebSocket(url: string | URL, protocols?: string | string[]) {
return resource<WebSocketHandle>(function*(provide) {
let input = createChannel<string, {code?: number; reason?: string}>();
@cowboyd
cowboyd / how-long.mjs
Last active March 20, 2024 16:47
sleep() race
/**
* Given the following definition of sleep(), how long will this program take to
* run?
*
* Try it out with:
*
* ```
* node how-long.js
* ```
*/
@cowboyd
cowboyd / 01-client.ts
Created March 10, 2024 16:48
Model a remote iterable using `EventSource` and Effection
import { main } from "npm:effection@3.0.3";
import { useEventStream } from "./use-event-stream.ts";
// consume the server as an interable.
await main(function* () {
let source = yield* useEventStream<number, string>("http://localhost:8000");
let next = yield* source.next();
@cowboyd
cowboyd / 01-test-scope.ts
Last active February 13, 2024 16:50
A testing scope for Effection to help writing tests
import { run, type Operation } from "effection";
export interface TestScope {
/**
* Call from your runner's "beforeEach" or equivalent
*/
addSetup(op: () => Operation<void>): void;
/**
* Call from runner's `it()` or equivalent;
@cowboyd
cowboyd / request-animation-frames.ts
Created January 19, 2024 14:23
Effection Request Animation Frame Stream
import { createSignal, resource, type Stream } from "./mod.ts";
/**
* Consume RAF's as a Stream.
*
* ## Example
*
* for (let timestamp of yield* each(requestAnimationFrames)) {
* // do work
* yield* each.next();
@cowboyd
cowboyd / console.ts
Last active January 9, 2024 14:18
Effection console effect
// deno-lint-ignore-file no-explicit-any
import { createContext, type Operation } from "./mod.ts";
export interface Console {
// Logging
assert(condition?: boolean, ...data: any[]): Operation<void>;
clear(): Operation<void>;
debug(...data: any[]): Operation<void>;
error(...data: any[]): Operation<void>;
@cowboyd
cowboyd / use-restartable.ts
Last active January 6, 2024 02:03
Implement a restartable operation from any operation
import {
createChannel,
each,
Operation,
resource,
spawn,
Task,
} from "effection";
export function useRestartable<TArgs extends unknown[]>(
@cowboyd
cowboyd / get-user.ts
Created December 19, 2023 14:43
Operations resolve whenever they are ready to continue be it synchronously or asynchronously
import { call } from "effection";
function* getUser2(id: number) {
if (cachedUser) return cachedUser;
let response = yield* call(fetch(`/users/${id}`));
return yield* call(response.json());
}
@cowboyd
cowboyd / timer.tsx
Created December 11, 2023 15:11
Timer Example
import { createSignal, call, first, each, spawn, sleep, type Operation } from "effection";
import { render } from "$revolution";
export default function* TimerIsland(): Operation<void> {
let reset = createSignal<void>();
let start = createSignal<void>();
let stop = createSignal<void>();
let elapsed = 0;
let t0 = performance.now();
@cowboyd
cowboyd / browser.test.ts
Created November 17, 2023 22:49
A trivial test case launching the astral browser
import { describe, it } from "https://deno.land/std@0.206.0/testing/bdd.ts"
import { launch } from "https://deno.land/x/astral@0.3.1/mod.ts";
describe("browser", () => {
it("launches, sleeps, and closes", async () => {
let browser = await launch();
await new Promise((resolve) => setTimeout(resolve, 2000));
browser.close();
});
});