Skip to content

Instantly share code, notes, and snippets.

@TimBeyer
TimBeyer / broken_app.js
Last active December 21, 2015 01:49 — forked from dazld/broken_app.js
// In this example we'd like to have a factory that works per request and allows to keep track of created Models
// The singleton will however apply to all requests and thus keep growing
// For that reason we supply it with a reset function.
// This works fine until between receiving a request and sending a response there is no asynchronous code execution
// Once multiple requests come in in very short intervals, the asynchronous execution will lead to requests cross-polluting
// the singleton class
// factory.js
//
// Should be per-request so we can keep track

Working but complicated

In this example we'd like to have a factory that works per request and allows to keep track of created Models We create a factory per request and pass it around to the parts of the code that need access This can quickly lead to hard to understand, messy code

Working using domains

In this example we'd like to have a factory that works per request and allows to keep track of created Models We create a factory per request and create a domain to which we attach the factory That way the singleton is automatically shared with all parts of the application under process.domain.*

var items = [{'price': 123, 'tax': 0.1}, {'price': 234, 'tax': 0.3}];
var taxCalculator = function (items) {
var taxedMapper = function (item) {
return item.price + item.price * item.tax;
};
var nonTaxedMapper = function (item) {
return item.price;
var uuid = require('node-uuid');
var _ = require('lodash');
var privates = {};
function MyClass () {
this.id = uuid.v4();
privates[this.id].foobar = 'somethingprivate';
};
@TimBeyer
TimBeyer / index.js
Created September 10, 2013 10:06 — forked from dazld/index.js
'use strict';
var assert = require('assert'),
director = require('director'),
EventEmitter = require('events').EventEmitter,
ns,// = require('continuation-local-storage').createNamespace('testing'),
express = require('express'),
app = express(),
domain = require('domain');
//\/////
//\ overLIB 4.22 - You may not remove or change this notice.
//\ Copyright Erik Bosrup 1998-2004. All rights reserved.
//\
//\ Contributors are listed on the homepage.
//\ This file might be old, always check for the latest version at:
//\ http://www.bosrup.com/web/overlib/
//\
//\ Please read the license agreement (available through the link above)
//\ before using overLIB. Direct any licensing questions to erik@bosrup.com.
@TimBeyer
TimBeyer / index.js
Created April 21, 2014 08:32
requirebin sketch
var async = require('async');
var sampleData = [{'name':'name1', 'age':'age1'},{'name':'name2', 'age':'age2'},{'name':'name3', 'age':'age3'}];
async.mapSeries(sampleData, function (data,callback) {
return callback(null, data['name']);
}, function(err, results) {
console.log('results : ', results);
});
// Getting data by name
async.reduce(sampleData, {}, function (byName, data, callback) {
@TimBeyer
TimBeyer / index.js
Created April 21, 2014 11:08
requirebin sketch
// This simply calls the callback with some data after a second
// Could be an AJAX call for example
var doSomethingAsync = function (callback) {
setTimeout(function () {
callback({ some: 'data' });
}, 1000);
};
var fnThatMakesAsyncCall = function () {
// From the outside there is no way to change this callback
@TimBeyer
TimBeyer / index.js
Last active August 29, 2015 14:06
requirebin sketch
var _ = require('lodash');
var Promise = require('bluebird');
var fakeRequest = function (url) {
return new Promise (function (resolve, reject) {
console.log(Date.now(), 'Request to', url);
setTimeout(function () {
console.log(Date.now(), 'Response from', url);
resolve(url)
}, Math.random() * 5000);