Skip to content

Instantly share code, notes, and snippets.

💭
🤔💭

Sam Thorogood samthor

View GitHub Profile
@samthor
samthor / emscripten-metadata.js
Last active Apr 16, 2019
code to read emscripten_metadata section of Web Assembly files
View emscripten-metadata.js
function lebReader(array) {
const view = new Uint8Array(array);
let at = 0;
return () => {
if (at >= view.length) {
return -1;
}
let ret = 0;
@samthor
samthor / IDObserver.js
Created Sep 9, 2018
helper JS to observe $ changes
View IDObserver.js
/**
* Raw IDObserver class. Use via default method below.
*/
export class IDObserver {
/**
* @param {!Node|!ShadowRoot} root
*/
constructor(root) {
@samthor
samthor / code.js
Last active Jan 16, 2019
Async cancellable promises
View code.js
// nb. This code is available in an ES module of Promise helpers, here:
// https://github.com/samthor/promises
// symbol returned to indicate that a call was cancelled
export const takeoverSymbol = Symbol('takeover');
/**
* Accepts a generator function, which yields Promises, and converts it to an async function
* that cancels any previous calls.
*/
View png.c
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int chunks, w, h;
} info_t;
const char expected_header[8] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a};
View intervalset.js
// FIXME: glitch doesn't know about Map/Set for some reason
var Map = window['Map'];
var Set = window['Set'];
/**
* @typedef {{i: number, o: *}}
*/
var IntervalData;
@samthor
samthor / undoer.js
Last active Mar 9, 2019
Native Undo & Redo helper
View undoer.js
// This code now lives at https://github.com/samthor/undoer (or 'undoer' on NPM).
@samthor
samthor / autocert-server.go
Last active Feb 13, 2019
Demo autocert server in Go
View autocert-server.go
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/user"
@samthor
samthor / fixer.js
Created Mar 15, 2018
removes all print-specific styles from stylesheets
View fixer.js
Array.from(document.styleSheets).forEach((ss) => {
const toDelete = [];
const toReplace = new Map();
for (let i = 0; i < ss.rules.length; ++i) {
const rule = ss.rules[i];
if (!(rule instanceof CSSMediaRule)) {
continue;
}
View argnames.js
const functionMatch = /^function\s*\w*\(([\w\s,]+)\) {/;
/**
* @param {!Function} fn
* @return {!Array<string>} array of simple arg names (no =, ... etc)
*/
function simpleArgNames(fn) {
const match = functionRe.exec(fn.toString());
if (!match) { return []; }
const args = match[1].split(',').map((x) => x.trim());
@samthor
samthor / module-hacks.md
Last active Sep 7, 2017
ES6 module hacks and nuances
View module-hacks.md

TODO

Hashed modules are different

import './banana.js';
import './banana.js#foo';

These are both run by Chrome 61, but are only fetched from the network once!

You can’t perform that action at this time.