Skip to content

Instantly share code, notes, and snippets.

View tkissing's full-sized avatar

Timo Kissing tkissing

  • Orange County, CA
  • 08:45 (UTC -07:00)
View GitHub Profile
@tkissing
tkissing / gist:b4944c285c761efeff9e
Created March 10, 2015 21:10
setInterval wrapper that returns "stop" method.
function startInterval(func, freq) {
var id = setInterval(func, freq);
return function() {
if (id) {
clearInterval(id);
id = null;
return true;
}
return false;
}
@tkissing
tkissing / gist:e5fa4908150c82d73131
Last active February 12, 2019 03:41
Singleton JavaScript modules that are easy to test! (Hint: Avoid object literals)
// using AMD syntax here to avoid confusion about scope and file boundaries,
// but the concept translates 1:1 to CommonJS (node/io) modules
define('badSingleton', function(require, exports, module) {
var dependency = require('dependency');
var privateState;
module.exports = {
foo: function() {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<body>
Hello World
</body>
</html>
function A() {
function privateA() {}
function privateB() {}
function publicA() {
// now this.publicB() here, just publicB() - safes typing and even if someone overrides publicB we still use the implementation from here
// of course that latter might not be desired, in which case you'd write this.publicB() here
return publicB() ? privateA() : privateB();
}
this.publicA = publicA;
@tkissing
tkissing / gist:0a642811a783ad0e09be
Created January 7, 2015 18:14
$.Deferred vs new Promise vs Callbacks - be careful you are not rebuilding the hell
// consider the following code
function getInfoDef() {
var result = {something: 'static'};
var def = $.Deferred();
$.ajax('/some-url').then(function(data) {
result.also = data.dynamic;
def.resolve(result);
}).fail(def.reject.bind(def));
@tkissing
tkissing / gist:dacc9e3e9dbbede84296
Last active August 29, 2015 14:07
serve-it - an easy-to-use local http(s) server

A global npm command serve-it that takes a "virtual url path" with an optional port and an optional path (defaulting to cwd). The command starts up an http server (optionally: https) and adds the required configuration to serve the files in that folder (recursively). If the server is already running, it just reconfigures the server at runtime.

Examples:

  • serve-it foo:8081 // serves current working directory at http://localhost:8081/foo
  • serve-it --save bar ./bar/src // serves ./bar/src at http://localhost:$PORT/bar with $PORT being default port from ~/.serve-it/config.json or 8080 if not configured; persists to ~/.serve-it/config.json

An optional --save adds the configuration to ~/.serve-it/config.json Anytime the runtime configuration changes, it is also written to ~/.serve-it/.runtime.json - allowing to restore the last configuration after a crash using a command like serve-it --restore

@tkissing
tkissing / gist:da645abfa7e9fa3bc9ea
Created September 30, 2014 03:10
Chain mandatory and optional async operations
// Problem:
// - We have 2 operations that can and should run in parallel
// - Both operations may finish in arbitrary order
// - one operation is mandatory, the other is optional
// - the result promise is to be resolved with a merge of the intermediate ones, with optional overriding mandatory
// Question:
// Can this be done
// - in a more readable manner than the "result = new Promise..." code block
// - with less code than the "result = new Promise..." code block
@tkissing
tkissing / gist:5ff2aff41ff3c5740545
Created September 25, 2014 18:24
Promise.resolve issue(?)
function ithrow() {
throw Error("Can't do that");
}
p1 = new Promise(function(resolve) {
resolve(ithrow())
});
p1.then(console.log.bind(console, 'p1'), console.warn.bind(console, 'p1'));
nslookup rtcgw.citrixonline.com
tracert rtcgw.citrixonline.com
@tkissing
tkissing / gist:8029250
Created December 18, 2013 20:19
Console script to count and list out globals on your page
/*jslint browser:true, eqeq:true, forin:true, sloppy: true*/
(function (window, document) {
var differences = {},
exceptions = ['addEventListener', 'document', 'location', 'navigator', 'window'],
globals = [],
ignoreList = (window.prompt('Ignore filter (comma sep)?', '$,jQuery,ko') || '').split(','),
iframe = document.createElement('iframe'),
i;
for (i in window) {