Skip to content

Commit

Permalink
Ensure single initialization of build dependencies
Browse files Browse the repository at this point in the history
Fixes #851
  • Loading branch information
glopesdev committed Apr 20, 2022
1 parent 7d3b8f7 commit 7eab53e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions Bonsai.Core/Expressions/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -938,12 +938,6 @@ internal static Expression HandleObservableCreationException(Expression expressi

#region Dynamic Properties

static readonly MethodInfo deferMethod = typeof(Observable).GetMethods()
.Single(m => m.Name == "Defer" &&
m.GetParameters()[0].ParameterType
.GetGenericArguments()[0]
.GetGenericTypeDefinition() == typeof(IObservable<>));

internal static Tuple<Expression, string> BuildArgumentAccess(IEnumerable<Expression> arguments, string selector)
{
if (string.IsNullOrEmpty(selector))
Expand Down Expand Up @@ -1107,11 +1101,17 @@ static IObservable<TResult> BuildDependency<TSource, TResult>(IObservable<TSourc
return source.IgnoreElements().Select(xs => default(TResult));
}

static IObservable<TResult> Lazy<TResult>(Func<IObservable<TResult>> observableFactory)
{
var source = new Lazy<IObservable<TResult>>(observableFactory);
return Observable.Defer(() => source.Value);
}

internal static Expression MergeBuildDependencies(Expression output, IEnumerable<Expression> buildDependencies)
{
var observableFactory = Expression.Lambda(output);
var outputType = output.Type.GetGenericArguments()[0];
var source = Expression.Call(deferMethod.MakeGenericMethod(outputType), observableFactory);
var source = Expression.Call(typeof(ExpressionBuilder), nameof(Lazy), new[] { outputType }, observableFactory);
buildDependencies = buildDependencies.Select(dependency => BuildDependency(dependency, output));
var mappingArray = Expression.NewArrayInit(output.Type, buildDependencies);
return Expression.Call(
Expand Down

0 comments on commit 7eab53e

Please sign in to comment.