This repository was archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathDefaultWorkspaceEditorSettings.cs
94 lines (78 loc) · 3.44 KB
/
DefaultWorkspaceEditorSettings.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.Editor;
namespace Microsoft.VisualStudio.Editor.Razor
{
internal class DefaultWorkspaceEditorSettings : WorkspaceEditorSettings
{
private readonly EditorSettingsManager _editorSettingsManager;
private readonly EventHandler<EditorSettingsChangedEventArgs> _onChanged;
private EventHandler<EditorSettingsChangedEventArgs> _changed;
private readonly ForegroundDispatcher _foregroundDispatcher;
private int _listenerCount = 0;
public DefaultWorkspaceEditorSettings(ForegroundDispatcher foregroundDispatcher, EditorSettingsManager editorSettingsManager)
{
if (foregroundDispatcher == null)
{
throw new ArgumentNullException(nameof(foregroundDispatcher));
}
if (editorSettingsManager == null)
{
throw new ArgumentNullException(nameof(editorSettingsManager));
}
_foregroundDispatcher = foregroundDispatcher;
_editorSettingsManager = editorSettingsManager;
_onChanged = OnChanged;
}
public override event EventHandler<EditorSettingsChangedEventArgs> Changed
{
add
{
_foregroundDispatcher.AssertForegroundThread();
_listenerCount++;
_changed += value;
if (_listenerCount == 1)
{
// We bind to the editor settings manager only when we have listeners to avoid leaking memory.
// Basically we're relying on anyone listening to us to have an understanding of when they're going
// to be torn down. In Razor's case this will just be the document tracker factory (which does know).
AttachToEditorSettingsManager();
}
}
remove
{
_foregroundDispatcher.AssertForegroundThread();
_listenerCount--;
_changed -= value;
if (_listenerCount == 0)
{
// We detatch from the editor settings manager when no one is listening to allow us to be garbage
// collected in the case that the workspace is tearing down.
DetachFromEditorSettingsManager();
}
}
}
// Internal for testing
internal virtual void AttachToEditorSettingsManager()
{
_editorSettingsManager.Changed += _onChanged;
}
// Internal for testing
internal virtual void DetachFromEditorSettingsManager()
{
_editorSettingsManager.Changed -= _onChanged;
}
public override EditorSettings Current => _editorSettingsManager.Current;
// Internal for testing
internal void OnChanged(object sender, EditorSettingsChangedEventArgs e)
{
_foregroundDispatcher.AssertForegroundThread();
Debug.Assert(_changed != null, nameof(OnChanged) + " should not be invoked when there are no listeners.");
var args = new EditorSettingsChangedEventArgs(Current);
_changed?.Invoke(this, args);
}
}
}