Skip to content

Instantly share code, notes, and snippets.

View ifandelse's full-sized avatar

Jim Cowart ifandelse

View GitHub Profile
@ifandelse
ifandelse / amqpBindingResolver.js
Created May 20, 2012 19:50
AMQP Style Bindings for postal.js
var amqpBindingsResolver = {
cache : { },
compare : function ( binding, topic ) {
if ( this.cache[topic] && this.cache[topic][binding] ) {
return true;
}
// binding.replace(/\./g,"\\.") // escape actual periods
// .replace(/#/g, ".*") // hashes match any value
// .replace(/\*/g, "[A-Z,a-z,0-9]*"); // asterisks match any alpha-numeric 'word'
@ifandelse
ifandelse / appInitFsm.js
Created April 17, 2012 02:00
Hierarchical State Machine API ideas for machina.js
// FSM to handle app init concerns - 90% of the time it would have linear progression through states
var appInitFsm = new machina.Fsm({
initialState: "uninitialized";
states: {
uninitialized: {
_onEnter: function() {
this.transition("initializing");
}
},
@ifandelse
ifandelse / groupsOf.js
Created April 5, 2012 15:54
Underscore mixin that takes a list and splits it into multiple lists, where each list is n elements long
_.mixin({
groupsOf: function(list, num) {
if (!_.isArray(list)) {
list = _.toArray(list);
}
var result = [];
while (list.length) {
result.push(list.splice(0, num));
}
return result;
@ifandelse
ifandelse / DistributedTask.js
Created March 29, 2012 20:20
Machina-based linear/fork-join FSM Task Idea
define([
'underscore',
'machina'
], function( _, machina ){
return function(config) {
var base = {
initialState: "notStarted",
checkIfReady: function() {
if(_.all(this.constraints[this.state].checkList, function(constraint) { return constraint; })) {
this.transition(this.constraints[this.state].nextState);
@ifandelse
ifandelse / postal.ideas.js
Created March 21, 2012 20:20
Postal API Ideas
// Below are FOUR possible examples of publish method signatures for the upcoming v0.6.0 release.
// Each one is using a simple "data" object (containing Alex's first and last name, since we were chatting as I wrote the gist)
// It's also worth bearing in mind that this is the top level API publish (where you don't have a handle to a channel already)
// you'll still be able to say var channel = postal.channel({ channel: "someChannel", topic: "someTopic" }), and then call publish off that later
// 1.) Single Payload, envelope is special member
/*
PROS:
one object to publish (data/env)
focus is on "DATA" being published - nice for consuming dev?
@ifandelse
ifandelse / amplify.js
Created March 20, 2012 16:34
amplify.js in an AMD Wrapper
/*!
* AmplifyJS 1.0.0 - Core, Store, Request
*
* Copyright 2011 appendTo LLC. (http://appendto.com/team)
* Dual licensed under the MIT or GPL licenses.
* http://appendto.com/open-source-licenses
*
* http://amplifyjs.com
*/
(function(root, doc, factory) {
@ifandelse
ifandelse / ConnectivityFsm.js
Created March 14, 2012 05:30
FSM to manage offline/online
var ConnectivityFsm = ( function( $, machina, amplify ) {
return function( heartbeatDef ) {
var settings = $.extend( true, {
type: "GET",
dataType: "json",
timeout: 5000
}, heartbeatDef );
@ifandelse
ifandelse / PrfTst.js
Created February 27, 2012 04:35
PrfTst
/*
postal.js
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.6.0
*/
(function(root, doc, factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
@ifandelse
ifandelse / deepExtend.js
Created February 20, 2012 18:47
Custom Deep Extend Mixin for Underscore
var slice = [].slice;
if (!_.deepExtend) {
var behavior = {
"*": function(obj, sourcePropKey, sourcePropVal) {
obj[sourcePropKey] = sourcePropVal;
},
"object": function(obj, sourcePropKey, sourcePropVal) {
obj[sourcePropKey] = deepExtend(obj[sourcePropKey] || {}, sourcePropVal);
},
"array": function(obj, sourcePropKey, sourcePropVal) {
@ifandelse
ifandelse / cartModule.js
Created January 16, 2012 03:59
Client-side Messaging in JavaScript - Part 2 (Postal.js) Example 2
var cartModule = (function(postal, $){
var cartId = 0,
cartTemplate = "#cart-tmpl",
cartItemTemplate = "#cart-item-tmpl",
cartChildrenSelector = "#cart-list",
wireUpCart = function(cart) {
postal.subscribe("cart", "init", _.bind(cart.init,cart));
postal.subscribe("cart", "item.add", function(item) {
var member = cart.id + "-" + item.id;