Skip to content

Commit 3d1b686

Browse files
Added BasePagedSourceProvider both Async and Sync.
Fixed Async bug where count could return 0 Fixed all Async to Sync calls so they cant hang.
1 parent 8f1b077 commit 3d1b686

20 files changed

Lines changed: 680 additions & 79 deletions

AlphaChiTech.Virtualization/ActionVirtualizationWrapper.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55

66
namespace AlphaChiTech.Virtualization
77
{
8+
/// <summary>
9+
/// This is a VirtualAction that wraps an Action, optionally with a repeating schedule.
10+
/// </summary>
811
public class ActionVirtualizationWrapper : BaseRepeatableActionVirtualization
912
{
1013
private Action _Action = null;
1114

15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ActionVirtualizationWrapper"/> class.
17+
/// </summary>
18+
/// <param name="action">The action.</param>
19+
/// <param name="threadModel">The thread model.</param>
20+
/// <param name="isRepeating">if set to <c>true</c> [is repeating].</param>
21+
/// <param name="repeatingSchedule">The repeating schedule.</param>
1222
public ActionVirtualizationWrapper(Action action,
1323
VirtualActionThreadModelEnum threadModel = VirtualActionThreadModelEnum.UseUIThread,
1424
bool isRepeating = false, TimeSpan? repeatingSchedule = null)
@@ -17,6 +27,9 @@ public ActionVirtualizationWrapper(Action action,
1727
_Action = action;
1828
}
1929

30+
/// <summary>
31+
/// Does the action.
32+
/// </summary>
2033
public override void DoAction()
2134
{
2235
var a = _Action;

AlphaChiTech.Virtualization/AlphaChiTech.Virtualization.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
<ItemGroup>
4646
<Compile Include="ActionVirtualizationWrapper.cs" />
4747
<Compile Include="BaseActionVirtualization.cs" />
48+
<Compile Include="BasePagedSourceProvider.cs" />
49+
<Compile Include="PagedSourceProviderMakeSync.cs" />
4850
<Compile Include="BaseRepeatableActionVirtualization.cs" />
4951
<Compile Include="DateBasedPageExpiryComparer.cs" />
5052
<Compile Include="ExecuteResetWA.cs" />
@@ -63,6 +65,7 @@
6365
<Compile Include="IVirtualizationAction.cs" />
6466
<Compile Include="PageDelta.cs" />
6567
<Compile Include="PagedSourceItemsPacket.cs" />
68+
<Compile Include="PagedSourceProviderMakeAsync.cs" />
6669
<Compile Include="PageFetchStateEnum.cs" />
6770
<Compile Include="PageReclaimOnTouched.cs" />
6871
<Compile Include="PaginationManager.cs" />

AlphaChiTech.Virtualization/BaseActionVirtualization.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@
55

66
namespace AlphaChiTech.Virtualization
77
{
8+
/// <summary>
9+
/// Base Class that does an action on the dispatcher thread. Simply implement the DoAction method.
10+
/// </summary>
811
public abstract class BaseActionVirtualization : IVirtualizationAction
912
{
13+
/// <summary>
14+
/// Gets or sets the thread model.
15+
/// </summary>
16+
/// <value>
17+
/// The thread model.
18+
/// </value>
1019
public VirtualActionThreadModelEnum ThreadModel { get; set; }
1120

21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="BaseActionVirtualization"/> class.
23+
/// </summary>
24+
/// <param name="threadModel">The thread model.</param>
1225
public BaseActionVirtualization(VirtualActionThreadModelEnum threadModel)
1326
{
1427
this.ThreadModel = threadModel;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AlphaChiTech.Virtualization
8+
{
9+
public class BasePagedSourceProvider<T> : IPagedSourceProvider<T>
10+
{
11+
public BasePagedSourceProvider()
12+
{
13+
}
14+
15+
public BasePagedSourceProvider(
16+
Func<int, int, PagedSourceItemsPacket<T>> funcGetItemsAt = null,
17+
Func<int> funcGetCount = null,
18+
Func<T, int> funcIndexOf = null,
19+
Action<int> actionOnReset = null
20+
)
21+
{
22+
this.FuncGetItemsAt = funcGetItemsAt;
23+
this.FuncGetCount = funcGetCount;
24+
this.FuncIndexOf = funcIndexOf;
25+
this.ActionOnReset = actionOnReset;
26+
}
27+
28+
private Func<int, int, PagedSourceItemsPacket<T>> _FuncGetItemsAt = null;
29+
30+
public Func<int, int, PagedSourceItemsPacket<T>> FuncGetItemsAt
31+
{
32+
get { return _FuncGetItemsAt; }
33+
set { _FuncGetItemsAt = value; }
34+
}
35+
private Func<int> _FuncGetCount = null;
36+
37+
public Func<int> FuncGetCount
38+
{
39+
get { return _FuncGetCount; }
40+
set { _FuncGetCount = value; }
41+
}
42+
private Func<T, int> _FuncIndexOf = null;
43+
44+
public Func<T, int> FuncIndexOf
45+
{
46+
get { return _FuncIndexOf; }
47+
set { _FuncIndexOf = value; }
48+
}
49+
private Action<int> _ActionOnReset = null;
50+
51+
public Action<int> ActionOnReset
52+
{
53+
get { return _ActionOnReset; }
54+
set { _ActionOnReset = value; }
55+
}
56+
57+
public virtual PagedSourceItemsPacket<T> GetItemsAt(int pageoffset, int count, bool usePlaceholder)
58+
{
59+
if (_FuncGetItemsAt != null) return _FuncGetItemsAt.Invoke(pageoffset, count);
60+
61+
return null;
62+
}
63+
64+
public virtual int Count
65+
{
66+
get
67+
{
68+
int ret = 0;
69+
70+
if (_FuncGetCount != null) ret = _FuncGetCount.Invoke();
71+
72+
return ret;
73+
}
74+
}
75+
76+
public virtual int IndexOf(T item)
77+
{
78+
int ret = -1;
79+
80+
if (_FuncIndexOf != null) ret = _FuncIndexOf.Invoke(item);
81+
82+
return ret;
83+
}
84+
85+
public virtual void OnReset(int count)
86+
{
87+
if (_ActionOnReset != null) _ActionOnReset.Invoke(count);
88+
}
89+
}
90+
}

AlphaChiTech.Virtualization/BaseRepeatableActionVirtualization.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55

66
namespace AlphaChiTech.Virtualization
77
{
8+
/// <summary>
9+
/// Base class there the Action repeats on a periodic basis (the RepeatingSchedule) like BaseActionVirtualization
10+
/// simply implement the DoAction method.
11+
/// </summary>
812
public abstract class BaseRepeatableActionVirtualization : BaseActionVirtualization, IRepeatingVirtualizationAction
913
{
1014
protected DateTime _LastRun = DateTime.MinValue;
1115
private TimeSpan _RepeatingSchedule = TimeSpan.FromSeconds(1);
1216

17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="BaseRepeatableActionVirtualization"/> class.
19+
/// </summary>
20+
/// <param name="threadModel">The thread model.</param>
21+
/// <param name="isRepeating">if set to <c>true</c> [is repeating].</param>
22+
/// <param name="repeatingSchedule">The repeating schedule.</param>
1323
public BaseRepeatableActionVirtualization(VirtualActionThreadModelEnum threadModel = VirtualActionThreadModelEnum.UseUIThread,
1424
bool isRepeating = false, TimeSpan? repeatingSchedule = null)
1525
: base(threadModel)
@@ -21,6 +31,12 @@ public BaseRepeatableActionVirtualization(VirtualActionThreadModelEnum threadMod
2131
}
2232
}
2333

34+
/// <summary>
35+
/// Gets or sets the repeating schedule.
36+
/// </summary>
37+
/// <value>
38+
/// The repeating schedule.
39+
/// </value>
2440
public TimeSpan RepeatingSchedule
2541
{
2642
get { return _RepeatingSchedule; }
@@ -29,17 +45,31 @@ public TimeSpan RepeatingSchedule
2945

3046
private bool _IsRepeating = false;
3147

48+
/// <summary>
49+
/// Gets or sets a value indicating whether [is repeating].
50+
/// </summary>
51+
/// <value>
52+
/// <c>true</c> if [is repeating]; otherwise, <c>false</c>.
53+
/// </value>
3254
protected bool IsRepeating
3355
{
3456
get { return _IsRepeating; }
3557
set { _IsRepeating = value; }
3658
}
3759

60+
/// <summary>
61+
/// check to see if the action should be kept.
62+
/// </summary>
63+
/// <returns></returns>
3864
public virtual bool KeepInActionsList()
3965
{
4066
return this.IsRepeating;
4167
}
4268

69+
/// <summary>
70+
/// Determines whether [is due to run].
71+
/// </summary>
72+
/// <returns></returns>
4373
public virtual bool IsDueToRun()
4474
{
4575

AlphaChiTech.Virtualization/DateBasedPageExpiryComparer.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55

66
namespace AlphaChiTech.Virtualization
77
{
8+
/// <summary>
9+
/// An implementation of a IPageExiryComparer that uses DateTime to see if the update should be applied
10+
/// </summary>
811
public class DateBasedPageExpiryComparer : IPageExpiryComparer
912
{
1013

1114
private static DateBasedPageExpiryComparer _Instance = new DateBasedPageExpiryComparer();
1215

16+
/// <summary>
17+
/// Gets the default instance.
18+
/// </summary>
19+
/// <value>
20+
/// The default instance.
21+
/// </value>
1322
public static DateBasedPageExpiryComparer DefaultInstance
1423
{
1524
get
@@ -18,6 +27,12 @@ public static DateBasedPageExpiryComparer DefaultInstance
1827
}
1928
}
2029

30+
/// <summary>
31+
/// Determines whether [is update valid] [the specified page based on the updateAt].
32+
/// </summary>
33+
/// <param name="pageUpdateAt">The page update at - null or a DateTime.</param>
34+
/// <param name="updateAt">The update at - null or a DateTime.</param>
35+
/// <returns></returns>
2136
public bool IsUpdateValid(object pageUpdateAt, object updateAt)
2237
{
2338
bool isStillValid = true;

AlphaChiTech.Virtualization/PageReclaimOnTouched.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
namespace AlphaChiTech.Virtualization
77
{
8+
/// <summary>
9+
/// PageReclainOnTouched is a Page Reclaimer implementation that releases pages based on when
10+
/// they where last touched.
11+
/// </summary>
12+
/// <typeparam name="T"></typeparam>
813
public class PageReclaimOnTouched<T> : IPageReclaimer<T>
914
{
1015

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AlphaChiTech.Virtualization
8+
{
9+
public class PagedSourceProviderMakeAsync<T> : BasePagedSourceProvider<T>, IPagedSourceProviderAsync<T>
10+
{
11+
public PagedSourceProviderMakeAsync()
12+
{
13+
}
14+
15+
public PagedSourceProviderMakeAsync(
16+
Func<int, int, PagedSourceItemsPacket<T>> funcGetItemsAt = null,
17+
Func<int> funcGetCount = null,
18+
Func<T, int> funcIndexOf = null,
19+
Action<int> actionOnReset = null,
20+
Func<int, int, int, T> funcGetPlaceHolder = null
21+
)
22+
: base(funcGetItemsAt, funcGetCount, funcIndexOf, actionOnReset)
23+
{
24+
this.FuncGetPlaceHolder = funcGetPlaceHolder;
25+
}
26+
27+
private Func<int, int, int, T> _FuncGetPlaceHolder = null;
28+
29+
public Func<int, int, int, T> FuncGetPlaceHolder
30+
{
31+
get { return _FuncGetPlaceHolder; }
32+
set { _FuncGetPlaceHolder = value; }
33+
}
34+
35+
public Task<PagedSourceItemsPacket<T>> GetItemsAtAsync(int pageoffset, int count, bool usePlaceholder)
36+
{
37+
var tcs = new TaskCompletionSource<PagedSourceItemsPacket<T>>();
38+
39+
try
40+
{
41+
tcs.SetResult(this.GetItemsAt(pageoffset, count, usePlaceholder));
42+
}
43+
catch (Exception e)
44+
{
45+
tcs.SetException(e);
46+
}
47+
48+
return tcs.Task;
49+
}
50+
51+
public virtual T GetPlaceHolder(int index, int page, int offset)
52+
{
53+
T ret = default(T);
54+
55+
if (_FuncGetPlaceHolder != null)
56+
ret = _FuncGetPlaceHolder.Invoke(index, page, offset);
57+
58+
return ret;
59+
}
60+
61+
public Task<int> GetCountAsync()
62+
{
63+
var tcs = new TaskCompletionSource<int>();
64+
65+
try
66+
{
67+
tcs.SetResult(this.Count);
68+
} catch(Exception e)
69+
{
70+
tcs.SetException(e);
71+
}
72+
73+
return tcs.Task;
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)