Skip to content

Instantly share code, notes, and snippets.

Avatar

Gordon Brander gordonbrander

View GitHub Profile
@gordonbrander
gordonbrander / Store.swift
Last active Feb 17, 2022
Store.swift - a simple Elm-like ObservableObject store for SwiftUI
View Store.swift
//
// 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"),
View PublisherManager.swift
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
View UnwrapOptional.swift
import Foundation
extension Optional {
struct NilError: Error {
let file: String
let line: Int
let column: Int
let function: String
}
@gordonbrander
gordonbrander / viewstore.swift
Last active May 17, 2021
SwiftUI immutable ViewStore
View viewstore.swift
/// ViewStore acts as a state container, typically for some view over the application's central store.
/// You construct a ViewStore and pass it to a subview. Because it is Equatable, you can
/// make the subview Equatable as well, with `.equatable()`. The subview will then only render
/// when the actual value of the state changes.
struct ViewStore<LocalState, LocalAction>: Equatable
where LocalState: Equatable {
static func == (
lhs: ViewStore<LocalState, LocalAction>,
rhs: ViewStore<LocalState, LocalAction>
) -> Bool {
@gordonbrander
gordonbrander / dirty.js
Last active Jan 26, 2021
dirty.js - dirty marking for any JS object
View dirty.js
/*
Released under MIT License
Copyright 2020 Gordon Brander
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
View store-element.js
const $state = Symbol('state')
const $rendered = Symbol('rendered state')
const $shadow = Symbol('shadow')
const $frame = Symbol('animation frame')
// StoreElement is a deterministic web component,
// inspired loosely by Elm's App Architecture pattern.
export class StoreElement extends HTMLElement {
constructor() {
super()
@gordonbrander
gordonbrander / README.md
Last active Jan 17, 2022
microview - data-driven views that render once per frame, at most
View README.md

Microview

What if... Virtual DOM... but by hand?
aha ha, just kidding...
unless.. ?

Microview is a tiny library for writing efficient data-driven DOM rendering logic by hand. DOM writes are driven by a pure data model. Using Microview, you can freely "bash the dom". Writes will be batched — they'll only happen once per animationframe, and only if the data model changes.

@gordonbrander
gordonbrander / framescheduler.js
Created Oct 20, 2020
FrameScheduler - a frame scheduler that will call callback at most once per frame.
View framescheduler.js
// Creates a frame scheduler that will call callback at most once per frame.
export const FrameScheduler = callback => {
let isScheduled = false
const draw = t => {
isScheduled = false
callback(t)
}
const schedule = () => {
@gordonbrander
gordonbrander / contract.js
Last active Feb 10, 2020
Racket-style contracts code sketch
View contract.js
// Simple runtime type checking.
export class ContractViolation extends Error {}
export const contract = predicate => value => {
if (predicate(value)) {
return value
} else {
throw new ContractViolation(
`Contract violation. Expected ${predicate}. Given ${value}.`
)
@gordonbrander
gordonbrander / starting.yaml
Created May 12, 2019
Starting - creative prompts
View starting.yaml
# Prompts for beginning new projects
start:
- "#5ps#"
- "#heart#"
- "#disirability_feasibility_viability#"
- "#practical_step#"
- "#build#"
- "#story#"
- "#ooda#"