Skip to content

Instantly share code, notes, and snippets.

@myobie
myobie / timer.swift
Created February 17, 2020 21:27
GCD Timer using DispatchSourceTimer that is much simpler
import Foundation
class Timer {
private let source: DispatchSourceTimer
private let block: () -> ()
public let isRepeating: Bool
init(_ interval: DispatchTimeInterval, repeat shouldRepeat: Bool = false, block: @escaping () -> ()) {
self.source = DispatchSource.makeTimerSource()
@myobie
myobie / timer.swift
Last active August 21, 2020 04:43
GCD Timer using DispatchSourceTimer
import Cocoa
import Combine
class Timer: Cancellable, Publisher {
enum Error: Swift.Error {
case cancelled
}
typealias Output = Never
typealias Failure = Error
@myobie
myobie / _add_permalink_links_to_every_heading_with_an_id
Last active January 16, 2020 18:10
Insert a link to the id of any heading which has an id
@myobie
myobie / fig-shortcode.html
Created November 21, 2019 14:21
A shortcode for Hugo to render a figure + img supporting 2x and 3x images automatically from the filename (max size is hard coded as 1200px)
{{- $original := .Page.Resources.GetMatch (printf "*%s*" (.Get "src")) -}}
{{- if findRE ".*@2x*." $original.Name -}}
{{- .Scratch.Set "image" ($original.Fit "2400x2400 q40") -}}
{{- else if findRE ".*@3x*." $original.Name -}}
{{- .Scratch.Set "image" ($original.Fit "3600x3600 q40") -}}
{{- else -}}
{{- .Scratch.Set "image" ($original.Fit "1200x1200 q55") -}}
{{- end -}}
@myobie
myobie / now.yml
Last active January 7, 2020 14:50
Zeit's now preview and prod deploy Actions Workflow – emulates how Zeit's Now GitHub integration works, but is even better because it forces a rebuild even if the source code hasn't changed
name: now
on:
push:
jobs:
deploy-prod:
name: Deploy prod
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
steps:
@myobie
myobie / _zeit_now_plus_hugo
Last active November 13, 2019 17:03
Zeit Now + Hugo current best setup
🎉
@myobie
myobie / crap.js
Created September 20, 2019 22:51
const hyperx = require('hyperx')
const hx = hyperx((componentName, properties, children) => {
return ['createElement', componentName, properties, children]
}, (children) => {
return ['createFragment', children]
})
class Header {
async fetch (ctx) {
@myobie
myobie / colorize.js
Last active August 2, 2019 16:42
How the color changing works on nathanherald.com
function sorter () { return 0.5 - Math.random() }
var styles = [ 'blaze', 'sunshine', 'dusk', 'blood', 'puce' ]
function colorize () {
var style = styles.sort(sorter)[0]
document.body.className = style
}
colorize()
setInterval(colorize, 10000)
@myobie
myobie / URLAppendingQueryItems.swift
Created July 17, 2019 14:17
Append query items just as easily as path components
extension URL {
func appendingQueryItems(_ items: [String: String]) -> URL {
guard var components = URLComponents(url: self, resolvingAgainstBaseURL: false) else {
fatalError()
}
var query = components.queryItems ?? []
items.forEach { (name, value) in
query.append(URLQueryItem(name: name, value: value))
@myobie
myobie / ExampleSimplePublisher.swift
Last active April 7, 2020 11:33
Make any class a super simple publisher with publish() and complete() functions
class Names: SimplePublisher {
typealias Output = Result<String, Error>
var subscriptions = [SimpleSubscription<Output, Failure>]()
let names = ["Alice", "Bob", "Claris", "Doug"]
shoutName() {
if let name = names.randomElement() {
publish(.success(name))
} else {
assertionFailure("There are def some names in there...")