Skip to content

Instantly share code, notes, and snippets.

Avatar

Charles Lowell cowboyd

View GitHub Profile
@cowboyd
cowboyd / main.js
Created Nov 29, 2021
Effection Workers
View main.js
const { once, spawn, main, on, createChannel } = require('effection');
const { Worker } = require('worker_threads');
main(function* () {
const worker1 = yield createWorker('./worker1.js');
const worker2 = yield createWorker('./worker2.js');
yield spawn(function*() {
const interval = setInterval(() => {
worker2.worker.postMessage({ t: 'getValue' }); // ticker
@cowboyd
cowboyd / 1-click-to-load.js
Last active Nov 15, 2021
Handle a stream of clicks by canceling the latest one
View 1-click-to-load.js
/**
* Implements an operation to fetch the data as described in https://twitter.com/BenLesh/status/1455597411385098242
* The "winner" is unambiguous. It is the request corresponding to the latest click, guaranteed.
* Other important things to note:
*
* 1. The HTTP request is cancelled every time along with its containing task
* 2. click listener is removed automatically at the end of the `clickToLoad` operation
* 3. The cancellation logic is explicit here to demonstrate the concept, but it can easily be abstracted into a separate
*. function. See the `click-to-load-HOF` example below
*/
@cowboyd
cowboyd / graphgen-relay.ts
Created Sep 28, 2021
Creating a Relay page from a graphgen edge type
View graphgen-relay.ts
import { Vertex } from '@frontside/graphgen';
export interface PageArgs {
first?: number;
before?: string;
last?: number;
after: string;
}
export interface PageInfo {
@cowboyd
cowboyd / spawn-point.ts
Last active Sep 23, 2021
Creating a spawn point that will enqueue a build
View spawn-point.ts
import { createQueue, main, on, Operation, Resource } from 'effection';
import { daemon, exec, Process } from '@effection/process';
import { watch } from 'chokidar';
main(function*() {
let watcher = watch('./src/**/*.ts', { ignoreInitial: true, ignored: 'dist' });
try {
let build = yield createSpawnPoint(start);
@cowboyd
cowboyd / debug.ts
Last active May 21, 2021
Rudimentary Effection debug API
View debug.ts
// pause
// -- suspend at the next point
// play
// -- unsuspend and you are now free to do
// step
// -- unspend, but suspend at the next point
import { main } from './main';
import { once, Operation } from 'effection';
import { EventEmitter } from 'events';
@cowboyd
cowboyd / everboil.md
Created May 12, 2021
Thoughts on evergreen boiler-plate configuration
View everboil.md

Thoughts on evolving project boilerplate

Projects are bootstrapped with templates, and so before we can talk about evolving project boilerplate, we have to first talk about how templates work.

What is a template really?

We can represent the rendering of a template T into an artifact a with the equation

a = T(v)
@cowboyd
cowboyd / supervisor.ts
Created Mar 17, 2021
potential supervisor in effection
View supervisor.ts
import { Task, Operation } from 'effection';
interface SupervisorOptions {
maxConcurrency?: number;
dropMostRecent?: boolean;
}
const supervisorDefaults = {
maxConcurrency: 1,
dropMostRecent: true
@cowboyd
cowboyd / atom-operation-map.ts
Created Feb 18, 2021
map a key-value object to an operation
View atom-operation-map.ts
export function* map<A>(slice: Slice<Record<string, A>>, operation: (slice: Slice<A>) => Operation<void>): Operation<void> {
let contexts = new Map<string,Context>();
function* synchronize(record: Record<string, A>) {
let keep = new Set<string>();
for (let key of Object.keys(record)) {
if (!contexts.has(key)) {
contexts.set(key, yield spawn(operation(slice.slice(key))))
}
@cowboyd
cowboyd / describe.ts
Created Feb 12, 2021
Hypothetical BDD syntaxt for bigtest that uses generators to build a test tree
View describe.ts
import { Page, TextField, Button, HTML, Heading, including } from '@bigtest/interactor';
import { describe } from '@bigtest/suite/bdd'; //hypothetical bdd syntax entry point
import { createUser } from './helpers';
export default describe("login test", function*() {
yield createUser("cowboyd", "password"); //=> injects `user` into context
yield Page.visit('/sign-in');
yield Heading(including('Sign into app')).exists();
yield describe("with good credentials", function*() {
@cowboyd
cowboyd / pipelined-interactors.ts
Created Dec 3, 2020
Hypothetical pipeline syntax for bigtest tests
View pipelined-interactors.ts
import { Page } from 'bigtest';
import { DashboardCard, Heading } from './interactors';
import { test, step, child, createList } from './client-test-helpers';
export default test('Admin UI: Main Dashboard')
|> createList('SomeEntity', 3)
|> Page.visit('/admin/dashboard')
|> Heading("Dashboard").exists()
|> DashboardCard("Stock Entities").exists()
|> child(