Skip to content

Instantly share code, notes, and snippets.

@brigand
brigand / nonBlockingGenerator.js
Last active February 2, 2017 04:31
This allows doing expensive computation, periodically yielding to the event loop, while keeping synchronous syntax
function nonBlocking(generator) {
return (...args) => {
var callback = args[args.length - 1];
var iter = generator(...args);
function next() {
try {
var res = iter.next();
}
catch (e) {
callback(e);
@brigand
brigand / apis.md
Last active May 27, 2017 07:38
Useful http apis for sample projects and experiments

Reddit

An api for accessing reddit information. Supports CORS. Doesn't require auth. Great for learning new frontend frameworks.

https://www.reddit.com/dev/api/

Hacker News

@brigand
brigand / .md
Last active August 26, 2016 20:28
immutable updates methods

Let's explore a few ways to do immutable updates in js. We'll start with this data:

var data = {
  foo: {
    bar: 1,
    baz: 2,
  }
};
function providesTimer(Component) {
return class Timer extends React.Component {
constructor(props) {
super(props);
this.timers = [];
this.setTimeout = this.setTimeout.bind(this);
}
setTimeout(callback, ms) {
@brigand
brigand / updateIfChanged.md
Last active April 27, 2016 14:30
updateIfChanged

updateIfChanged is a pattern that allows preventing updates based on a unique key. It inverts control of updates from a dumb child to the smarter parent. It's particularly useful because it can avoid the normal issue of children and callbacks causing a rerender when it's not truly required.

The alternative workaround is usually memoizing the children and callbacks which can be expensive, complicated, and leak memory.

Implementation

import deepEqual from 'lodash/isEqual';
@brigand
brigand / 1.kittyType.js
Last active March 18, 2016 16:59
Example structure for express applications with mongoose, but applies with other databases
// src/models/types/kittyType.js
var mongoose = require('mongoose');
var kittySchema = exports.schema = mongoose.Schema({
name: String
});
var Kitten = exports.model = mongoose.model('Kitten', kittySchema);
@brigand
brigand / 1.cache.js
Last active March 14, 2016 00:05
react-obcache - api redesign
import ObCache from 'obcache';
var cache = new ObCache();
cache.register('userInfo', ([id]) => functionThatReturnsAPromise(id));
export default cache;
@brigand
brigand / nickname.md
Created March 9, 2016 07:45
Freenode Setting Up Your Nickname

What is the recommended way to set up my IRC nickname?

Please follow these steps to set up your nick and configure your client. Check off each step to make sure it's been done:

Select a permanent, master nickname. If the nickname you want is registered but has expired, just ask a staffer and in most cases, we will be happy to drop it for you.

Please avoid using the name of a community project or trademarked entity, to avoid conflicts. Write down your password and be sure to keep the sheet of paper in a safe place.

Register your IRC nick:

@brigand
brigand / uu.md
Last active March 10, 2016 16:07
uu - a cli tool for writing readable bash scripts

UU basics

UU supports many basic commands. Here are some. Writing UU scripts requires some knowledge but reading and modifying them should be easy for people familiar with english and one or more programming languages.

uu with "$path" split-on / get -1
echo foo 1 bar 2 baz | uu with-stdin spit-on ' ' match '\w+' print %s
echo '{"key": "value"}' | uu with-stdin json-get 'key' print %s
echo '{"key": "value"}' | uu with-stdin json-set 'key' 'value2' print %j 
@brigand
brigand / 1.App.js
Created February 17, 2016 00:49
r2d2 a new api for defining react components
import Immutable from 'immutable';
import {makeComponent} from 'r2d2';
import TodoList from './TodoList.js';
makeComponent('TodoList')
.state('items', Immutable.List([]))
.render(({state, utils}) => {
return (
<div>
<h1>Todo</h1>