Skip to content

Instantly share code, notes, and snippets.

@jonastemplestein
jonastemplestein / node-callAsync.js
Created August 10, 2010 23:25
Nifty function that generates a callback that cannot be executed synchronously
/**
* Nifty function that generates a callback that cannot be executed synchronously
* This is useful to circumvent the behaviour of crappy libraries that are not always async
* (e.g. if they cache certain results)
*/
exports.callAsync = function(original_callback) {
// capture scope
var self = this;
return function() {
var args = arguments;
@jonastemplestein
jonastemplestein / once.js
Created September 6, 2010 10:23
I use this for Jake dependencies that only need to run once.
/**
* makes sure a function is only executed once
* all future runs of the function return the same result instantly
* I use this for Jakefiles where tasks should not run twice as dependencies
*
* by @jonashuckestein
*/
var once = function(fn) {
var self = this;
var ran_once = false;
@jonastemplestein
jonastemplestein / fb_auth.coffee
Created November 27, 2010 00:36
Node middleware that decodes FB cookies
crypto = require 'crypto'
class FBSession
constructor: (@app_id, @app_secret) ->
@state = 'logged_out'
initialize: (req) =>
@req = req
req.fbSession = () => if @state is 'logged_in' then this else null
@jonastemplestein
jonastemplestein / node-object-sync-client-example.html
Created February 6, 2011 09:22
node-object-sync client example
<script src="socket.io/socket.io.js"></script>
<script src="/coffee/object-sync-client.js"></script>
<script>
sync = new ObjectSync();
sync.connect();
sync.on('update', function(obj) {
switch(obj.type) {
case 'player':
drawPlayer(obj);
break;
@jonastemplestein
jonastemplestein / node-object-sync-server-example.coffee
Created February 6, 2011 09:23
node-object-sync server example
ObjectSync = require 'object-sync'
server = http.createServer()
# Hook an ObjectSync instance up to our server.
# Define a bunch of handlers for CRUD events. The handlers are
# async because they'll likely interact with some kind of database
sync = ObjectSync.listen server,
# a client wants to delete object with id id
destroy: (id, client_sid, callback) ->
@jonastemplestein
jonastemplestein / EventEmitter.coffee
Created February 6, 2011 09:37
A CoffeeScript port of the nodejs EventEmitter
isArray = Array.isArray or (obj) ->
obj.constructor.toString().indexOf("Array") isnt -1
default_max_listeners = 10
class EventEmitter
setMaxListeners: (n) ->
@_events.maxListeners = n
emit: (type) ->
@jonastemplestein
jonastemplestein / failing_spec.rb
Last active August 29, 2015 13:57
BUG: counter_cache updates counter twice on assignment
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
-- Logs begin at Fri 2014-08-08 18:44:18 UTC. --
Aug 09 18:39:43 ip-10-0-59-226.ec2.internal sh[14005]: File "/app/api/models.py", line 100, in _get_scheduler
Aug 09 18:39:43 ip-10-0-59-226.ec2.internal sh[14005]: self.domain, self.options)
Aug 09 18:39:43 ip-10-0-59-226.ec2.internal sh[14005]: File "/app/scheduler/coreos.py", line 26, in __init__
Aug 09 18:39:43 ip-10-0-59-226.ec2.internal sh[14005]: f.write(base64.b64decode(auth))
Aug 09 18:39:43 ip-10-0-59-226.ec2.internal sh[14005]: File "/usr/lib/python2.7/base64.py", line 76, in b64decode
Aug 09 18:39:43 ip-10-0-59-226.ec2.internal sh[14005]: raise TypeError(msg)
Aug 09 18:39:43 ip-10-0-59-226.ec2.internal sh[14005]: TypeError: Incorrect padding
Aug 09 18:39:43 ip-10-0-59-226.ec2.internal sh[14005]: [2014-08-09 12:39:43,935: INFO/MainProcess] Task api.tasks.stop_containers[07ea8c60-ef16-4bcc-ac86-b772c2522ab4] succeeded in 0.0510701110034s: None
Aug 09 18:39:44 ip-10-0-59-226.ec2.internal sh[14005]: INFO yogurt-bagpiper: Containers scaled web=1