This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { strict as assert } from "assert"; | |
import { createMiddleware, Msg } from "./middleware.js"; | |
describe("createMiddleware", () => { | |
it("should create middleware with empty initial state", () => { | |
const middleware = createMiddleware(); | |
assert.deepEqual(middleware("test"), "test"); | |
}); | |
it("should execute single middleware", () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let slowAnimations = false; | |
/** Set slow animations for debugging */ | |
export const setSlowAnimations = (isSlow: boolean) => { | |
slowAnimations = isSlow; | |
}; | |
/** @returns 10s if slow animations is turned on, otherwise returns `ms` */ | |
export const slowable = (ms: number) => (slowAnimations ? 10000 : ms); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const multidispatch = <T>() => { | |
const routes = new Map< | |
(...args: unknown[]) => boolean, | |
(...args: unknown[]) => T | |
>(); | |
const call = (...args: unknown[]) => { | |
for (const [isMatch, handler] of routes.entries()) { | |
if (isMatch(...args)) { | |
return handler(...args); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set key on object, but only if value has changed. | |
// This is useful when setting keys on DOM elements, where setting the same | |
// value may trigger a style recalc. | |
// | |
// Note that the typical layout-triggering DOM properties are read-only, | |
// so this is safe to use to write to DOM element properties. | |
// See https://gist.github.com/paulirish/5d52fb081b3570c81e3a. | |
export const prop = (object, key, value) => { | |
if (object[key] !== value) { | |
object[key] = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set property of element. | |
// Uses a write-through cache to only set | |
// when value actually changes. | |
export const prop = (el, key, value) => { | |
let cacheKey = Symbol.from(`prop.${key}`) | |
if (el[cacheKey] != value) { | |
el[cacheKey] = value | |
el[key] = value | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const sleep = ms => new Promise(resolve => { | |
setTimeout(resolve, ms) | |
}) | |
export const hz = x => (1/x * 1000) | |
const complete = Symbol('complete') | |
export class Mailbox { | |
#buffer = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CDOM - Cached DOM | |
// CDOM minimizes DOM writes by caching last written value and only touching the | |
// DOM when a new value does not equal the cached value. | |
// The goal of CDOM is to make it safe to "bash the DOM" as often as you like | |
// without affecting performance, even bashing it every frame. | |
// Create a cached setter | |
export const CachedSetter = (ns, set) => (el, value) => { | |
let cacheKey = Symbol.for(`CachedSetter::${ns}`) | |
if (el[cacheKey] !== value) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {StoreElement, change} from './modest.js' | |
class ModestApp extends StoreElement { | |
static template() { | |
return ` | |
<style> | |
:host { | |
display: block; | |
} | |
.time { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a Python singledispatch / Clojure multimethod style function that | |
// dispatches on the first argument. | |
// | |
// Returns a wrapped function that calls appropriate underlying function | |
// based on prototype of first argument. | |
// | |
// Custom implementations for specific types can be registered through calling | |
// `.define(constructor, fun)` on the wrapped function. | |
export const singledispatch = fallback => { | |
let _key = Symbol('singledispatch method') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shorthand for Object.freeze | |
export const freeze = Object.freeze | |
// Define an immutable model intializer. | |
// Objects are frozen on the way out. | |
export const Model = init => flags => freeze(init(flags)) | |
// Put a value to a key, returning a new frozen object | |
export const put = (state, key, val) => freeze({...state, [key]: val}) |
NewerOlder