Skip to content

Commit

Permalink
Solved 2 bugs
Browse files Browse the repository at this point in the history
1. Context Delegate Subscribe failed when running from thread pool thread. Created a static exec method that returns the current TaskScheuler becuase the SynchronizationContext was null
2. RegisterService TFrom, TTO caused a bug in the init routine, it caused the service to be created twice.
  • Loading branch information
kobi2294 committed May 30, 2019
1 parent 6e9da74 commit 0334906
Show file tree
Hide file tree
Showing 23 changed files with 190 additions and 37 deletions.
10 changes: 10 additions & 0 deletions Extensions/MvvmKitAppSample/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Text;
using System.Threading.Tasks;
using MvvmKitAppSample.Services;
using Unity;
using System.Diagnostics;
using System.Threading;

namespace MvvmKitAppSample
{
Expand All @@ -16,11 +19,18 @@ protected override async Task ConfigureContainerOverride()
await base.ConfigureContainerOverride();
await Navigation.RegisterStaticRegions(typeof(GlobalNav));
RegisterService<IUiService, UiService>();
RegisterService<BackgroundService>();
RegisterService<IBgService2, BgService2>();
RegisterService<IBgService1, BgService1>();
}

protected override async Task InitializeShellOverride()
{
await base.InitializeShellOverride();

var bgs = Container.Resolve<BackgroundService>();
var uis = Container.Resolve<IUiService>();

await Navigation.RouteTo(GlobalNav.ShellRoutes.Shell);
await Navigation.RouteTo(GlobalNav.Main, GlobalNav.MainRoutes.One);
await Navigation.RouteTo(GlobalNav.Gain, GlobalNav.MainRoutes.Two);
Expand Down
34 changes: 17 additions & 17 deletions Extensions/MvvmKitAppSample/Components/Shell/ShellVm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ protected override async Task OnInitialized(object param)
var i = await _service.MyNumber.Get();
await _service.MyNumber.Set(43);

await _service.PropName.Changed.Subscribe(this, val =>
{
Debug.WriteLine("PropName was changed to " + val);
return Tasks.Empty;
});

await _service.A.Changed.Subscribe(this, val =>
{
Debug.WriteLine("service.A = " + val);
return Tasks.Empty;
});

await _service.OnMyBrithday.Subscribe(this, val =>
{
Debug.WriteLine("All is well" + val);
return Task.CompletedTask;
});
//await _service.PropName.Changed.Subscribe(this, val =>
//{
// Debug.WriteLine("PropName was changed to " + val);
// return Tasks.Empty;
//});

//await _service.A.Changed.Subscribe(this, val =>
//{
// Debug.WriteLine("service.A = " + val);
// return Tasks.Empty;
//});

//await _service.OnMyBrithday.Subscribe(this, val =>
//{
// Debug.WriteLine("All is well" + val);
// return Task.CompletedTask;
//});

await Navigation.RegisterRegion(MyRegion);
await Navigation.NavigateTo<PageOneVm>(MyRegion);
Expand Down
4 changes: 4 additions & 0 deletions Extensions/MvvmKitAppSample/MvvmKitAppSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
</Compile>
<Compile Include="Components\PageTwo\PageTwoVm.cs" />
<Compile Include="Services\BackgroundService.cs" />
<Compile Include="Services\BgService1.cs" />
<Compile Include="Services\BgService2.cs" />
<Compile Include="Services\IBgService1.cs" />
<Compile Include="Services\IBgService2.cs" />
<Compile Include="Services\IUiService.cs" />
<Compile Include="Services\UiService.cs" />
</ItemGroup>
Expand Down
12 changes: 11 additions & 1 deletion Extensions/MvvmKitAppSample/Services/BackgroundService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using MvvmKit;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Unity;

Expand All @@ -20,10 +22,13 @@ public class BackgroundService: BackgroundServiceBase


private readonly ServiceField<bool> _PropName = true;

public ServiceProperty<bool> PropName { get => (_PropName, this); }

public AsyncEvent<DateTime> OnMyBrithday { get; } = new AsyncEvent<DateTime>(DateTime.Now);

private IUiService _uiService;

public BackgroundService()
{
}
Expand All @@ -34,16 +39,21 @@ protected override async Task OnInit()
}

