Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
gordonbrander / rendering-element.js
Created March 6, 2023 13:39
RenderingElement - A custom element that batches updates on animation frame
// 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.
@gordonbrander
gordonbrander / mut.js
Last active March 6, 2023 16:13
Mut.js - efficient DOM writing via mutation and version flag
const $version = Symbol('version')
export const version = state => {
if state[$version] == null {
return 0
}
return state[$version]
}
export const markSynced = (following, leading) => {
@gordonbrander
gordonbrander / DispatchQueue+future.swift
Created February 16, 2023 19:22
DispatchQueue.future
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
@gordonbrander
gordonbrander / Store.swift
Last active February 17, 2022 01:55
Store.swift - a simple Elm-like ObservableObject store for SwiftUI
//
// 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"),
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
@gordonbrander
gordonbrander / UnwrapOptional.swift
Created June 15, 2021 13:51
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 20:48
SwiftUI immutable ViewStore
/// 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 November 6, 2022 09:19
dirty.js - dirty marking for any JS object
/*
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
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 April 25, 2023 20:38
microview - data-driven views that render once per frame, at most

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.