Skip to content

Instantly share code, notes, and snippets.

View matt-curtis's full-sized avatar

Matt Curtis matt-curtis

View GitHub Profile

Cheat sheet: modules

Named exports, named imports, namespace imports

If we put export in front of a named entity inside a module, it becomes a named export of that module. All other entities are private to the module.

//===== lib1.mjs =====
// Named exports
@steipete
steipete / TouchBarSwiftUIHack.swift
Created April 19, 2021 15:17
Your SwiftUI app crashes in *** Assertion failure in -[NSTouchBarLayout setLeadingWidgetWidth:], NSTouchBarLayout.m:78 ? Use this hack to work around the problem!
import Foundation
import InterposeKit
import OSLog
/// Hack tow work around Assertion failure in -[NSTouchBarLayout setLeadingWidgetWidth:], NSTouchBarLayout.m:78
/// This sometimes happens when macOS restores a window.
/// This even runs if there is no OS-level touch bar.
class MacOSWorkarounds {
static let logger = Logger(category: "MacOSWorkarounds")
@IanKeen
IanKeen / 00_Example_Array.swift
Last active September 3, 2021 13:25
PropertyWrapper: Dealing with heterogenous arrays (non-property wrapper approach as well)
struct Foo: Codable {
let type: String
let value: String
}
struct Bar: Codable {
let type: String
let value: Int
}
protocol Item { }
@FWEugene
FWEugene / SwiftConcurrency.md
Created January 10, 2019 17:37
All about concurrency

Threads

Foundation offers a Thread class, internally based on pthread, that can be used to create new threads and execute closures.

// Detaches a new thread and uses the specified selector as the thread entry point.
Thread.detachNewThreadSelector(selector: Selector>, toTarget: Any, with: Any)

// Subclass
class MyThread: Thread {
@marcedwards
marcedwards / spinner.pde
Last active November 22, 2020 22:20
A loading spinner for Processing 3.3.7
// A loading spinner.
// By @marcedwards from @bjango.
void setup() {
size(300, 300, P2D);
frameRate(30);
smooth(8);
noFill();
stroke(255);
strokeWeight(6);
@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active April 26, 2024 10:15
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@smileyborg
smileyborg / InteractiveTransitionCollectionViewDeselection.m
Last active January 15, 2023 13:03
Animate table & collection view deselection alongside interactive transition (for iOS 11 and later)
// UICollectionView Objective-C example
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] firstObject];
if (selectedIndexPath != nil) {
id<UIViewControllerTransitionCoordinator> coordinator = self.transitionCoordinator;
if (coordinator != nil) {
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
import UIKit
import Cartography // https://github.com/robb/Cartography
/**
This is an example of self sizing `UICollectionView` cells using AutoLayout,
where the width of cells is always the width of the parent, to mimic `UITableView`.
*/
fileprivate let items: [String] = (0..<100)
.map { _ in Lorem.sentences(Int.random(min: 1, max: 8)) } // Using https://github.com/lukaskubanek/LoremSwiftum/blob/master/Sources/LoremSwiftum.swift
@shaps80
shaps80 / InfiniteIterator.swift
Created October 20, 2017 12:08
An iterator that automatically wraps back to the `firstIndex` after the `endIndex` is reached.
import Foundation
public struct InfiniteIterator<Base: Collection>: IteratorProtocol {
private let collection: Base
private var index: Base.Index
public init(collection: Base) {
self.collection = collection
self.index = collection.startIndex
@matt-curtis
matt-curtis / accept-all-suggestions.js
Created February 28, 2017 21:23
Google Docs - Accept all suggestions
function run(){
var shouldRun = confirm("Are you sure you want to accept all suggestions?\n\n(Note: Depending on the number of suggestions you have, this script may take a few seconds to run.)");
if(!shouldRun) return false;
var events = [ "mouseover", "mousedown", "click", "mouseup" ];
var suggestionButtons = Array.from(document.querySelectorAll("[role='button'][aria-label='Accept suggestion']"));
suggestionButtons.forEach((el) => {
events.forEach((eventName, i) => {