[InjectionMethod]
public void Inject()
public void Inject(IUiService uiService)
{
// store dependencies here
_uiService = uiService;
}

public Task Method()
{
return Run(async () =>
{
Debug.WriteLine("I am in a ba service, thread = " + Thread.CurrentThread.ManagedThreadId);

await _A.Set(_A.Value + 1);

await _uiService.Method();
}, true);
}
}
Expand Down
45 changes: 45 additions & 0 deletions Extensions/MvvmKitAppSample/Services/BgService1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using MvvmKit;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace MvvmKitAppSample.Services
{
public class BgService1 : BackgroundServiceBase, IBgService1
{
private IBgService2 _svc;

protected override async Task OnInit()
{
await base.OnInit();

// init logic here
await _svc.OnString.Subscribe(this, OnString);
}

private async Task OnString(string arg)
{
await Task.Delay(1000);
Debug.WriteLine("Hey Baby! " + arg);
}

[InjectionMethod]
public void Inject(IBgService2 svc)
{
// store dependencies here
_svc = svc;
}

public Task Method()
{
return Run(() =>
{
// your logic here
});
}
}
}
44 changes: 44 additions & 0 deletions Extensions/MvvmKitAppSample/Services/BgService2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using MvvmKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace MvvmKitAppSample.Services
{
public class BgService2 : BackgroundServiceBase, IBgService2
{
public AsyncEvent<string> OnString { get; } = new AsyncEvent<string>("First Value");

protected override async Task OnInit()
{
await base.OnInit();

// init logic here
}

public static int counter = 0;
public int id;

public BgService2()
{
id = ++counter;
}

[InjectionMethod]
public void Inject()
{
// store dependencies here
}

public Task Method()
{
return Run(() =>
{
// your logic here
});
}
}
}
9 changes: 9 additions & 0 deletions Extensions/MvvmKitAppSample/Services/IBgService1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace MvvmKitAppSample.Services
{
public interface IBgService1
{
Task Method();
}
}
12 changes: 12 additions & 0 deletions Extensions/MvvmKitAppSample/Services/IBgService2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading.Tasks;
using MvvmKit;

namespace MvvmKitAppSample.Services
{
public interface IBgService2
{
AsyncEvent<string> OnString { get; }

Task Method();
}
}
3 changes: 3 additions & 0 deletions Extensions/MvvmKitAppSample/Services/UiService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using MvvmKit;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Unity;

Expand All @@ -27,6 +29,7 @@ public Task Method()
{
return Run(() =>
{
Debug.WriteLine("I am in UI Service, Thread = " + Thread.CurrentThread.ManagedThreadId);
// your logic here
});
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

<ItemGroup>
<PackageReference Include="MvvmKit">
<Version>1.0.0.18</Version>
<Version>1.0.0.20</Version>
</PackageReference>
<PackageReference Include="Unity">
<Version>5.10.3</Version>
Expand Down
2 changes: 1 addition & 1 deletion Extensions/MvvmKitExtension/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="MvvmKit Extension.fca60f99-6a29-4936-a072-ba741fee533f" Version="2.18" Language="en-US" Publisher="Kobi Hari" />
<Identity Id="MvvmKit Extension.fca60f99-6a29-4936-a072-ba741fee533f" Version="2.20" Language="en-US" Publisher="Kobi Hari" />
<DisplayName>MvvmKit Extension</DisplayName>
<Description xml:space="preserve">An Extension for visual studio that contains templates and snippets that will help you develop an application using the MVVM Kit</Description>
<MoreInfo>https://github.com/kobi2294/MvvmKit/wiki</MoreInfo>
Expand Down
9 changes: 7 additions & 2 deletions Source/MvvmKit/Mvvm/Core/BootstrapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Unity;
Expand Down Expand Up @@ -96,13 +97,17 @@ public async void Run()

// init services
var services = _servicesToInit.Select(type => Container.Resolve(type) as ServiceBase)
.Select(service => service.Init());
.Select(service => service.Init())
.ToList();

var ctxt = SynchronizationContext.Current;

await OnServicesInitializing();

await InitializeShellOverride();

await Task.WhenAll(services);

await OnServicesInitialized();
}

