This file contains hidden or 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 hidden or 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}) |
This file contains hidden or 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 isObject = thing => thing != null && typeof thing === "object" | |
export class ReadOnlyProxyWriteError extends Error {} | |
const ReadOnlyProxyDescriptor = { | |
get: (target, key) => { | |
let value = target[key] | |
if (!isObject(value)) { | |
return value | |
} |
This file contains hidden or 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 $version = Symbol('version') | |
const VersionedProxy = { | |
set(target, prop, value) { | |
target[$version] = target[$version] + 1 | |
target[prop] = value | |
}, | |
deleteProperty(target, prop) { | |
target[$version] = target[$version] + 1 |
This file contains hidden or 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
// RenderingElement | |
// A custom element that batches state updates on animation frame. | |
export class RenderingElement extends HTMLElement { | |
// Element state to render | |
#state = this.defaults(); | |
// Has an animation frame been scheduled? | |
#isAnimationFrameScheduled = false | |
// Perform a render and flag that animationFrame has completed. |
This file contains hidden or 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
const $version = Symbol('version') | |
export const version = state => { | |
if state[$version] == null { | |
return 0 | |
} | |
return state[$version] | |
} | |
export const markSynced = (following, leading) => { |
This file contains hidden or 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 Foundation | |
import Combine | |
extension DispatchQueue { | |
/// Run a closure on a dispatch queue asyncronously. | |
/// - Returns a future. | |
func future<Output>( | |
perform: @escaping () -> Output | |
) -> Future<Output, Never> { | |
Future<Output, Never> { promise in |
This file contains hidden or 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
// | |
// Store.swift | |
// | |
// Created by Gordon Brander on 9/15/21. | |
// | |
// MIT LICENSE | |
// Copyright 2021 Gordon Brander | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a | |
// copy of this software and associated documentation files (the "Software"), |
This file contains hidden or 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 Foundation | |
import Combine | |
/// Holds cancellables returned by Publisher until the publisher completes. | |
/// | |
/// Combine Publishers return a Cancellable that will automatically cancel the Publisher if the | |
/// Cancellable falls out of scope. Since Publishers can take some time to complete, you often | |
/// want to hold on to the Cancellable reference until the publisher has completed. | |
/// | |
/// PublisherManager takes care of the boilerplate of holding on to Cancellables, and helps |
This file contains hidden or 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 Foundation | |
extension Optional { | |
struct NilError: Error { | |
let file: String | |
let line: Int | |
let column: Int | |
let function: String | |
} |