Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow propagation of Defer nested sequence visualizer #1931

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Bonsai.Core.Tests/InspectBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,54 @@ public void Build_GroupInspectBuilder_ReturnNestedVisualizerElement()
Assert.AreSame(target, visualizerElement.Builder);
}

[TestMethod]
public void Build_DeferInspectBuilder_ReturnNestedVisualizerElement()
{
// related to https://github.com/bonsai-rx/bonsai/issues/1896
ExpressionBuilder target = null;
var workflow = new TestWorkflow()
.AppendUnit()
.AppendNested(
input => input
.AppendUnit()
.Capture(out target)
.AppendOutput(),
workflow => new Reactive.Defer(workflow))
.ToInspectableGraph();
workflow.Build();

var output = workflow[workflow.Count - 1].Value;
var visualizerElement = ExpressionBuilder.GetVisualizerElement(output);
Assert.AreSame(target, visualizerElement.Builder);
}

[TestMethod]
public void Build_DeferWithNestedVisualizerInspectBuilder_ReturnNestedVisualizerElement()
{
// related to https://github.com/bonsai-rx/bonsai/issues/1896
ExpressionBuilder target = null;
var workflow = new TestWorkflow()
.AppendUnit()
.AppendNested(
input => input
.AppendUnit()
.AppendSubject<Reactive.BehaviorSubject>(nameof(System.Reactive.Unit))
.AppendNested(
input => input
.AppendUnit()
.Capture(out target)
.AppendOutput(),
workflow => new Reactive.Visualizer(workflow))
.AppendOutput(),
workflow => new Reactive.Defer(workflow))
.ToInspectableGraph();
workflow.Build();

var output = workflow[workflow.Count - 1].Value;
var visualizerElement = ExpressionBuilder.GetVisualizerElement(output);
Assert.AreSame(target, visualizerElement.Builder);
}

[TestMethod]
public void Build_PropertyMappedInspectBuilderToWorkflowOutput_ReturnVisualizerElement()
{
Expand Down
7 changes: 7 additions & 0 deletions Bonsai.Core.Tests/TestWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public TestWorkflow AppendNested<TWorkflowExpressionBuilder>(
return Append(workflowBuilder);
}

public TestWorkflow AppendSubject<TSubjectBuilder>(string name)
where TSubjectBuilder : SubjectExpressionBuilder, new()
{
var subjectBuilder = new TSubjectBuilder { Name = name };
return Append(subjectBuilder);
}

public ExpressionBuilderGraph ToInspectableGraph()
{
return Workflow.ToInspectableGraph();
Expand Down
7 changes: 7 additions & 0 deletions Bonsai.Core/Expressions/InspectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ methodCall.Arguments[0] is MethodCallExpression lazy &&
{
source = methodCall.Arguments[0];
}
// If multicasting into a defer combinator, recurse on the main output
else if (methodCall.Method.DeclaringType == typeof(Reactive.Defer) &&
methodCall.Arguments.Count == 1 &&
methodCall.Arguments[0] is LambdaExpression lambda)
{
return GetInspectBuilder(lambda.Body);
}
else break;
}
else if (methodCall.Object.Type == typeof(InspectBuilder))
Expand Down
15 changes: 6 additions & 9 deletions Bonsai.Core/Reactive/Defer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Linq.Expressions;
using System.Reflection;
using System.Reactive.Linq;
using System;
using Bonsai.Expressions;
Expand All @@ -18,12 +16,6 @@ namespace Bonsai.Reactive
[Description("Creates a new observable sequence for each subscription using the encapsulated workflow.")]
public class Defer : WorkflowExpressionBuilder
{
static readonly MethodInfo deferMethod = typeof(Observable).GetMethods()
.Single(m => m.Name == "Defer" &&
m.GetParameters()[0].ParameterType
.GetGenericArguments()[0]
.GetGenericTypeDefinition() == typeof(IObservable<>));

/// <summary>
/// Initializes a new instance of the <see cref="Defer"/> class.
/// </summary>
Expand Down Expand Up @@ -59,8 +51,13 @@ public override Expression Build(IEnumerable<Expression> arguments)
{
var factory = Expression.Lambda(selectorBody);
var resultType = selectorBody.Type.GetGenericArguments()[0];
return Expression.Call(deferMethod.MakeGenericMethod(resultType), factory);
return Expression.Call(typeof(Defer), nameof(Process), new[] { resultType }, factory);
});
}

static IObservable<TResult> Process<TResult>(Func<IObservable<TResult>> factory)
{
return Observable.Defer(factory);
}
}
}