Skip to content

Instantly share code, notes, and snippets.

Avatar
👻
A ghost

Nathan myobie

👻
A ghost
View GitHub Profile
View headless.js
const handler = require('serve-handler')
const { createServer } = require('http')
const launcher = require('chrome-launcher')
const remoteInterface = require('chrome-remote-interface')
let exitStatus = 0
const baseURL = 'http://localhost:6000'
const server = createServer((request, response) => {
@myobie
myobie / state.js
Last active Apr 16, 2020
A not very sophisticated "store" where I can dispatch "actions" if a very typesafe way
View state.js
// Fake immer
/** @template T */
export class State {
/** @typedef {function(T):void} Callback */
/** @typedef {function(): T} InitialValueFactory */
/**
* @param {T | InitialValueFactory} initialValue
*/
@myobie
myobie / dropzone.js
Last active Apr 15, 2020
Gives one a div which will overlay the page when a file is dragged onto the window and lets one specify a callback which will receive the files which were dropped if any (using a new as-of-yet-unpublished state library)
View dropzone.js
import { html, css, globalCSS } from './html.js'
import { State } from './state.js'
const isDraggingOver = new State(false)
let runOnce = false
isDraggingOver.onUpdate(newState => {
const root = document.documentElement
const dropzoneDisplay = newState ? 'block' : 'none'
root.style.setProperty('--dropzone-display', dropzoneDisplay)
@myobie
myobie / simple-state.js
Last active Apr 14, 2020
Very simple state class with an update callback
View simple-state.js
/** @template T */
class State {
/** @typedef {function(T):void} Callback */
/**
* @param {T} initialValue
*/
constructor (initialValue) {
this.value = initialValue
@myobie
myobie / likes.js
Last active Apr 11, 2020
Prototyping the dream code for a new frontend framework
View likes.js
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 Apr 10, 2020
Wrap an async generator with index generation
View gen-indexes.js
/**
* @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 Apr 4, 2020
Outline for my blog post about getting started with wasm
View outline.md
  • 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 Feb 17, 2020
GCD Timer using DispatchSourceTimer that is much simpler
View timer.swift
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 Feb 15, 2020
GCD Timer using DispatchSourceTimer
View timer.swift
import Cocoa
import Combine
class Timer: Cancellable, Publisher {
enum Error: Swift.Error {
case cancelled
}
typealias Output = Never
typealias Failure = Error
You can’t perform that action at this time.