Skip to content

Instantly share code, notes, and snippets.

@RoyalIcing
RoyalIcing / 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() {}
export function* TrafficLights() {
function* Green() {
yield on("timer", Yellow);
}
function* Yellow() {
yield on("timer", Red);
}
function* Red() {
yield on("timer", Green);
}
@RoyalIcing
RoyalIcing / 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() {
@RoyalIcing
RoyalIcing / 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() {
@RoyalIcing
RoyalIcing / 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() {
// 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]
@RoyalIcing
RoyalIcing / example.js
Created June 2, 2022 10:59
yieldtable
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))]);
@RoyalIcing
RoyalIcing / react-simple-model.js
Last active June 2, 2022 11:40
State Machine for simplified version of React’s rendering model
export function* ReactComponent() {
function* Initial() {
yield on("render", Rendering);
}
function* Rendering() {
yield on("resourceSuspended", Suspended);
yield on("error", Errored);
yield on("commit", Committed);
}
function* Suspended() {
@RoyalIcing
RoyalIcing / traffic-lights.jsx
Last active June 9, 2022 12:54
Yieldmachine + React
import { start, on } from "https://unpkg.com/[email protected]/dist/yieldmachine.module.js"
function TrafficLightsMachine() {
const { Green, Yellow, Red } = {
*Green() {
yield on("timer", Yellow);
},
*Yellow() {
yield on("timer", Red);
},
@RoyalIcing
RoyalIcing / 1.ts
Last active November 18, 2022 05:44
JavaScript and TypeScript: two worlds run at different times
// Constants are values with names.
// Functions accept values and return values.
const favoriteNumber = 7
const favoriteNumber2 = (n: number) => n * 2
const doubleNumber = (n: number) => n * 2
const a = doubleNumber(favoriteNumber)
// = 14