Skip to content

Instantly share code, notes, and snippets.

@damianh
Last active August 29, 2015 14:23
Show Gist options
  • Save damianh/405627516c71b64f6522 to your computer and use it in GitHub Desktop.
Save damianh/405627516c71b64f6522 to your computer and use it in GitHub Desktop.
namespace ClassLibrary2
{
using System;
using System.Reactive;
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
using MidFunc = System.Func<
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>,
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>>;
public static class RebuildablePipeline
{
public static MidFunc Create(IObservable<Unit> onRebuild, Func<AppFunc> createPipeline)
{
return next =>
{
var pipeline = createPipeline(); //Need a pipeline to start off
onRebuild.Subscribe(_ => pipeline = createPipeline()); // Rebuild when signaled.
return env => pipeline(env);
};
}
}
}
namespace Tests
{
using System;
using System.Collections.Generic;
using System.Reactive;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using ClassLibrary2;
using Xunit;
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
public class Class1
{
[Fact]
public async Task Blah()
{
var subject = new Subject<Unit>();
int i = 0;
Func<AppFunc> createPipeline = () =>
{
i++;
return env =>
{
env["counter"] = i;
return Task.FromResult(0);
};
};
var rootAppFunc = RebuildablePipeline.Create(subject, createPipeline)(_ => Task.FromResult(0));
var env1 = new Dictionary<string, object>();
for (int k = 1; k < 100; k++)
{
await rootAppFunc(env1);
Assert.Equal(k, env1["counter"]);
subject.OnNext(Unit.Default);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment