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
/// 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 { |
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 Foundation | |
extension Optional { | |
struct NilError: Error { | |
let file: String | |
let line: Int | |
let column: Int | |
let function: String | |
} |
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 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 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 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
// Microrouter based on history.pushState. | |
// All thrills, no frills. | |
// Usage: | |
// | |
// var h = urlstate(callback); | |
// h.push('#foo') | |
function urlstate(callback) { | |
// Since `history.pushState` doesn't fire `popstate`, we can use this function | |
// instead. Will update history state and call `callback` with currently | |
// showing `url`. |
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 $$ = (selector) => document.querySelector(selector); | |
export const $ = (selector) => document.querySelectorAll(selector); | |
// Apply a side effect to `n` items. | |
export const sets = (f, n, ...rest) => { | |
for (var i = 0; i < n.length; i++) f(n[i], ...rest); | |
} | |
export const toggleClass = (el, classname, isAdding) => | |
isAdding ? el.classList.add(classname) : el.classList.remove(classname); |
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
// 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 = () => { |
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
// Takes a function and wraps it, making sure: | |
// | |
// * It enforces `new` | |
// * It returns `this` | |
// | |
// Doing this enables a jQuery coding style for object creation, | |
// where constructor functions become chain-able immediately after | |
// construction, and don't have to be called with `new`. For example: | |
// | |
// var F = decorateCtor(function (a, b, c) { ... }); |
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
// Foldable.js | |
// ============================================================================= | |
// | |
// Fold DOM elements based on whether they match a query string. | |
// | |
// How: | |
// | |
// var foldableParagraphs = document.querySelectorAll('p.foldable'); | |
// foldable(foldableParagraphs) | |
// |
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
var nil = 'indicating a nil value'; | |
function reducePredicatePair(reduction, pair) { | |
// If no reduction has yet been found and the predicate | |
// matches, run the assocated function. | |
if(reduction[0] === nil && pair[0].apply(null, reduction[1])) { | |
reduction[0] = pair[1].apply(null, reduction[1]); | |
} | |
return reduction; | |
} |