summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Internals/Ticker.cs
blob: 9b7c575b3dc17257d153514b5604ea7199f2b107 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;

namespace Xamarin.Forms.Internals
{
	public abstract class Ticker
	{
		static Ticker s_ticker;
		readonly Stopwatch _stopwatch;
		readonly List<Tuple<int, Func<long, bool>>> _timeouts;

		int _count;
		bool _enabled;

		protected Ticker()
		{
			_count = 0;
			_timeouts = new List<Tuple<int, Func<long, bool>>>();

			_stopwatch = new Stopwatch();
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		public static void SetDefault(Ticker ticker) => Default = ticker;
		public static Ticker Default
		{
			internal set { s_ticker = value; }
			get { return s_ticker ?? (s_ticker =  Device.PlatformServices.CreateTicker()); }
		}

		public virtual int Insert(Func<long, bool> timeout)
		{
			_count++;
			_timeouts.Add(new Tuple<int, Func<long, bool>>(_count, timeout));

			if (!_enabled)
			{
				_enabled = true;
				Enable();
			}

			return _count;
		}

		public virtual void Remove(int handle)
		{
			Device.BeginInvokeOnMainThread(() =>
			{
				_timeouts.RemoveAll(t => t.Item1 == handle);

				if (!_timeouts.Any())
				{
					_enabled = false;
					Disable();
				}
			});
		}

		protected abstract void DisableTimer();

		protected abstract void EnableTimer();
		
		protected void SendSignals(int timestep = -1)
		{
			long step = timestep >= 0 ? timestep : _stopwatch.ElapsedMilliseconds;
			_stopwatch.Reset();
			_stopwatch.Start();

			var localCopy = new List<Tuple<int, Func<long, bool>>>(_timeouts);
			foreach (Tuple<int, Func<long, bool>> timeout in localCopy)
			{
				bool remove = !timeout.Item2(step);
				if (remove)
					_timeouts.RemoveAll(t => t.Item1 == timeout.Item1);
			}

			if (!_timeouts.Any())
			{
				_enabled = false;
				Disable();
			}
		}

		void Disable()
		{
			_stopwatch.Reset();
			DisableTimer();
		}

		void Enable()
		{
			_stopwatch.Start();
			EnableTimer();
		}
	}
}