Skip to content

Instantly share code, notes, and snippets.

@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:[email protected]";
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 / 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 / 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 / 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/[email protected]/testing/bdd.ts"
import { launch } from "https://deno.land/x/[email protected]/mod.ts";
describe("browser", () => {
it("launches, sleeps, and closes", async () => {
let browser = await launch();
await new Promise((resolve) => setTimeout(resolve, 2000));
browser.close();
});
});
@cowboyd
cowboyd / rebase.ts
Created November 12, 2023 03:03
revolution html middleware that rebases a document at a different url
import { posixNormalize } from "https://deno.land/[email protected]/path/_normalize.ts";
import { selectAll } from "npm:[email protected]";
import type { Element } from "hastx/jsx-runtime";
/**
* Rebase an HTML document at a different URL. This replaces all `<a href>` and
* `<img src>` attributes that contain an absolute path. Any path that is
* relative or contains a fully qualitfied URL will be left alone.
*