Skip to content

Instantly share code, notes, and snippets.

@cowboyd
cowboyd / catalog.graphql
Created March 3, 2023 15:58
Hypothetical GraphQL API for Backstage
interface Node {
id: ID!
}
interface Entity implements Node
@loads(from: "catalog")
@discriminates(with: "kind") {
id: ID!
kind: String!
}
@cowboyd
cowboyd / observer.ts
Created February 23, 2023 21:40
Stateful observer for guaranteed delivery of continuations.
import type { Observer, Resolve } from "../types.ts";
import { shift } from "../deps.ts";
export function createObservable<T>() {
let observers = new Map<Observer<T>, Resolve<T>>();
return {
notify(value: T) {
for (let dispatch of observers.values()) {
@cowboyd
cowboyd / op-fns.ts
Last active February 22, 2023 17:01
all() and race() implementations with effection v3 architecture
import type { Operation } from "./types.ts";
import { action, resource, spawn } from "./mod.ts";
export function all<T>(operations: Operation<T>[]): Operation<T[]> {
return resource(function* All(provide) {
let scope = yield* useScope();
let tasks = operations.map(op => scope.run(() => op));
let results = yield* map(tasks, task => task);
@cowboyd
cowboyd / Editor.tsx
Created January 6, 2023 22:03
Trying to instantiate Monaco in fresh.deno.dev
import { useState, useRef, useEffect } from "preact/hooks";
import { useCSP } from "$fresh/runtime.ts";
const monacoSrc = 'https://esm.sh/v102/monaco-editor@0.34.1/es2021/monaco-editor.js';
export function Editor() {
useCSP(csp => {
let scriptSrc = csp.directives.scriptSrc = csp.directives.scriptSrc ?? [];
scriptSrc.push(monacoSrc);
});
@cowboyd
cowboyd / scope.ts
Last active October 13, 2022 18:09
Scope resource for managing concurrency in effection
export interface ScopeOptions {
maxConcurrency: number;
}
export interface Scope {
(operation: Operation<T>): Operation<void>;
}
export function createScope(options: ScopeOptions): Operation<Scope> {
let { maxConcurrency = Infinity } = options;
@cowboyd
cowboyd / nunjucks.ts
Last active September 12, 2022 20:04
Use nunchucks to render a precompiled template that doesn't require a `window` object or any global variables.
// requires deno > 1.25.1
// deno run --allow-env --unstable nunjucks.ts
import nunjucks from "npm:nunjucks";
// we use nunjucks itself to create the wrapper function
let wrapperFn = nunjucks.compile(`
(function() {
return function appendTemplates(precompiled) {
{% for template in templates %}
import { google } from 'googleapis'
import { Firestore } from '@google-cloud/firestore'
import { main, createQueue } from 'effection';
import "path"
import _ from "lodash"
class DB {
@cowboyd
cowboyd / use-handler.tsx
Last active February 4, 2023 22:59
A hypothetical restartable operation for @effection/react
import { useCallback, useMemo } from 'react';
import { Task, Operation, createQueue, spawn } from 'effection';
import { useOperation } from './use-operation';
/**
* Create an event handler that can be used for UI events safely.
* If the handler is invoked more than once, the existing task is halted
* and a new one is spawned. E.g.
*
* const AutoCompleter = ({ searchFn }) => {
@cowboyd
cowboyd / experiment.ts
Last active August 10, 2022 21:45
Effection 3 effects/resources as continuations
import { Operation, run, effect, sleep } from "./mod.ts";
interface Num {
value: number;
incrementSlowly(): Operation<void>;
}
function useNum(initial: number) {
return effect<Num>(function*(provide) {
let value = initial;
@cowboyd
cowboyd / README.md
Last active May 6, 2022 12:49
Contains pass / fail terminal output. Both raw, and rendered to html

In order to generate html from terminal output, you can use this tool https://github.com/buildkite/terminal-to-html to do it in two steps.

Capture

First, run your command, and save it to a raw output file.

$ mycommand > output.term