Debounce a function when you want it to execute only once after a defined interval of time. If the event occurs multiple times within the interval, the interval is reset each time.
Example A user is typing into an input field and you want to execute a function, such as a call to the server, only when the user stops typing for a certain interval, such as 500ms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Originally taken from https://github.com/mgtitimoli/await-mutex | |
class Mutex { | |
constructor() { | |
this._locking = Promise.resolve(); | |
this._locked = false; | |
} | |
isLocked() { | |
return this._locked; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cache = {} | |
export default getJSONWithCache(url) { | |
return cache[url] ? | |
Promise.resolve(cache[url]) : | |
getJSON(url).then(json => { | |
cache[url] = json | |
return json | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
WHAT: A very simple example of redux + redux-simple-router using Typescript. | |
WHY: The official example found here: https://github.com/rackt/redux-simple-router/tree/1.0.2/examples/basic has problems: | |
1) it is spread over many files making it very hard to "skim" | |
2) it is organized by function, not by feature. (Example: learning "how to manipulate redux state" is spread over 5 files in 4 folders) | |
3) there are no comments explaining what's going on/why. | |
WHO: by JasonS@Novaleaf.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
eventsource "github.com/antage/eventsource/http" | |
redis "github.com/vmihailenco/redis" | |
"log" | |
"net/http" | |
) | |
func haltOnErr(err error){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var listenableMap = new WeakMap(); | |
export function getListenable (object) { | |
if (!listenableMap.has(object)) { | |
listenableMap.set(object, {}); | |
} | |
return listenableMap.get(object); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://thecodebarbarian.com/2015/03/20/callback-hell-is-a-myth | |
// some things changed because I have to copy from image | |
describe('.totalValue', function() | |
it('should calculate the total value of items in a space', function(done){ | |
var table = new Item('table', 'dining room', '1', '3000'); | |
var chair = new Item('chair', 'living room', '3', '300'); | |
var couch = new Item('couch', 'living room', '2', '1100'); | |
var chair2 = new Item('chair', 'dining room', '4', '500'); | |
var bed = new Item('bed', 'bed room', '1','2000'); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var fs = require("fs"); | |
var path = require("path"); | |
var Sequelize = require("sequelize"); | |
var env = process.env.NODE_ENV || "development"; | |
var config = require(__dirname + '/../config/config.json')[env]; | |
var sequelize = new Sequelize(config.database, config.username, config.password, config); | |
var db = {}; |