Skip to content

Instantly share code, notes, and snippets.

View timruffles's full-sized avatar

Tim Ruffles timruffles

View GitHub Profile
@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')
@timruffles
timruffles / troll_table.sql
Last active June 8, 2017 11:19
I tried to find a limit to Posgres's table+column names. There is none! 👍
scratch=# create table "delete from account;" ("""id""" text primary key, "sutff %\n" text, "&" text, "1" text, U&"\+01F600" text);
CREATE TABLE
scratch=# \d "delete from account;"
Table "public.delete from account;"
Column | Type | Modifiers
-----------+------+-----------
"id" | text | not null
sutff %\n | text |
& | text |
1 | text |
@timruffles
timruffles / post-commit.sh
Created January 7, 2013 11:06
.git/hooks/post-commit to automatically label referenced issues in github issues (you'll probably not want to close issues immediately if you have a QA process, haven't deployed yet etc) put it in the path above, and chmod +x to have it executable.
#!/bin/bash
commit_msg() {
git log -1 HEAD --format=%s
}
find_issues() {
grep --color=never -oP "(?<=\#)\d+"
}
@timruffles
timruffles / observables.js
Created January 23, 2017 12:42
Functional Observables
{
/*
interface Observer<T> {
next(t: T): void;
error(e: Error): void
complete(): void;
}
*/
@timruffles
timruffles / hoist.js
Created January 5, 2017 21:31
hoistage
// Difference between lintability/RTE of hoisting vs non-hoisted code
// ReferenceError: foo is not defined
// at <anonymous>:3:5
try {
foo()
const x = 1;
const foo = () => x;
} catch(e) {
console.log(e)
@timruffles
timruffles / 01_mapValues.js
Last active December 2, 2016 10:43
mapValues -> Functor for functions
// (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);
@timruffles
timruffles / gist:3491629
Created August 27, 2012 19:35
getting google spreadsheet worksheet gids
https://spreadsheets.google.com/feeds/worksheets/ KEY /public/basic
https://spreadsheets.google.com/feeds/worksheets/0AmE0pIAjxxOvdFNocHMtd256aWg4cFpTZ005akhnQWc/public/basic?alt=json-in-script&callback=foo
@timruffles
timruffles / auth.md
Last active July 13, 2016 10:42
proper auth handling

Proper client-side auth

At the mo we have the boilerplate project's getCurrentUser() method, that synchronously returns the user. If it's not fetched yet, bad luck!

This isn't good. We should handle:

  1. waiting to confirm if we're logged in or not. (unless we block the rest of app startup till we know, not too much of a UX cost)
  2. currently this happens at startup
  3. we could embed this on app startup, as we generate the index.html client-side now, e.g window.USER = { id: 15, name: "bob... }
  4. expiration (i.e starting to get 401 Unauthorized responses)
@timruffles
timruffles / bday-paradox.js
Created June 22, 2016 13:35
empirical birthday paradox calculator - for hashes
const hashBits = parseInt(process.env.bits || 32);
const hashValues = Math.pow(2, hashBits);
const checkPoints = Array.from({ length: 9 }, (e, i) => 1 - (i+1)/10);
for(var uniqueHashValuesRemaining = hashValues, p = 1;
p > 0.1;
uniqueHashValuesRemaining -= 1, p *= uniqueHashValuesRemaining/hashValues
@timruffles
timruffles / dumb-es6-bench.js
Last active June 16, 2016 09:29
dumb micro bench of es5 vs es6 key value stores. combined set/get. paste into a console or `pbpaste | node` near you! I got es6: 57.397ms es5: 92.984ms for node 6.0.0
var m = new Map;
var o = {};
var value;
var rs = [];
var i = 1e5;
while(i--) rs.push(Math.random().toString())
var i = 1e5; console.time("es6");
while(i--) {