Skip to content

Instantly share code, notes, and snippets.

@dead-claudia
dead-claudia / node-repl-autoload.js
Created May 14, 2018 06:27
Helpful Node.js REPL function (use via `node -r ./path/to/node-repl-autoload.js`).
"use strict"
global.autoloadExport = (invoke, deps = []) => {
const req = global.require, [file] = deps = deps.map(req.resolve)
return arg => {
const mod = req(file)
deps.forEach(dep => delete req.cache[dep])
return invoke(mod, arg)
}
}
@dead-claudia
dead-claudia / simple-event-emitter.js
Created May 12, 2018 05:10
Super simple event emitter
"use strict"
function EventEmitter() {
this._events = Object.create(null)
}
EventEmitter.prototype.on = function (event, func) {
var handlers = this._events[event] = this._events[event] || []
if (handlers.indexOf(func) < 0) handlers.push(func)
}
@dead-claudia
dead-claudia / load-uncached.js
Created May 12, 2018 04:48
Better module uncached loader
"use strict"
const Module = require("module")
const loaded = new WeakMap()
let visited = new WeakSet()
const parents = new Set()
function removeRecursive(parent, mod) {
const index = loaded.get(mod)
@dead-claudia
dead-claudia / state-driver.js
Last active May 29, 2018 07:33
State driver for async generator-based storylines
// This is careful to avoid memory leaks. Also, it is written in pure
// ES5 assuming a Promise polyfill (so it works with transpiled code
// without needing transpiled) and the relevant Symbol polyfills.
function run(route, render, onError) {
"use strict"
if (onError == null) onError = function (e) { console.error(e) }
function run(next) {
var current = next.value
if (Array.isArray(current)) current = current[0]
@dead-claudia
dead-claudia / 0-tap-original.js
Last active April 7, 2018 18:35
Pipeline comparison using real-ish code (example adapted from some of my own code in the wild)
// Adapted from https://github.com/isiahmeadows/thallium/blob/b0bce2d2b7a104f5aa73b1ba0b532bcb78418d48/r/spec.js
// Original: no pipeline operators
import * as R from "../lib/reporter"
import {color as c} from "../lib/reporter"
const printIndent = (_, str) => _.print(" ".repeat(_.state.level) + str)
const getLast = (report, _, key = "name") => report.path[_.state.level - 1][key]
export default R.on("spec", {
// A complete set of type defs for Fantasy Land
type unknown = {} | null | undefined;
interface Setoid {
"fantasy-land/equals"(other: this): boolean;
}
interface Ord {
"fantasy-land/lte"(other: this): boolean;
}
@dead-claudia
dead-claudia / promise-api.d.ts
Last active March 13, 2018 07:54
Simpler, better Promise library
type PollResult<T> =
| {type: "pending"}
| {type: "resolved", value: T}
| {type: "rejected", value: Error}
| {type: "cancelled"}
interface Receiver<T> {
resolve?(value: T): any
reject?(value: Error): any
}
@dead-claudia
dead-claudia / link-data.json
Created February 14, 2018 23:30
Flems test links
@dead-claudia
dead-claudia / mithril-route-resolver-2.js
Created January 17, 2018 23:28
Attempt to implement the route resolver concept as a component
// API:
// `m(RouteResolver, {onmatch(path), render(Comp)}) -> Component`
//
// Note: this is all in pure ES5
//
// Assumed extensions:
// - `m.route.default()` - Get the default route
var RouteResolver = {
oninit: function (vnode) {
@dead-claudia
dead-claudia / mithril-route-resolver.js
Last active January 17, 2018 23:25
Attempt to implement the RouteResolver API out of core
// This wraps the `m.route` API to implement route resolvers
//
// Note: this is all in pure ES5
//
// Assumed extensions:
// - `m.route.default()` - Get the default route
var route = (function () {
"use strict"