Skip to content

Instantly share code, notes, and snippets.

View thomaswilburn's full-sized avatar
🦝

Thomas Wilburn thomaswilburn

🦝
View GitHub Profile
@thomaswilburn
thomaswilburn / geocode.js
Last active January 6, 2026 15:43
Make sandboxed JSONP calls to the Census geocoder
function jsonp(url, callback = "callback") {
return new Promise((ok, fail) => {
let iframe = document.createElement("iframe");
let onmessage = function(e) {
if (e.source == iframe.contentWindow) {
iframe.remove();
window.removeEventListener("message", onmessage);
ok(e.data.response);
}
};
class Cluster {
start = 0;
end = 0;
min = 0;
max = 0;
items = [];
constructor(items, start, end, min, max) {
this.items = items;
this.start = start;
@thomaswilburn
thomaswilburn / signals.js
Created June 18, 2024 17:40
Preact Signals But With EventTarget
var stack = [];
var verbose = false;
// make our own undefined sentinel value
var undefined = Symbol("undefined signal");
class Signal extends EventTarget {
#value = undefined;
#computation = null;
#lifetime = new AbortController();
@thomaswilburn
thomaswilburn / import-ants.js
Last active February 20, 2024 21:34
Async imports for CSS files
var links = document.querySelectorAll(`link[rel="stylesheet"][media="async"], link[rel="async-styles"]`);
for (var link of links) {
var resolved = new URL(link.href, window.location);
var processed = await getSheet(resolved.toString());
var style = document.createElement("style");
var constructed = new CSSStyleSheet();
var output = serializeCSS(processed);
constructed.replaceSync(output);
// console.log(output);
@thomaswilburn
thomaswilburn / blanks.html
Last active December 15, 2023 01:07
Fill In The Blanks
<!doctype html>
<style>
.card {
display: grid;
grid-template-columns: 1fr 2fr;
& img {
max-height: 200px;
}
}
@thomaswilburn
thomaswilburn / defaultdict.js
Created December 12, 2023 22:35
Defaultdict but make it JavaScript
const TARGET = Symbol("proxy target");
function defaultdict(missing) {
var dict = {};
var instantiate = missing;
if (missing instanceof Function) {
if (missing.constructor) {
instantiate = () => new missing();
}
} else if (missing instanceof Object) {
@thomaswilburn
thomaswilburn / index.js
Created December 11, 2023 17:50
ReactiveStore
var proxyRegistry = new WeakMap();
function observe(root, callback) {
var handler = {
get(target, property, rec) {
var value = target[property];
if (value instanceof Object && !(value instanceof Function)) {
if (proxyRegistry.has(value)) {
return proxyRegistry.get(value);
}
@thomaswilburn
thomaswilburn / index.js
Created December 5, 2023 21:04
Classy signals
var tracking = [];
var pending = new Set();
function anticipate(fn) {
pending.add(fn);
requestAnimationFrame(() => {
for (var p of pending) p();
pending.clear();
});
}
@thomaswilburn
thomaswilburn / index.html
Created December 3, 2023 03:39
Benchmark Lit iteration vs. replaceChildren
<!doctype html>
<ul id="ul"></ul>
<script type="module">
import { html, render } from "https://unpkg.com/lit-html";
import { repeat } from "https://unpkg.com/lit-html/directives/repeat.js";
import { map } from "https://unpkg.com/lit-html/directives/map.js";
function replacer(container, data, factory, update = () => {}) {
@thomaswilburn
thomaswilburn / datapatcher.js
Last active November 30, 2023 18:56
Update DW graphics from a Google Sheet
// run with deno run --allow-all datapatcher.js {SHEET_ID} {SHEET_TAB (optional)}
import { login, google } from "https://raw.githubusercontent.com/Chalkbeat/deno-google-login/main/index.js";
import * as flags from "https://deno.land/[email protected]/flags/mod.ts";
var args = flags.parse(Deno.args);
var spreadsheetId = args.sheet || args._[0];