Skip to content

Instantly share code, notes, and snippets.

View patrickgwsmith's full-sized avatar

Patrick George Wyndham Smith patrickgwsmith

View GitHub Profile
const Key = primaryKeyText('key');
const Count = column('count', int(1));
const CountersTable = table('counters', [Key, Count]);
function* Create() {
yield create(CounterTable);
}
function* QueryCountFor(key) {
return select([Count], [where(Key(key))]);
// See: https://twitter.com/buildsghost/status/1529258931809202176
// And: https://twitter.com/lewisl9029/status/1529360924343095296
let db
function allProps(object) {
Object.fromEntries(
await Promise.all(
Object.entries(object).map(async ([key, promise]) => {
return [key, await promise]
@patrickgwsmith
patrickgwsmith / filter-machine.js
Last active April 14, 2022 07:37
Filter with toggling machine
export function* OpenFilter() {
function* Closed() {
yield on("First", First);
yield on("Second", Second);
}
function* First() {
yield on("First", Closed);
yield on("Second", Second);
}
function* Second() {
@patrickgwsmith
patrickgwsmith / f1-qualifying-state-machine.js
Created March 23, 2022 03:55
Modelling Formula One qualifying as a state machine
const S1Time = Symbol('S1Time');
function DriverTime() {
function* NoTime() {
yield on('sector1Set', SectorTime, id('Sector1'));
yield on('sector2Set', SectorTime, id('Sector2'));
yield on('sector3Set', SectorTime, id('Sector3'));
}
function* SectorTime() {
@patrickgwsmith
patrickgwsmith / youtube-url-parser.js
Last active March 4, 2022 05:41
YouTube URL YieldParser
function* VideoID() {
const [videoID] = yield /^[a-zA-Z0-9_]+$/;
return videoID;
}
function* Long() {
yield "https://www.youtube.com/watch?v=";
const videoID = yield VideoID;
return videoID;
}
function* Embed() {
export function* TrafficLights() {
function* Green() {
yield on("timer", Yellow);
}
function* Yellow() {
yield on("timer", Red);
}
function* Red() {
yield on("timer", Green);
}
@patrickgwsmith
patrickgwsmith / loader.js
Created February 15, 2022 06:13
Loader state machine component
export function Loader() {
function* Loading() {
yield on("error", Failed);
yield on("timeout", TimedOut);
yield on("abort", Aborted);
yield on("success", Loaded);
}
function* Failed() {}
function* TimedOut() {}
function* Aborted() {}
@patrickgwsmith
patrickgwsmith / remorse.php
Created January 20, 2022 01:43
Remorse: Focused on web fundamentals and archaic UX
<?php
function loader($request) {
return getProjects();
}
function action($request) {
$form = $request.formData();
return createProject([ "title" => $form.get("title") ]);
}
@patrickgwsmith
patrickgwsmith / 1.js
Last active March 9, 2022 04:23
Mini modules
/**
* Pi to 8 decimal places
*/
export const pi = 3.14159265;
export const dateFormat = "YYYY/MM/DD";
export const isEnabled = true;
export const flavors = ["vanilla", "chocolate", "caramel", "raspberry"];
@patrickgwsmith
patrickgwsmith / timing.ts
Last active December 2, 2021 02:59
Debouncing with logical clocks in React
import { DispatchWithoutAction, useEffect, useMemo, useReducer } from 'react';
import type { DependencyList, EffectCallback } from 'react';
/**
* A logical clock.
*
* @returns a tuple with the current clock value, and a stable function that advances the clock.
*/
export function useTicker(): [number, DispatchWithoutAction] {
return useReducer(n => n + 1, 0);