Skip to content

Commit

Permalink
Fixed a bug that caused the custom text plug to stop updating after c…
Browse files Browse the repository at this point in the history
…hanging its pattern
  • Loading branch information
Reavert committed Feb 17, 2024
1 parent 6df8588 commit a3ec86c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 10 additions & 6 deletions VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/CustomTextPlug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,41 @@ public CustomTextPlug(string id, string name)
_id = id;
_name = name;
}

public void SetPattern(string pattern)
{
_stringObserver?.Dispose();
if (_stringObserver == null)
{
_stringObserver = new StringObserver();
}

_stringObserver.Clear();

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

var stringObserver = new StringObserver();
foreach (var entry in entries)
{
switch (entry.Type)
{
case ObservableStringParser.EntryType.Text:
stringObserver.AddText(entry.Value);
_stringObserver.AddText(entry.Value);
break;

case ObservableStringParser.EntryType.Keyword:
var variable = _variableService.GetVariableByName(entry.Value);
if (variable != null)
stringObserver.AddText(new ObservableVariable(variable));
_stringObserver.AddText(new ObservableVariable(variable));
break;
}
}

_stringObserver = stringObserver;
_pattern = pattern;
}

public void ClearObserver()
{
_stringObserver?.Dispose();
_stringObserver?.Clear();
}

public override void Enable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace VisualStudioDiscordRPC.Shared.Plugs.TextPlugs
{
public class StringObserver : IDisposable
public class StringObserver
{
public event Action Changed;

Expand All @@ -23,10 +23,12 @@ public void AddText(string staticText)
AddText(staticTextSource);
}

public void Dispose()
public void Clear()
{
foreach (IObservableString observableString in _observableStrings)
observableString.Changed -= OnAnyTextSourceChanged;

_observableStrings.Clear();
}

private void OnAnyTextSourceChanged()
Expand Down

0 comments on commit a3ec86c

Please sign in to comment.