forked from codecadwallader/codemaid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeTreeBuilderAsync.cs
More file actions
108 lines (92 loc) · 3.23 KB
/
CodeTreeBuilderAsync.cs
File metadata and controls
108 lines (92 loc) · 3.23 KB
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
100
101
102
103
104
105
106
107
108
using SteveCadwallader.CodeMaid.Model.CodeItems;
using System;
using System.ComponentModel;
namespace SteveCadwallader.CodeMaid.Model.CodeTree
{
/// <summary>
/// A helper class for performing code tree building in an asynchronous context.
/// </summary>
internal class CodeTreeBuilderAsync
{
#region Fields
private readonly BackgroundWorker _bw;
private readonly Action<SnapshotCodeItems> _callback;
private CodeTreeRequest _pendingRequest;
#endregion Fields
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CodeTreeBuilderAsync" /> class.
/// </summary>
/// <param name="callback">The callback for results.</param>
internal CodeTreeBuilderAsync(Action<SnapshotCodeItems> callback)
{
_bw = new BackgroundWorker { WorkerSupportsCancellation = true };
_bw.DoWork += OnDoWork;
_bw.RunWorkerCompleted += OnRunWorkerCompleted;
_callback = callback;
}
#endregion Constructors
#region Internal Methods
/// <summary>
/// Builds a code tree asynchronously from the specified request.
/// </summary>
/// <param name="request">The request.</param>
internal void RetrieveCodeTreeAsync(CodeTreeRequest request)
{
if (_bw.IsBusy)
{
_pendingRequest = request;
_bw.CancelAsync();
}
else
{
_pendingRequest = null;
_bw.RunWorkerAsync(request);
}
}
#endregion Internal Methods
#region Private Methods
/// <summary>
/// Called when the background worker should perform its work.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">
/// The <see cref="System.ComponentModel.DoWorkEventArgs" /> instance containing the event data.
/// </param>
private static void OnDoWork(object sender, DoWorkEventArgs e)
{
if (!(e.Argument is CodeTreeRequest request) || request.RawCodeItems == null)
{
return;
}
var codeItems = CodeTreeBuilder.RetrieveCodeTree(request);
if (!e.Cancel)
{
e.Result = new SnapshotCodeItems(request.Document, codeItems);
}
}
/// <summary>
/// Called when the background worker has completed.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">
/// The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs" /> instance containing
/// the event data.
/// </param>
private void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (_pendingRequest != null)
{
RetrieveCodeTreeAsync(_pendingRequest);
}
else if (e.Error == null)
{
if (e.Result is SnapshotCodeItems snapshot)
{
_callback(snapshot);
}
}
}
#endregion Private Methods
}
}