Skip to content

Instantly share code, notes, and snippets.

View myobie's full-sized avatar

Nathan myobie

View GitHub Profile
@myobie
myobie / script.lua
Created October 3, 2023 22:38
wrk lua script to output the count of response http status codes
View script.lua
local threads = {}
function setup(thread)
-- track each thread
table.insert(threads, thread)
end
function init(args)
-- thread local statuses table
statuses = {["100"] = 0}
@myobie
myobie / ci.yml
Last active September 20, 2023 09:26
Don't run GitHub Actions for pull requests that are drafts
View ci.yml
on:
push:
branches:
- master
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
@myobie
myobie / html-import.js
Last active September 4, 2023 12:38
Dreaming of HTML imports 🤔
View html-import.js
// Example usage:
// <script type="module" src="./html-import.js"></script>
// <html-import href="./example.html" runscripts></html-import>
// <!-- now we can use the component defined from example.html -->
// <example-custom-element><p>This paragraph will be slotted.</p></example-custom-element>
export class HTMLImport extends HTMLElement {
static {
customElements.define('html-import', this)
}
@myobie
myobie / ext-integer.ex
Last active August 29, 2023 16:31
Allow collecting into an integer for sums in elixir
View ext-integer.ex
defmodule ExtInteger do
defimpl Collectable, for: Integer do
@doc """
Collect into: an integer
## Examples
iex> for n <- 1..5, into: 0, do: n
15
"""
@myobie
myobie / useStateWithReactiveInput.ts
Last active July 7, 2023 16:02 — forked from schickling/useStateWithReactiveInput.ts
Reactive `useState` variant
View useStateWithReactiveInput.ts
import React from 'react'
/**
* A variant of `React.useState` which allows the `inputState` to change over time as well.
* Important: This hook is synchronous / single-render-pass (i.e. doesn't use `useEffect` or `setState` directly).
*/
const useStateWithReactiveInput = <T>(inputState: T): [T, (newState: T | ((prev: T) => T)) => void] => {
const [_, rerender] = React.useState(0)
const stateRef = React.useMemo<{ current: T }>(() => ({ current: inputState }), [])
@myobie
myobie / application_helper.rb
Created May 19, 2011 17:11
My try at a good presenter pattern with rails
View application_helper.rb
module ApplicationHelper
def present(model, items = {})
name = model.is_a?(String) ? model : model.class.model_name.underscore
items.merge!(name.to_sym => model) unless model.is_a?(String)
"#{name.classify}Presenter".constantize.new(controller, items) do |presenter|
yield(presenter) if block_given?
end
@myobie
myobie / full-stacktrace.txt
Created June 19, 2023 19:03
Internal application error: TypeError: cannot read headers: request closed
View full-stacktrace.txt
Internal application error: TypeError: cannot read headers: request closed
at Object.get headerList (ext:deno_fetch/23_request.js:115:17)
at Request.request.<computed> (ext:deno_fetch/23_request.js:565:60)
at Request.get [headers] (ext:deno_fetch/23_request.js:252:46)
at Request.get headers (ext:deno_fetch/23_request.js:452:16)
at NativeRequest.get headers (https://deno.land/x/oak@v12.5.0/http_server_native_request.ts:72:26)
at new Request (https://deno.land/x/oak@v12.5.0/request.ts:150:21)
at new Context (https://deno.land/x/oak@v12.5.0/context.ts:172:20)
at Application.#handleRequest (https://deno.land/x/oak@v12.5.0/application.ts:450:17)
at Application.listen (https://deno.land/x/oak@v12.5.0/application.ts:656:28)
@myobie
myobie / application.ex
Last active May 8, 2023 23:21
Using Finch with ExAws
View application.ex
defmodule Example.Application do
@moduledoc false
use Application
def start(_type, _args) do
children = [
Example.Repo,
ExampleWeb.Telemetry,
{Phoenix.PubSub, name: Example.PubSub},
@myobie
myobie / effect-with-previous-value.ts
Created April 4, 2023 09:09
Sometimes I want to know the previous value for an effect
View effect-with-previous-value.ts
import { effect } from '@preact/signals-core'
export function effectWithPreviousValue<T>(
initialValue: T,
compute: (prev: T) => T
): () => void {
let prev = initialValue
return effect(() => {
prev = compute(prev)
@myobie
myobie / complex-signal.ts
Last active March 29, 2023 11:09
A simple, non-performance-optimized way to keep a Set, Map, etc in a Signal
View complex-signal.ts
import { signal } from '@preact/signals'
export type ComplexSignal<T> = {
value: T
peek: T
subscribe: (cb: (t: T) => void) => () => void
update: (cb: (t: T) => void) => void
}
export function complexSignal<T>(initialValue: T): ComplexSignal<T> {