Permalink
Browse files

In BufferIdleEventUtil, move the dispatcher timer to be stored in eac…

…h buffer's property bag instead of a static dictionary, which could hold text buffers around indefinitely if the timers aren't removed manually.
  • Loading branch information...
1 parent 9a8449c commit c6a4c460f5f6f6ad2c6f0a3e9241dbad7616ccee Noah Richards committed Mar 15, 2010
Showing with 34 additions and 15 deletions.
  1. +34 −15 BufferIdleEventUtil.cs
@@ -10,16 +10,14 @@ namespace MarkdownMode
static class BufferIdleEventUtil
{
static object bufferListenersKey = new object();
-
- static Dictionary<ITextBuffer, DispatcherTimer> bufferTimers
- = new Dictionary<ITextBuffer, DispatcherTimer>();
+ static object bufferTimerKey = new object();
#region Public interface
public static bool AddBufferIdleEventListener(ITextBuffer buffer, EventHandler handler)
{
HashSet<EventHandler> listenersForBuffer;
- if (!buffer.Properties.TryGetProperty(bufferListenersKey, out listenersForBuffer))
+ if (!TryGetBufferListeners(buffer, out listenersForBuffer))
listenersForBuffer = ConnectToBuffer(buffer);
if (listenersForBuffer.Contains(handler))
@@ -33,7 +31,7 @@ public static bool AddBufferIdleEventListener(ITextBuffer buffer, EventHandler h
public static bool RemoveBufferIdleEventListener(ITextBuffer buffer, EventHandler handler)
{
HashSet<EventHandler> listenersForBuffer;
- if (!buffer.Properties.TryGetProperty(bufferListenersKey, out listenersForBuffer))
+ if (!TryGetBufferListeners(buffer, out listenersForBuffer))
return false;
if (!listenersForBuffer.Contains(handler))
@@ -51,16 +49,38 @@ public static bool RemoveBufferIdleEventListener(ITextBuffer buffer, EventHandle
#region Helpers
- static void DisconnectFromBuffer(ITextBuffer buffer)
+ static bool TryGetBufferListeners(ITextBuffer buffer, out HashSet<EventHandler> listeners)
{
- buffer.Changed -= BufferChanged;
+ return buffer.Properties.TryGetProperty(bufferListenersKey, out listeners);
+ }
+
+ static void ClearBufferListeners(ITextBuffer buffer)
+ {
+ buffer.Properties.RemoveProperty(bufferListenersKey);
+ }
+ static bool TryGetBufferTimer(ITextBuffer buffer, out DispatcherTimer timer)
+ {
+ return buffer.Properties.TryGetProperty(bufferTimerKey, out timer);
+ }
+
+ static void ClearBufferTimer(ITextBuffer buffer)
+ {
DispatcherTimer timer;
- if (bufferTimers.TryGetValue(buffer, out timer))
+ if (TryGetBufferTimer(buffer, out timer))
{
- timer.Stop();
- bufferTimers.Remove(buffer);
+ if (timer != null)
+ timer.Stop();
+ buffer.Properties.RemoveProperty(bufferTimerKey);
}
+ }
+
+ static void DisconnectFromBuffer(ITextBuffer buffer)
+ {
+ buffer.Changed -= BufferChanged;
+
+ ClearBufferListeners(buffer);
+ ClearBufferTimer(buffer);
buffer.Properties.RemoveProperty(bufferListenersKey);
}
@@ -81,7 +101,7 @@ static void RestartTimerForBuffer(ITextBuffer buffer)
{
DispatcherTimer timer;
- if (bufferTimers.TryGetValue(buffer, out timer))
+ if (TryGetBufferTimer(buffer, out timer))
{
timer.Stop();
}
@@ -94,11 +114,10 @@ static void RestartTimerForBuffer(ITextBuffer buffer)
timer.Tick += (s, e) =>
{
- if (timer != null)
- timer.Stop();
+ ClearBufferTimer(buffer);
HashSet<EventHandler> handlers;
- if (buffer.Properties.TryGetProperty(bufferListenersKey, out handlers))
+ if (TryGetBufferListeners(buffer, out handlers))
{
foreach (var handler in handlers)
{
@@ -107,7 +126,7 @@ static void RestartTimerForBuffer(ITextBuffer buffer)
}
};
- bufferTimers[buffer] = timer;
+ buffer.Properties[bufferTimerKey] = timer;
}
timer.Start();

0 comments on commit c6a4c46

Please sign in to comment.