Expand All @@ -129,7 +134,7 @@ protected void RegisterService<InterfaceType, ServiceType>(bool initService = tr
Container.RegisterType<InterfaceType, ServiceType>(new ContainerControlledLifetimeManager());
if (initService)
{
_servicesToInit.Add(typeof(ServiceType));
_servicesToInit.Add(typeof(InterfaceType));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/MvvmKit/Mvvm/Core/DelegateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DelegateCommand(Action execute)
/// </summary>
/// <param name="execute">The <see cref="Action"/> to invoke when <see cref="ICommand.Execute"/> is called.</param>
/// <param name="canExecute">The <see cref="Func{TResult}"/> to invoke when <see cref="ICommand.CanExecute"/> is called</param>
DelegateCommand(Action execute, Func<bool> canExecute)
public DelegateCommand(Action execute, Func<bool> canExecute)
: base(execute, canExecute)
{
}
Expand Down
4 changes: 2 additions & 2 deletions Source/MvvmKit/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.18")]
[assembly: AssemblyFileVersion("1.0.0.18")]
[assembly: AssemblyVersion("1.0.0.20")]
[assembly: AssemblyFileVersion("1.0.0.20")]
[assembly: NeutralResourcesLanguage("en")]

5 changes: 5 additions & 0 deletions Source/MvvmKit/Tools/Async/ContextDelegates/AsyncEventT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public Task Subscribe(object owner, Func<T, Task> callback)
return callback(LatestValue);
}

public Task Subscribe(object onString)
{
throw new NotImplementedException();
}

public Task Unsubscribe(object owner, Func<T, Task> callback = null)
{
if (callback == null)
Expand Down
4 changes: 2 additions & 2 deletions Source/MvvmKit/Tools/Async/ContextDelegates/ContextAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public ContextAction(TaskScheduler scheduler, object owner, Action action)
: this(scheduler.ToContextRunner(), action.ToWeak(owner)) { }

public ContextAction(WeakAction wa)
: this(TaskScheduler.FromCurrentSynchronizationContext(), wa) { }
: this(Exec.RunningTaskScheduler, wa) { }

public ContextAction(object owner, Action a)
: this(TaskScheduler.FromCurrentSynchronizationContext(), a.ToWeak(owner)) { }
: this(Exec.RunningTaskScheduler, a.ToWeak(owner)) { }

public Task Invoke()
{
Expand Down
4 changes: 2 additions & 2 deletions Source/MvvmKit/Tools/Async/ContextDelegates/ContextActionT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public ContextAction(TaskScheduler scheduler, object owner, Action<T> action)
: this(scheduler.ToContextRunner(), action.ToWeak(owner)) { }

public ContextAction(WeakAction<T> wa)
: this(TaskScheduler.FromCurrentSynchronizationContext(), wa) { }
: this(Exec.RunningTaskScheduler, wa) { }

public ContextAction(object owner, Action<T> a)
: this(TaskScheduler.FromCurrentSynchronizationContext(), a.ToWeak(owner)) { }
: this(Exec.RunningTaskScheduler, a.ToWeak(owner)) { }


public Task Invoke(T arg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public ContextAction(TaskScheduler scheduler, object owner, Action<T1, T2> actio
: this(scheduler.ToContextRunner(), action.ToWeak(owner)) { }

public ContextAction(WeakAction<T1, T2> wa)
: this(TaskScheduler.FromCurrentSynchronizationContext(), wa) { }
: this(Exec.RunningTaskScheduler, wa) { }

public ContextAction(object owner, Action<T1, T2> a)
: this(TaskScheduler.FromCurrentSynchronizationContext(), a.ToWeak(owner)) { }
: this(Exec.RunningTaskScheduler, a.ToWeak(owner)) { }

public Task Invoke(T1 arg1, T2 arg2)
{
Expand Down
Loading

0 comments on commit 0334906

Please sign in to comment.