Skip to content

Instantly share code, notes, and snippets.

@myobie
myobie / simple-state.js
Last active April 14, 2020 08:21
Very simple state class with an update callback
/** @template T */
class State {
/** @typedef {function(T):void} Callback */
/**
* @param {T} initialValue
*/
constructor (initialValue) {
this.value = initialValue
@myobie
myobie / likes.js
Last active April 11, 2020 16:24
Prototyping the dream code for a new frontend framework
import { createStorage } from './bassics/storage'
import { html, until, render } from './bassics/html'
const storage = createStorage()
const likes = storage.state('likes', fetchLikes())
function reloadLikes () {
return likes.update(fetchLikes())
}
@myobie
myobie / gen-indexes.js
Created April 10, 2020 18:12
Wrap an async generator with index generation
/**
* @template T
* @param {AsyncGenerator<T, void, void>} gen
* @returns {AsyncGenerator<[T, number], void, void>}
*/
export async function * genIndexes (gen) {
let i = 0
for await (const item of gen) {
yield [item, i]
i++
@myobie
myobie / outline.md
Created April 4, 2020 17:02
Outline for my blog post about getting started with wasm
  • Why?
    • Use existing code and libraries, especially for encryption or hashing
    • Sharing code for algorithms or compute intensive operations
    • Don’t intend expose much (anything?) to the wasm instance
    • There some gotchas like 64bit integers
  • Browser only, no toolchain
    • I have toolchain fatigue and it turns out the latest browsers are kinda great
    • import and export, async and await, wasm, etc all work
    • I like to use a tiny file server to serve a directory on a port on localhost
  • serve
@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
🎉