Skip to content

Instantly share code, notes, and snippets.

View yocontra's full-sized avatar

contra yocontra

View GitHub Profile
@yocontra
yocontra / native.cc
Created April 24, 2012 08:12
Emitting events from a native node/v8 module
/*
You need to mix EventEmitter into MyObject from the JS end before you can emit/listen for events
*/
bool MyObject::Emit(const char *event, int argCount, Handle<Value> emitArgs[])
{
HandleScope scope;
//Format arguments to pass to v8::Function
int nArgCount = argCount + 1;
Handle<Value> *nEmitArgs = new Handle<Value>[nArgCount];
@yocontra
yocontra / stuff.coffee
Created April 30, 2012 03:53
Number of seconds to pretty date
prettySeconds = (secs) ->
days = Math.floor secs / 86400
hours = Math.floor (secs % 86400) / 3600
minutes = Math.floor ((secs % 86400) % 3600) / 60
seconds = ((secs % 86400) % 3600) % 60
out = ""
out += "#{days} days " if days > 0
out += "#{hours} hours " if hours > 0
out += "#{minutes} minutes" if minutes > 0
out += " #{seconds} seconds" if seconds > 0 and days <= 0
@yocontra
yocontra / mock.coffee
Created May 15, 2012 18:02
dermis view mock
define
effect: "fade"
construct: -> # anything before render
render: require("templates/mock")
destruct: -> # clean up here
@yocontra
yocontra / xmlhttprequest.js
Created May 16, 2012 10:49
Tiny XMLHTTPRequest shim
var ids = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP", "Msxml2.XMLHTTP.4.0"];
if (typeof XMLHttpRequest === "undefined") {
for (var i = 0; i < ids.length; i++) {
try {
new ActiveXObject(ids[i]);
window.XMLHttpRequest = function() {
return new ActiveXObject(ids[i]);
};
break;
} catch (e) {}
@yocontra
yocontra / EventEmitter.coffee
Created May 20, 2012 19:57
Tiny browser/node EventEmitter implementation in coffeescript
class EventEmitter
constructor: ->
@events = {}
emit: (event, args...) ->
return false unless @events[event]
listener args... for listener in @events[event]
return true
addListener: (event, listener) ->
@yocontra
yocontra / slitscan.pde
Created May 27, 2012 22:54
Processing slitscan
import processing.video.*;
MovieMaker movie;
boolean rec;
int n;
int varZero;
int prevN;
int prevZero;
int numPixels;
int prevPixels;
require 'should'
http = require 'http'
Vein = require 'vein'
port = Math.floor(Math.random() * 1000) + 8000
server = new Vein http.createServer().listen port
describe 'first test', (done)->
it 'should do stuff', (done)->
server.drop()
server.stack = []
@yocontra
yocontra / detectanimation.coffee
Created June 28, 2012 20:17 — forked from lakenen/detectanimation.js
JavaScript animated GIF detection!
isAnimatedGif = (src, cb) ->
request = new XMLHttpRequest()
request.open "GET", src, true
request.responseType = "arraybuffer"
request.addEventListener "load", ->
arr = new Uint8Array request.response
return cb false if arr[0..3] isnt [0x47,0x49,0x46,0x38]
request.send()
@yocontra
yocontra / ex.coffee
Created July 14, 2012 06:49
example code
server = new Pulsar port: 4001
schan = channel 'notify:operators'
schan.on 'newClient', ->
schan.emit 'updateUnanswered', 5
client = new Pulsar.Client port: 4001
cchan = client.channel 'notify:operators'
cchan.on 'updateUnanswered', (num) -> console.log 'unanswered:', num
@yocontra
yocontra / dstm.coffee
Created August 7, 2012 02:39
DSTM via warlock demo
###
Network workflow:
Client connects
Client creates transaction
Server sends 'sync' with root object
Client runs transaction
if .retry() called
Client listens for 'sync' message then runs transaction again
else