Skip to content

Commit a3ec86c

Browse files
committed
Fixed a bug that caused the custom text plug to stop updating after changing its pattern
1 parent 6df8588 commit a3ec86c

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/CustomTextPlug.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,41 @@ public CustomTextPlug(string id, string name)
2424
_id = id;
2525
_name = name;
2626
}
27+
2728
public void SetPattern(string pattern)
2829
{
29-
_stringObserver?.Dispose();
30+
if (_stringObserver == null)
31+
{
32+
_stringObserver = new StringObserver();
33+
}
34+
35+
_stringObserver.Clear();
3036

3137
var parser = new ObservableStringParser();
3238
var entries = parser.Parse(pattern);
3339

34-
var stringObserver = new StringObserver();
3540
foreach (var entry in entries)
3641
{
3742
switch (entry.Type)
3843
{
3944
case ObservableStringParser.EntryType.Text:
40-
stringObserver.AddText(entry.Value);
45+
_stringObserver.AddText(entry.Value);
4146
break;
4247

4348
case ObservableStringParser.EntryType.Keyword:
4449
var variable = _variableService.GetVariableByName(entry.Value);
4550
if (variable != null)
46-
stringObserver.AddText(new ObservableVariable(variable));
51+
_stringObserver.AddText(new ObservableVariable(variable));
4752
break;
4853
}
4954
}
5055

51-
_stringObserver = stringObserver;
5256
_pattern = pattern;
5357
}
5458

5559
public void ClearObserver()
5660
{
57-
_stringObserver?.Dispose();
61+
_stringObserver?.Clear();
5862
}
5963

6064
public override void Enable()

VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/StringObserver.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace VisualStudioDiscordRPC.Shared.Plugs.TextPlugs
66
{
7-
public class StringObserver : IDisposable
7+
public class StringObserver
88
{
99
public event Action Changed;
1010

@@ -23,10 +23,12 @@ public void AddText(string staticText)
2323
AddText(staticTextSource);
2424
}
2525

26-
public void Dispose()
26+
public void Clear()
2727
{
2828
foreach (IObservableString observableString in _observableStrings)
2929
observableString.Changed -= OnAnyTextSourceChanged;
30+
31+
_observableStrings.Clear();
3032
}
3133

3234
private void OnAnyTextSourceChanged()

0 commit comments

Comments
 (0)