summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Native/BatchableExtensions.cs
blob: 5f630e1b3f50bdd166fb6edf50962785afd51922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Runtime.CompilerServices;

namespace Xamarin.Forms.Platform.Tizen.Native
{
	internal static class BatchableExtensions
	{
		static readonly ConditionalWeakTable<IBatchable, BatchCount> s_counters = new ConditionalWeakTable<IBatchable, BatchCount>();

		public static void BatchBegin(this IBatchable target)
		{
			BatchCount value = null;

			if (s_counters.TryGetValue(target, out value))
			{
				value.Count++;
			}
			else
			{
				s_counters.Add(target, new BatchCount());
			}
		}

		public static void BatchCommit(this IBatchable target)
		{
			BatchCount value = null;
			if (s_counters.TryGetValue(target, out value))
			{
				value.Count--;
				if (value.Count == 0)
				{
					target.OnBatchCommitted();
				}
				else if (value.Count < 0)
				{
					Log.Error("Called BatchCommit() without BatchBegin().");
					value.Count = 0;
				}
			}
			else
			{
				Log.Error("Called BatchCommit() without BatchBegin().");
			}
		}

		public static bool IsBatched(this IBatchable target)
		{
			BatchCount value = null;

			if (s_counters.TryGetValue(target, out value))
			{
				return value.Count != 0;
			}
			else
			{
				return false;
			}
		}

		class BatchCount
		{
			public int Count = 1;
		}
	}
}