Skip to content

Instantly share code, notes, and snippets.

@myobie
myobie / create-state.ts
Last active July 2, 2020 11:20
Make a little state object which can be updated through Immer's produce
import produce, { Draft, Immutable } from 'immer'
type Updater<S> = (state: Draft<S>) => void | S
type UpdateCallback<S> = (state: Immutable<S>) => void
type State<T> = {
current: Immutable<T>;
update: (updater: Updater<T>) => void;
onUpdate: (cb: UpdateCallback<T>) => void;
}
@myobie
myobie / .gitignore
Last active June 5, 2020 07:13
Download and compile libsodium to wasm using wasi-sdk
/tmp/
/lib/
@myobie
myobie / .gitignore
Last active June 3, 2020 11:33
wasi-sdk release binaries example
/src/
/tmp/
@myobie
myobie / ci.yml
Created May 27, 2020 15:31
Extremely satisfying to have playwright running on all OS's for all browsers
on: [push]
jobs:
lint-and-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
@myobie
myobie / next-tick.ts
Last active May 6, 2020 19:14
Do some work next time the event loop is free
export function nextTick<O> (cb: () => O | Promise<O>): Promise<O> {
return new Promise((resolve, reject) => {
requestAnimationFrame(() => {
Promise.resolve(cb())
.then(value => resolve(value))
.catch(e => reject(e))
})
})
}
@myobie
myobie / uuids.ex
Created April 27, 2020 16:13
Save 14 characters for UUIDs
def generate_id do
result =
Ecto.UUID.generate()
|> String.replace(~r/-+/, "")
|> Base.decode16(case: :lower)
case result do
:error ->
throw("Something went wrong generating a uuid")
defmodule Example.RandomTest do
use ExUnit.Case
test "loads wasm file" do
{:ok, bytes} = File.read("priv/vendor/needs-random.wasm")
{:ok, agent} = Agent.start_link(fn -> nil end)
{:ok, instance} =
Wasmex.start_link(%{
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 April 16, 2020 15:13
A not very sophisticated "store" where I can dispatch "actions" if a very typesafe way
// 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 April 15, 2020 11:32
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)
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)