Skip to content

Instantly share code, notes, and snippets.

View blixt's full-sized avatar
🗺️
Exploring

Blixt blixt

🗺️
Exploring
View GitHub Profile
@blixt
blixt / getPath.js
Created April 23, 2015 21:13
getPath+
/*
The MIT License (MIT)
Copyright (c) 2010 Blixt
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
@blixt
blixt / waiter.js
Created October 10, 2012 22:33
Waiters (promises/deferreds/futures)
function Waiter(context) {
if (context) { this.context = context; }
}
Waiter.prototype.failed = function (cb) {
if (this.result) {
if (!this.success) { cb.apply(this.context || this, this.result); }
} else {
if (!this.failedCbs) { this.failedCbs = []; }
this.failedCbs.push(cb);
@blixt
blixt / index.js
Last active December 29, 2020 14:32
requirebin sketch
var procedural = require('procedural');
var avatar = procedural('avatar')
// The block size is just visual, so it shouldn't affect randomization.
.doNotHash('blockSize')
// The username is needed to create a unique avatar for every user.
.takes('username')
// Size, in blocks. Different sizes will create different avatars.
.takes('size', function validate(avatar, blocks) {
// Ensure that size is a positive integer divisible by 2.
@blixt
blixt / procedural-avatar.js
Last active December 29, 2020 14:32
Procedural avatars using procedural.
var avatar = procedural('avatar')
.takes('username')
// Size, in blocks.
.takes('size', function validate(avatar, blocks) {
return typeof blocks == 'number' && blocks > 0;
})
// The pixel size of a single (square) block.
.takes('blockSize', function validate(avatar, px) {
return typeof px == 'number' && px > 0;
})
@blixt
blixt / dedupe.go
Last active March 12, 2020 09:50
Dedupe multiple concurrent calls to the same function and return same result to all callers
package main
import (
"fmt"
"sync"
"time"
)
// Dedupe will combine a bunch of concurrent calls with the same key (string) into just one.
// Example:
import { useEffect, useRef, useState } from "react"
interface IO<T> {
setters: ((value: T) => void)[]
update: (value: T) => void
value: T | undefined
}
const store: { [name: string]: IO<any> | undefined } = {}
@blixt
blixt / modifyresponse_test.go
Created January 9, 2019 11:10
Testing ModifyResponse with ReverseProxy and web sockets
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
@blixt
blixt / transform-class.ts
Created November 22, 2018 18:02
Did you know TypeScript's type system is Turing complete?
// Return a type with only the methods of T that return R.
type MethodsWithReturnType<T, R> = {
[P in keyof T]: T[P] extends () => infer RR ? (RR extends R ? P : never) : never
}[keyof T]
// Filter away all keys except methods that return void and
// return a new type with those methods returning booleans.
type VoidMethodsToBoolMethods<T> = { [P in MethodsWithReturnType<T, void>]: () => boolean }
// Our test input class:
@blixt
blixt / progress.py
Last active July 5, 2018 16:38
Simple progress indicator copy/paste function for Python scripts
# -*- coding: utf-8 -*-
import sys
_last_status = None
_last_status_permille = 0
def print_status(status=None, fraction=0):
global _last_status, _last_status_permille
permille = int(fraction * 1000)
if status != _last_status:
@blixt
blixt / docker-antics.bash
Last active June 11, 2018 17:55
Workaround for copying files in same directory in separate layers (without specifying individual files)
# First, outside the Dockerfile:
tar --exclude ./static-large --exclude-from=.dockerignore -czf non-large.tar.gz ./*
# Then, in the Dockerfile:
COPY static-large $WORKDIR_PATH/static-large/
ADD non-large.tar.gz $WORKDIR_PATH/