Skip to content

Instantly share code, notes, and snippets.

View timruffles's full-sized avatar

Tim Ruffles timruffles

View GitHub Profile
@timruffles
timruffles / operation.ts
Last active April 23, 2021 17:11
operation - like Result and various other ADTs in Haskell etc
export type Operation<T, E> =
typeof NotStarted |
typeof Waiting |
Complete<T> |
Failed<E>
export enum OperationState {
NotStartedState = 'NotStarted',
WaitingState = 'Waiting',
CompleteState = 'Complete',
@timruffles
timruffles / count_functions_loc.sh
Created June 12, 2020 14:09
A bash script that counts go function and method lines of code (including whitespace and comments)
#!/bin/bash
#
# Usage: bash count_functions_loc.sh some/directory
set -euo pipefail
main() {
for f in $(find $1 -name '*.go' -not -name 'test_*.go'); do
count_funcs < "$f" | awk "{ print \"$f\", \$1, \$2 }"
done
@timruffles
timruffles / ban_random_int.sh
Created July 11, 2019 13:33
A bad way to generate a random init in a range using only bash built-ins. Useful in a pinch when you aren't sure what external programs are available.
# Gets an int between min max inclusive very inefficiently, but
# only using bash built-ins. More inefficient the smaller the gap
#
# usage: n=$( bad_random_int 1000 2000 )
bad_random_int() {
local min=$1
local max=$2
local n=0
while [[ "$n" -lt "$min" ]] || [[ "$n" -gt "$max" ]]; do
@timruffles
timruffles / go-quiz.go
Last active August 19, 2019 07:43
What does this program output, and why? Reason it out gophers! Answers in a spoiler block - https://github.com/dear-github/dear-github/issues/166#issuecomment-236342209
package main
import "fmt"
func main() {
type person struct {
nickname string
}
ppl := []person{
@timruffles
timruffles / demo.go
Last active March 7, 2019 17:19
Go - cost of change for methods in application code. App code has many types that work together, requiring mocking.
// Let's define a type with a method that needs a fair few arguments
type Thing struct {}
func (c *Thing) Verb(a A,b B,c C,d D) {}
// We have things that depend on Thing and we want to test them, so to mock it we'll need to
// define an interface and generate a mock
type Thinger interface {
Verb(a A,b B,c C,d D)
}
@timruffles
timruffles / add.sh
Last active February 25, 2019 15:52
Go dep - adding a fork via `source`. Useful when you want to use a private fork of a public repo for instance, without updating all package paths.
SOURCE="github.com/you/thing"
FORK="github.com/other/thing"
PACKAGE="some-package"
CONSTRAINT="aabbcc"
dep ensure -add ${SOURCE}/${PACKAGE}:${FORK}.git@${CONSTRAINT}
@timruffles
timruffles / next.md
Last active February 23, 2024 17:04
Next.js page request handling

How Next.js responds to a page request with HTML

We create the next request handler function via app.getRequestHandler(). This returns a standard express handler, so we use it like expressApp.get('*', middlewareA(), middlewareB(), nextApp.getRequestHandler()).

When the handler is invoked:

  • Server#handleRequest (next-server/server/lib/next-server.js)
    • Parses URL + query string if not already done
  • Server#run
  • Searches for matching route
@timruffles
timruffles / js-values-sketch.c
Created July 13, 2018 07:57
Representing JsValues for JS -> C compiler
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
/**
* So, plan is to store the values for the JS compiler
* in a union, nicking Lua's idea. That way immediate
* values like numbers live inside the JSValue wrapper,
@timruffles
timruffles / enums.ts
Last active June 15, 2017 09:34
string enums (ish, with some caveats) before ts 2.4
// cast the strings as any
enum StringEnum {
a = 'a' as any,
b = 'b' as any,
}
const a = StringEnum.a;
const x = 'a';
const y = 1;
@timruffles
timruffles / story.md
Last active June 14, 2017 17:01
A story, wherein I discover 'share()' in RxJS is a tricky blighter

The docs for .share() state: 'Returns a new Observable that multicasts (shares) the original Observable.'.

This sounds like share() is a lovely simple method that'll make your life easier. However share() is really a trap, for if you have the hubris to use .share() the angry complexity bees that live inside RxJS will swarm out and sting you in the eye (in a rate-limited fashion).

Let's see why. Our goal is to take a cold observable, take only the first item from it, do something to it, and share the resulting observable between 2 subscribers. They should both share the values from our new observable. Easy huh?

{
const coldObservable = Rx.Observable.create(function(observer) {
  console.log('observable created')