Skip to content

Instantly share code, notes, and snippets.

@cburgdorf
cburgdorf / gist:1071759
Created July 8, 2011 12:57
WrapAs/AppendAs RxJS Extensions
Rx.Observable.prototype.WrapAs = function (propertyName) {
return this.Select(function (x) {
var temp = {};
temp[propertyName] = x;
return temp;
});
};
Rx.Observable.prototype.AppendAs = function (propertyName, data) {
return this.Select(function (x) {
@cburgdorf
cburgdorf / gist:1170494
Created August 25, 2011 11:49
cachedObservable
//This doesnt work (Fails on second try)
var cachedObservable;
var observableFms = Rx.Observable.If(function() {
return cachedObservable !== undefined;
},
cachedObservable,
Rx.Observable.Defer(function() {
cachedObservable = service.getObservableFmsAuswertungForTour(data.ID);
return cachedObservable;
@cburgdorf
cburgdorf / gist:1175750
Created August 27, 2011 19:03
Rx unminified
// Copyright (c) Microsoft Corporation. All rights reserved.
// This code is licensed by Microsoft Corporation under the terms
// of the MICROSOFT REACTIVE EXTENSIONS FOR JAVASCRIPT AND .NET LIBRARIES License.
// See http://go.microsoft.com/fwlink/?LinkId=186234.
(function () {
var a;
var b;
var c = this;
var d = "Index out of range";
if (typeof ProvideCustomRxRootObject == "undefined") b = c.Rx = {};
@cburgdorf
cburgdorf / gist:1355881
Created November 10, 2011 19:29
Why is this just printing 1?
var scheduler = new TestScheduler();
var source = scheduler.CreateHotObservable(
new Recorded<Notification<int>>(600, Notification.CreateOnNext(1)),
new Recorded<Notification<int>>(1200, Notification.CreateOnNext(2)),
new Recorded<Notification<int>>(1600, Notification.CreateOnNext(3)),
new Recorded<Notification<int>>(2000, Notification.CreateOnNext(4)),
new Recorded<Notification<int>>(2400, Notification.CreateOnNext(5)),
new Recorded<Notification<int>>(3000, Notification.CreateOnNext(6)),
new Recorded<Notification<int>>(3500, Notification.CreateOnNext(7))
@cburgdorf
cburgdorf / gist:1379283
Created November 19, 2011 19:59
Functional recursion and tail recursion
using System;
namespace Factorial
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("factorial of 3 using simple recursion: " + Factorial(3));
Console.WriteLine("factorial of 3 using accumulator passing style: " + FactorialAcc(3));
@cburgdorf
cburgdorf / gist:1485758
Created December 16, 2011 11:42
How to listen for STRG + UP / STRG + DOWN the Rx way
var keys = {
UP: 38,
DOWN: 40,
CTRL: 17
}
var selectKeyCode = function(event){ return event.keyCode; };
var selectDown = function(){ return "down";};
@cburgdorf
cburgdorf / gist:1961910
Created March 2, 2012 22:31
generic brain melt
public static class SignalRRxExtensions
{
public static void PublishOnClient<THub, T>(this IObservable<T> observable, Expression<Func<THub, dynamic>> expression) where THub : Hub, new ()
{
var connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<THub>();
var subject = new Rx.Subject();
var getObservable = function (eventName) {
return subject.asObservable()
.where(function (x) { return x.EventName === eventName; })
.takeUntil(function (x) { return x.Type === "onComplete"; })
.selectMany(function (x) {
return x.Type === "onNext" ? Rx.Observable.returnValue(x.Data) :
x.Type === "onError" ? Rx.Observable.throwException(x.Data) :
Rx.Observable.empty();
@cburgdorf
cburgdorf / gist:2023362
Created March 12, 2012 17:02
Bring back the forkJoin
Rx.Observable.forkJoin = function (sources) {
var tempSources = arguments.length > 1 ? arguments : sources;
return Rx.Observable
.fromArray(tempSources)
.selectMany(function (o, i) {
return o.takeLast(1).select(function (value) { return { i: i, value: value }; });
})
.aggregate({ array: [], count: 0 }, function (results, result) {
Rx.Observable.returnValue(3).merge(Rx.Observable.returnValue(2)).subscribe(function(x){console.log(x);});