Sunday 29 June 2014

DetectStale within a stream

The DetectStale<T> extension method within the Reactive Trader RX example application detects when a stream has stopped producing values and injects an IsStale indicator to allow the UI to react.

The idea is that while the stream is steadily providing values all is good.  As soon as a value hasn’t been received within a set period of time, a new value is sent down the stream to say “this steam has gone stale”.

First here is an example of it being used within a test:

image
The DetectStale internally uses a timer to know whether it should be sending in effect an null/empty stale value. Here are another couple of tests for the DetectStale extension method:

Here is the DetectStale code itself: You can the source subscription on line 42 calling the normal/expected OnNext method with the value.  After that they call the scheduleStale method containing the timer with Stale / null value.

Tuesday 24 June 2014

ThreadPool

A friend of mine a few years back was asked in an interview how many threads are in the .NET threadpool by default.

He asked me after the interview and I had no idea.  So we did some research and it turns out not many people on the internet knew either. 

Turns out it depends the version of the .NET Framework, how many cores, 32/64 bit.

Here’s some code to find out on your machine and .NET framework setup:

class Program
{
static void Main(string[] args)
{
int workerThreads, completionPortThreads;

ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
Console.WriteLine("Min worker threads {0}, completionPortThreads {1}", workerThreads, completionPortThreads);

ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
Console.WriteLine("Max worker threads {0}, completionPortThreads {1}", workerThreads, completionPortThreads);

ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
Console.WriteLine("Available worker threads {0}, completionPortThreads {1}", workerThreads, completionPortThreads);

Console.ReadKey();
}
}

image

Monday 23 June 2014

From Task to Rx to Streaming data

Here is a simple example of a ViewModel calling a LoadCustomers service:

public async Task LoadCustomers()
{
IsLoading = true;
Customers.Clear();
var customers = await services.GetAllCustomers();
Customers.AddRange(customers);
IsLoading = false;
}
Note: I’m using the PRISM extension method that allows for adding a range to an observable collection, giving less notifications.  The error handling has been removed to focus on the code.

Here’s the equivalent for Rx:
public async Task LoadCustomers()
{
IsLoading = true;
Customers.Clear();
services.GetAllCustomers().ToObservable()
.SubscribeOn(NewThreadScheduler.Default)
.ObserveOnDispatcher()
.Subscribe(
customers => Customers.AddRange(customers),
() => IsLoading = false);
}

The SubscribeOn extension method is ensuring the GetAllCustomers call is being made on a separate thread to keep the UI as free as possible.  Once the response comes back, the ObserveOnDispatcher ensures the UI thread runs our Customers.AddRange code along with the IsLoading = false code which is running on the Observable OnCompleted event.

So any benefits from this?  Not really.  But imagine our GetAllCustomers wasn’t just a call to a web endpoint.

First we’ll remove the ToObservable() extension method and code up the observable ourselves so we understand what’s actually going on under the hood:

public async Task LoadCustomers()
{
IsLoading = true;
Customers.Clear();

var source = Observable.Create<IEnumerable<Customer>>(
async o =>
{
var response = await services.GetAllCustomers();
o.OnNext(response);
o.OnCompleted();
return Disposable.Empty;
});

source
.SubscribeOn(NewThreadScheduler.Default)
.ObserveOnDispatcher()
.Subscribe(
customers => Customers.AddRange(customers),
() => IsLoading = false);
}

Now if we start to require more work being done to retrieve data than just a web call, like perhaps converting DTOs to UI models, this can be done within the observable code.  Remember this work is not being done on the UI thread so our app is nice and responsive.

Now imagine our services returned observables rather than DTOs or UI models:

public void LoadCustomers()
{
services.GetAllCustomers()
.SubscribeOn(NewThreadScheduler.Default)
.ObserveOnDispatcher()
.Subscribe(customers => Customers.AddRange(customers));
}

We now have a viewmodel that is dealing with observables and doesn’t care whether that data is coming from a web service call, file I/O or a Nirvana (Universal) endpoint that is streaming data.

Thursday 19 June 2014

Faking XMLHttpRequest with SinonJS

 // uses sinon and jQuery

