Skip to content

Instantly share code, notes, and snippets.

Tim Ruffles timruffles

View GitHub Profile
timruffles / demo.go
Last active Mar 7, 2019
Go - cost of change for methods in application code. App code has many types that work together, requiring mocking.
View demo.go
// 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 /
Last active Feb 25, 2019
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.
dep ensure -add ${SOURCE}/${PACKAGE}:${FORK}.git@${CONSTRAINT}
timruffles /
Last active Nov 27, 2018
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 / js-values-sketch.c
Created Jul 13, 2018
Representing JsValues for JS -> C compiler
View js-values-sketch.c
#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 / enums.ts
Last active Jun 15, 2017
string enums (ish, with some caveats) before ts 2.4
View enums.ts
// 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 /
Last active Jun 14, 2017
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')
timruffles / troll_table.sql
Last active Jun 8, 2017
I tried to find a limit to Posgres's table+column names. There is none! 👍
View troll_table.sql
scratch=# create table "delete from account;" ("""id""" text primary key, "sutff %\n" text, "&" text, "1" text, U&"\+01F600" text);
scratch=# \d "delete from account;"
Table "public.delete from account;"
Column | Type | Modifiers
"id" | text | not null
sutff %\n | text |
& | text |
1 | text |
timruffles / observables.js
Created Jan 23, 2017
Functional Observables
View observables.js
interface Observer<T> {
next(t: T): void;
error(e: Error): void
complete(): void;
View hoist.js
// Difference between lintability/RTE of hoisting vs non-hoisted code
// ReferenceError: foo is not defined
// at <anonymous>:3:5
try {
const x = 1;
const foo = () => x;
} catch(e) {
timruffles / 01_mapValues.js
Last active Dec 2, 2016
mapValues -> Functor for functions
View 01_mapValues.js
// (a -> a) -> (a -> b) -> (a -> b)
var mapValue = transform => fn => v => fn(transform(v));
// (string -> string) - so string is a above
var louder = x => x.toUpperCase();
// (string -> Element) - so element is b above
var hi = (m) => ({ type: 'h1', value: m })
// (string -> Element) - so we end up with a (a -> b) again
var titleLoud = mapValue(louder)(h1);
You can’t perform that action at this time.