Skip to content

Instantly share code, notes, and snippets.

@cowboyd
cowboyd / promises
Last active February 18, 2022 07:23
Stream promises using effection
import { createStream } from 'effection';
// create an effection stream out of an array of promises
// https://frontside.com/effection/docs/guides/collections
export function producer(promises) {
return createStream(publish => {
for (let promise of promises) {
promise.then(publish);
}
});
@cowboyd
cowboyd / main.js
Created November 29, 2021 10:50
Effection Workers
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 April 11, 2023 10:54
Handle a stream of clicks by canceling the latest one
/**
* 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 September 28, 2021 11:00
Creating a Relay page from a graphgen edge type
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 September 23, 2021 07:46
Creating a spawn point that will enqueue a build
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 17:10
Rudimentary Effection debug API
// 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 01:54
Thoughts on evergreen boiler-plate configuration

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 March 17, 2021 03:01
potential supervisor in effection
import { Task, Operation } from 'effection';
interface SupervisorOptions {
maxConcurrency?: number;
dropMostRecent?: boolean;
}
const supervisorDefaults = {
maxConcurrency: 1,
dropMostRecent: true
@cowboyd
cowboyd / atom-operation-map.ts
Created February 18, 2021 12:29
map a key-value object to an operation
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 February 12, 2021 08:38
Hypothetical BDD syntaxt for bigtest that uses generators to build a test tree
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*() {