var server = sinon.fakeServer.create();
server.respondWith([200, { "Content-Type": "application/json" }, '{"myData":3}']);
$.getJSON('some/url');
console.log(server.response);
server.restore();
JSFiddle:  http://jsfiddle.net/stevenhollidge/crDCT/4/ 
// uses sinon and jquery

var xhr = sinon.useFakeXMLHttpRequest();
var requests = [];

xhr.onCreate = function (request) {
requests.push(request);
};

var responseData = '{"myData":3}';
$.getJSON('some/url', function(data) { console.log(data); });
requests[0].respond(200, { "Content-Type": "application/json" }, responseData);
console.log(requests[0].url);
xhr.restore();


JSFiddle:  http://jsfiddle.net/stevenhollidge/crDCT/2/


An example combined with Jasmine to mock out the http call:

// uses jasmine, sinon and jQuery

describe("useFakeXMLHttpRequest", function() {
var xhr, requests;

beforeEach(function () {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];

xhr.onCreate = function (request) {
requests.push(request);
};
},

afterEach: function () {
xhr.restore();
},

it('should be able to handle responses', function () {
var responseData = '{"myData":3}';
$.getJSON('some/url', function(data) { console.log(data); });
requests[0].respond(200, { "Content-Type": "application/json" }, responseData);
expect(requests[0].url).toBe('some/url');
}
}

JSFiddle:  http://jsfiddle.net/stevenhollidge/crDCT/

JavaScript revocable function


function revocable(f) {
return {
invoke: function () {
return f.apply(this, arguments);
},
revoke : function () {
f = null;
}
};
}

var temp = revocable(console.log);
temp.invoke('1'); // logs
temp.revoke();
temp.invoke('2'); // throws

// Works in FF, apparently Chrome has a bug... hmmm better ways to do this i think
// use with timeout to allow access
// to a fuction for a set period of time

JS Fiddle:  http://jsfiddle.net/stevenhollidge/tKC5Q/

Simple JavaScript counter

function counterf(value) {
return {
inc: function () {
return value += 1;
},
dec: function () {
return value -= 1;
}
};
}

var counter = counterf(10);
counter.inc() // 11
counter.dec() // 10

JSFiddle: http://jsfiddle.net/stevenhollidge/f9sKV/

JavaScript function that can be called only once

function add(a, b) {
return a+b;
}

function once(func) {
return function () {
var f = func;
func = null;
return f.apply(
this,
arguments
);
};
}

var add_once = once(add);
console.log(add_once(3, 4)); // 7
console.log(add_once(3, 4)); // throws as f is now null

JSFiddle: http://jsfiddle.net/stevenhollidge/uA6TH/


But I prefer the following code as it’s more readable:

function add(a, b) {
return a+b;
}

function once(func) {
var usageCount = 0;

return function () {
if (usageCount === 1) {
throw 'Already used';
}
usageCount += 1;
return func.apply(
this,
arguments
);

}
}

var add_once = once(add);
console.log(add_once(3, 4)); // 7
console.log(add_once(3, 4)); // throws

JSFiddle: http://jsfiddle.net/stevenhollidge/eqnU3/

Wednesday 18 June 2014

JavaScript Functional Inheritance

Taken from Douglas Crockford:

function gizmo(id) {
return {
id: id,
toString: function () {
return "gizmo " + id;
}
};
}

function hoozit(id) {
var that = gizmo(id);
this.test = function (testid) {
return testid === id;
};
return that;
}

JS Fiddle: http://jsfiddle.net/stevenhollidge/FGbX6/

JavaScript Closures

Slow: digit_name creates the array each time it's invoked

var digit_name = function (n) {
var names = ['zero', 'one', 'two', 'three'];

return names[n];
};

console.log(digit_name(2)); // two

An example of a closure


Fast: digit_name immediately invokes a function and holds the result, a pointer to the array. This is a pointer to a variable that lives on outside of the scope of the function it was created in.


names is closed over the outer function.

var digit_name = (function () {
var names = ['zero', 'one', 'two', 'three'];

return function (n) {
return names[n];
};
}());

console.log(digit_name(2)); // two
JS Fiddle:  http://jsfiddle.net/stevenhollidge/22Ab7/

Tuesday 17 June 2014

Simple JavaScript Interceptor

The function on line 14 amends an object method to intercept method calls to fire pre and post functions. The object can then continue to be used as normal.


var Calculator = function () {}
Calculator.prototype.Add = function (a, b) {
console.log("Adding a+b");
return a+b;
}

var SuperCalculator = function () {}
SuperCalculator.prototype = new Calculator();
SuperCalculator.prototype.Delete = function(a, b) {
console.log("Deleting a-b");
return a-b;
}

function createInterceptor (key, context, originalFunc, preFunc) {
var interceptor = function () {
preFunc();
var result = originalFunc.apply(context, arguments);
postFunc();
return result;
}
return interceptor;
}

var mySuperCalculator = new SuperCalculator();
var preFunc = function () { console.log("Pre func fired"); }
var postFunc = function () { console.log("Post func fired"); }

for(var key in mySuperCalculator) {
mySuperCalculator[key] = createInterceptor(key, mySuperCalculator, mySuperCalculator[key], preFunc, postFunc);
}

var addResult = mySuperCalculator.Add(1, 2);
console.log("AddResult: " + addResult);

var deleteResult = mySuperCalculator.Delete(2,1);
console.log("DeleteResult: " + deleteResult);

image


JSFiddle: http://jsfiddle.net/stevenhollidge/vb893/

Saturday 14 June 2014

Setting default values for arguments in JavaScript

function complex (settings){
settings.option1 = (typeof settings.option1 === "undefined") ? "default option 1" : settings.option1;
settings.option2 = (typeof settings.option2 === "undefined") ? "default option 2" : settings.option2;
settings.option3 = (typeof settings.option3 === "undefined") ? "default option 3" : settings.option3;
settings.option4 = (typeof settings.option4 === "undefined") ? "default option 4" : settings.option4;

console.log(settings.option1);
console.log(settings.option2);
console.log(settings.option3);
console.log(settings.option4);
};
complex({option1: 'this is option 1', option3: 'this is option 3'});
image

JS Fiddle:  http://jsfiddle.net/stevenhollidge/w9z2g/

Thursday 12 June 2014

Export JSON from SQL Server


create procedure [dbo].[GetJSON] (
@schema_name varchar(50),
@table_name varchar(50),
@registries_per_request smallint = null
)
as
begin
if ( ( select count(*) from information_schema.tables where table_schema = @schema_name and table_name = @table_name ) > 0 )
begin
declare @json varchar(max),
@line varchar(max),
@columns varchar(max),
@sql nvarchar(max),
@columnNavigator varchar(50),
@counter tinyint,
@size varchar(10)

if (@registries_per_request is null)
begin
set @size = ''
end
else
begin
set @size = 'top ' + convert(varchar, @registries_per_request)
end
set @columns = '{'

declare schemaCursor cursor for
select column_name
from information_schema.columns
where table_schema = @schema_name
and table_name = @table_name
open schemaCursor

fetch next from schemaCursor into @columnNavigator

select @counter = count(*)
from information_schema.columns
where table_schema = @schema_name
and table_name = @table_name

while @@fetch_status = 0
begin
set @columns = @columns + '''''' + @columnNavigator + ''''':'''''' + convert(varchar(max), ' + @columnNavigator + ') + '''''''
set @counter = @counter - 1
if ( 0 != @counter )
begin
set @columns = @columns + ','
end

fetch next from schemaCursor into @columnNavigator
end

set @columns = @columns + '}'

close schemaCursor
deallocate schemaCursor

set @json = '['

set @sql = 'select ' + @size + '''' + @columns + ''' as json into tmpJsonTable from [' + @schema_name + '].[' + @table_name + ']'
print @sql
exec sp_sqlexec @sql

select @counter = count(*) from tmpJsonTable

declare tmpCur cursor for
select * from tmpJsonTable
open tmpCur

fetch next from tmpCur into @line

while @@fetch_status = 0
begin
set @counter = @counter - 1
set @json = @json + @line
if ( 0 != @counter )
begin
set @json = @json + ','
end

fetch next from tmpCur into @line
end

set @json = @json + ']'

close tmpCur
deallocate tmpCur
drop table tmpJsonTable

select @json as json
end
end

This will do the trick for small json payloads

Sunday 1 June 2014

Angular List

An Angular list that features bootstrap layout, reads and denormalises JSON data from a text file and has filtering.

Screenshot:

image

Online demo here

Source