repo: Simplify watch toggling mechanism
This commit is contained in:
parent
c99e57ea74
commit
fccb790a2d
|
@ -417,8 +417,6 @@ namespace SparkleLib.Git {
|
||||||
// Merges the fetched changes
|
// Merges the fetched changes
|
||||||
private void Rebase ()
|
private void Rebase ()
|
||||||
{
|
{
|
||||||
DisableWatching ();
|
|
||||||
|
|
||||||
if (HasLocalChanges) {
|
if (HasLocalChanges) {
|
||||||
Add ();
|
Add ();
|
||||||
|
|
||||||
|
@ -441,8 +439,6 @@ namespace SparkleLib.Git {
|
||||||
SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Conflict resolved");
|
SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Conflict resolved");
|
||||||
OnConflictResolved ();
|
OnConflictResolved ();
|
||||||
}
|
}
|
||||||
|
|
||||||
EnableWatching ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,6 @@ namespace SparkleLib {
|
||||||
private SparkleListenerBase listener;
|
private SparkleListenerBase listener;
|
||||||
private System.Timers.Timer remote_timer = new System.Timers.Timer () { Interval = 5 * 1000 };
|
private System.Timers.Timer remote_timer = new System.Timers.Timer () { Interval = 5 * 1000 };
|
||||||
private DateTime last_poll = DateTime.Now;
|
private DateTime last_poll = DateTime.Now;
|
||||||
private Object watch_lock = new Object ();
|
|
||||||
private Object change_lock = new Object ();
|
private Object change_lock = new Object ();
|
||||||
private double progress_percentage = 0.0;
|
private double progress_percentage = 0.0;
|
||||||
private string progress_speed = "";
|
private string progress_speed = "";
|
||||||
|
@ -176,7 +175,7 @@ namespace SparkleLib {
|
||||||
CreateInitialChangeSet ();
|
CreateInitialChangeSet ();
|
||||||
|
|
||||||
ChangeSets = GetChangeSets ();
|
ChangeSets = GetChangeSets ();
|
||||||
CreateWatcher ();
|
this.watcher = CreateWatcher ();
|
||||||
|
|
||||||
new Thread (
|
new Thread (
|
||||||
new ThreadStart (delegate {
|
new ThreadStart (delegate {
|
||||||
|
@ -208,13 +207,13 @@ namespace SparkleLib {
|
||||||
// Sync up everything that changed
|
// Sync up everything that changed
|
||||||
// since we've been offline
|
// since we've been offline
|
||||||
if (HasLocalChanges) {
|
if (HasLocalChanges) {
|
||||||
DisableWatching ();
|
this.watcher.Disable ();
|
||||||
SyncUpBase ();
|
SyncUpBase ();
|
||||||
|
|
||||||
while (HasUnsyncedChanges)
|
while (HasUnsyncedChanges)
|
||||||
SyncUpBase ();
|
SyncUpBase ();
|
||||||
|
|
||||||
EnableWatching ();
|
this.watcher.Enable ();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.remote_timer.Start ();
|
this.remote_timer.Start ();
|
||||||
|
@ -236,6 +235,18 @@ namespace SparkleLib {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private SparkleWatcher CreateWatcher ()
|
||||||
|
{
|
||||||
|
SparkleWatcher watcher = new SparkleWatcher (LocalPath);
|
||||||
|
|
||||||
|
watcher.ChangeEvent += delegate (FileSystemEventArgs args) {
|
||||||
|
OnFileActivity (args);
|
||||||
|
};
|
||||||
|
|
||||||
|
return watcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Starts a timer when something changes
|
// Starts a timer when something changes
|
||||||
public void OnFileActivity (FileSystemEventArgs args)
|
public void OnFileActivity (FileSystemEventArgs args)
|
||||||
{
|
{
|
||||||
|
@ -254,7 +265,7 @@ namespace SparkleLib {
|
||||||
|
|
||||||
if (!this.is_buffering && HasLocalChanges) {
|
if (!this.is_buffering && HasLocalChanges) {
|
||||||
this.is_buffering = true;
|
this.is_buffering = true;
|
||||||
DisableWatching ();
|
this.watcher.Disable ();
|
||||||
this.remote_timer.Stop ();
|
this.remote_timer.Stop ();
|
||||||
|
|
||||||
SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Activity detected, waiting for it to settle...");
|
SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Activity detected, waiting for it to settle...");
|
||||||
|
@ -268,8 +279,8 @@ namespace SparkleLib {
|
||||||
if (size_buffer.Count >= 4)
|
if (size_buffer.Count >= 4)
|
||||||
size_buffer.RemoveAt (0);
|
size_buffer.RemoveAt (0);
|
||||||
|
|
||||||
DirectoryInfo dir_info = new DirectoryInfo (LocalPath);
|
DirectoryInfo info = new DirectoryInfo (LocalPath);
|
||||||
size_buffer.Add (CalculateSize (dir_info));
|
size_buffer.Add (CalculateSize (info));
|
||||||
|
|
||||||
if (size_buffer.Count >= 4 &&
|
if (size_buffer.Count >= 4 &&
|
||||||
size_buffer [0].Equals (size_buffer [1]) &&
|
size_buffer [0].Equals (size_buffer [1]) &&
|
||||||
|
@ -279,13 +290,14 @@ namespace SparkleLib {
|
||||||
SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Activity has settled");
|
SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Activity has settled");
|
||||||
this.is_buffering = false;
|
this.is_buffering = false;
|
||||||
|
|
||||||
DisableWatching ();
|
this.watcher.Disable ();
|
||||||
while (HasLocalChanges)
|
while (HasLocalChanges)
|
||||||
SyncUpBase ();
|
SyncUpBase ();
|
||||||
EnableWatching ();
|
this.watcher.Enable ();
|
||||||
}
|
|
||||||
|
|
||||||
Thread.Sleep (500);
|
} else {
|
||||||
|
Thread.Sleep (500);
|
||||||
|
}
|
||||||
|
|
||||||
} while (this.is_buffering);
|
} while (this.is_buffering);
|
||||||
}
|
}
|
||||||
|
@ -296,7 +308,7 @@ namespace SparkleLib {
|
||||||
private void SyncUpBase ()
|
private void SyncUpBase ()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
DisableWatching ();
|
this.watcher.Disable ();
|
||||||
this.remote_timer.Stop ();
|
this.remote_timer.Stop ();
|
||||||
|
|
||||||
SparkleHelpers.DebugInfo ("SyncUp", "[" + Name + "] Initiated");
|
SparkleHelpers.DebugInfo ("SyncUp", "[" + Name + "] Initiated");
|
||||||
|
@ -319,7 +331,7 @@ namespace SparkleLib {
|
||||||
|
|
||||||
HasUnsyncedChanges = true;
|
HasUnsyncedChanges = true;
|
||||||
SyncDownBase ();
|
SyncDownBase ();
|
||||||
DisableWatching ();
|
this.watcher.Disable ();
|
||||||
|
|
||||||
if (this.server_online && SyncUp ()) {
|
if (this.server_online && SyncUp ()) {
|
||||||
HasUnsyncedChanges = false;
|
HasUnsyncedChanges = false;
|
||||||
|
@ -339,7 +351,7 @@ namespace SparkleLib {
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
this.remote_timer.Start ();
|
this.remote_timer.Start ();
|
||||||
EnableWatching ();
|
this.watcher.Enable ();
|
||||||
|
|
||||||
this.progress_percentage = 0.0;
|
this.progress_percentage = 0.0;
|
||||||
this.progress_speed = "";
|
this.progress_speed = "";
|
||||||
|
@ -351,7 +363,7 @@ namespace SparkleLib {
|
||||||
{
|
{
|
||||||
SparkleHelpers.DebugInfo ("SyncDown", "[" + Name + "] Initiated");
|
SparkleHelpers.DebugInfo ("SyncDown", "[" + Name + "] Initiated");
|
||||||
this.remote_timer.Stop ();
|
this.remote_timer.Stop ();
|
||||||
DisableWatching ();
|
this.watcher.Disable ();
|
||||||
|
|
||||||
if (SyncStatusChanged != null)
|
if (SyncStatusChanged != null)
|
||||||
SyncStatusChanged (SyncStatus.SyncDown);
|
SyncStatusChanged (SyncStatus.SyncDown);
|
||||||
|
@ -405,16 +417,7 @@ namespace SparkleLib {
|
||||||
SyncStatusChanged (SyncStatus.Idle);
|
SyncStatusChanged (SyncStatus.Idle);
|
||||||
|
|
||||||
this.remote_timer.Start ();
|
this.remote_timer.Start ();
|
||||||
EnableWatching ();
|
this.watcher.Enable ();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void CreateWatcher ()
|
|
||||||
{
|
|
||||||
this.watcher = new SparkleWatcher (LocalPath);
|
|
||||||
this.watcher.ChangeEvent += delegate (FileSystemEventArgs args) {
|
|
||||||
OnFileActivity (args);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -481,22 +484,6 @@ namespace SparkleLib {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void DisableWatching ()
|
|
||||||
{
|
|
||||||
lock (this.watch_lock) {
|
|
||||||
this.watcher.EnableRaisingEvents = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected void EnableWatching ()
|
|
||||||
{
|
|
||||||
lock (this.watch_lock) {
|
|
||||||
this.watcher.EnableRaisingEvents = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private DateTime progress_last_change = DateTime.Now;
|
private DateTime progress_last_change = DateTime.Now;
|
||||||
private TimeSpan progress_change_interval = new TimeSpan (0, 0, 0, 1);
|
private TimeSpan progress_change_interval = new TimeSpan (0, 0, 0, 1);
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ namespace SparkleLib {
|
||||||
public delegate void ChangeEventEventHandler (FileSystemEventArgs args);
|
public delegate void ChangeEventEventHandler (FileSystemEventArgs args);
|
||||||
public event ChangeEventEventHandler ChangeEvent;
|
public event ChangeEventEventHandler ChangeEvent;
|
||||||
|
|
||||||
|
private Object thread_lock = new Object ();
|
||||||
|
|
||||||
|
|
||||||
public SparkleWatcher (string path) : base (path)
|
public SparkleWatcher (string path) : base (path)
|
||||||
{
|
{
|
||||||
|
@ -52,5 +54,19 @@ namespace SparkleLib {
|
||||||
ChangeEvent (args);
|
ChangeEvent (args);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Enable ()
|
||||||
|
{
|
||||||
|
lock (this.thread_lock)
|
||||||
|
EnableRaisingEvents = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Disable ()
|
||||||
|
{
|
||||||
|
lock (this.thread_lock)
|
||||||
|
EnableRaisingEvents = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -192,7 +192,7 @@ namespace SparkleShare {
|
||||||
|
|
||||||
[DllImport("/System/Library/Frameworks/CoreServices.framework/CoreServices")]
|
[DllImport("/System/Library/Frameworks/CoreServices.framework/CoreServices")]
|
||||||
private extern static IntPtr CFRunLoopGetMain ();
|
private extern static IntPtr CFRunLoopGetMain ();
|
||||||
|
|
||||||
[DllImport("/System/Library/Frameworks/CoreServices.framework/CoreServices")]
|
[DllImport("/System/Library/Frameworks/CoreServices.framework/CoreServices")]
|
||||||
private extern static IntPtr FSEventStreamCreate (
|
private extern static IntPtr FSEventStreamCreate (
|
||||||
IntPtr allocator,
|
IntPtr allocator,
|
||||||
|
|
Loading…
Reference in a new issue