This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggregateSearchContext.cs
94 lines (82 loc) · 1.89 KB
/
AggregateSearchContext.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Threading;
namespace SDA_DonationTracker
{
public class AggregateSearchContext
{
private JoinWaiter waiter;
private Mutex AccessControl;
private JArray Data;
private SearchContext[] Contexts;
private List<TrackerError> Errors;
public AggregateSearchContext(params SearchContext[] contexts)
: this(contexts as IEnumerable<SearchContext>)
{
}
public AggregateSearchContext(IEnumerable<SearchContext> contexts)
{
this.Contexts = contexts.ToArray();
this.Data = new JArray();
this.waiter = new JoinWaiter(this.OnComplete);
this.AccessControl = new Mutex();
this.Errors = new List<TrackerError>();
}
public void Begin()
{
foreach (var context in this.Contexts)
{
waiter.AddProcess();
context.OnComplete += this.AddContents;
context.OnError += this.Failure;
context.Begin();
}
waiter.Start();
}
private void Failure(TrackerErrorType error, string message)
{
try
{
this.AccessControl.WaitOne();
this.Errors.Add(new TrackerError(error, message));
}
finally
{
this.AccessControl.ReleaseMutex();
}
waiter.ProcessComplete();
}
private void AddContents(JArray results)
{
try
{
this.AccessControl.WaitOne();
foreach (JObject obj in results)
this.Data.Add(obj);
}
finally
{
this.AccessControl.ReleaseMutex();
}
waiter.ProcessComplete();
}
private void OnComplete()
{
if (this.Errors.Count == 0)
{
if (this.Completed != null)
this.Completed(this.Data);
}
else
{
if (this.Error != null)
this.Error(this.Errors);
}
}
public event Action<JArray> Completed;
public event Action<IEnumerable<TrackerError>> Error;
}
}