Skip to content

Commit

Permalink
Merge pull request #906 from AvaloniaUI/issue/905-event-getobservable
Browse files Browse the repository at this point in the history
Added GetObservable for routed events.
  • Loading branch information
grokys committed Mar 4, 2017
2 parents 6ad65bd + 5b6e74c commit b953d3f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Avalonia.Interactivity/InteractiveExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;

namespace Avalonia.Interactivity
{
Expand All @@ -11,12 +13,36 @@ namespace Avalonia.Interactivity
/// </summary>
public static class InteractiveExtensions
{
/// <summary>
/// Gets an observable for a <see cref="RoutedEvent{TEventArgs}"/>.
/// </summary>
/// <param name="o">The object to listen for events on.</param>
/// <param name="routedEvent">The routed event.</param>
/// <param name="routes">The routing strategies to listen to.</param>
/// <param name="handledEventsToo">Whether handled events should also be listened for.</param>
/// <returns>
/// An observable which fires each time the event is raised.
/// </returns>
public static IObservable<TEventArgs> GetObservable<TEventArgs>(
this IInteractive o,
RoutedEvent<TEventArgs> routedEvent,
RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble,
bool handledEventsToo = false)
where TEventArgs : RoutedEventArgs
{
return Observable.Create<TEventArgs>(x => o.AddHandler(
routedEvent,
(_, e) => x.OnNext(e),
routes,
handledEventsToo));
}

/// <summary>
/// Gets the route for bubbling events from the specified interactive.
/// </summary>
/// <param name="interactive">The interactive.</param>
/// <returns>The event route.</returns>
public static IEnumerable<IInteractive> GetBubbleEventRoute(this IInteractive interactive)
internal static IEnumerable<IInteractive> GetBubbleEventRoute(this IInteractive interactive)
{
while (interactive != null)
{
Expand All @@ -30,7 +56,7 @@ public static IEnumerable<IInteractive> GetBubbleEventRoute(this IInteractive in
/// </summary>
/// <param name="interactive">The interactive.</param>
/// <returns>The event route.</returns>
public static IEnumerable<IInteractive> GetTunnelEventRoute(this IInteractive interactive)
internal static IEnumerable<IInteractive> GetTunnelEventRoute(this IInteractive interactive)
{
return interactive.GetBubbleEventRoute().Reverse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,32 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Reactive.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Reactive.Core.3.0.0\lib\net45\System.Reactive.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Reactive.Interfaces, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Reactive.Interfaces.3.0.0\lib\net45\System.Reactive.Interfaces.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Reactive.Linq, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Reactive.Linq.3.0.0\lib\net45\System.Reactive.Linq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Reactive.PlatformServices, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Reactive.PlatformServices.3.0.0\lib\net45\System.Reactive.PlatformServices.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Reactive.Windows.Threading, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Reactive.Windows.Threading.3.0.0\lib\net45\System.Reactive.Windows.Threading.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c">
<HintPath>..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
</Reference>
Expand Down
18 changes: 18 additions & 0 deletions tests/Avalonia.Interactivity.UnitTests/InteractiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,24 @@ public void Typed_Class_Handlers_Should_Be_Called()
Assert.True(target.GetVisualParent<TestInteractive>().ClassHandlerInvoked);
}

[Fact]
public void GetObservable_Should_Listen_To_Event()
{
var ev = new RoutedEvent<RoutedEventArgs>("test", RoutingStrategies.Direct, typeof(TestInteractive));
var target = new TestInteractive();
var called = 0;
var subscription = target.GetObservable(ev).Subscribe(_ => ++called);

var args = new RoutedEventArgs(ev, target);
target.RaiseEvent(args);

subscription.Dispose();

target.RaiseEvent(args);

Assert.Equal(1, called);
}

private TestInteractive CreateTree(
RoutedEvent ev,
EventHandler<RoutedEventArgs> handler,
Expand Down
6 changes: 6 additions & 0 deletions tests/Avalonia.Interactivity.UnitTests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Reactive" version="3.0.0" targetFramework="net45" />
<package id="System.Reactive.Core" version="3.0.0" targetFramework="net45" />
<package id="System.Reactive.Interfaces" version="3.0.0" targetFramework="net45" />
<package id="System.Reactive.Linq" version="3.0.0" targetFramework="net45" />
<package id="System.Reactive.PlatformServices" version="3.0.0" targetFramework="net45" />
<package id="System.Reactive.Windows.Threading" version="3.0.0" targetFramework="net45" />
<package id="xunit" version="2.1.0" targetFramework="net45" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.1.0" targetFramework="net45" />
Expand Down

0 comments on commit b953d3f

Please sign in to comment.