Skip to content

Commit

Permalink
fixed scss option bug and Queue thread bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Feb 13, 2013
1 parent 57c2343 commit 1547b4c
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 156 deletions.
3 changes: 1 addition & 2 deletions OrangeBits/Compilers/OrangeCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ public static CompileResults Process(OrangeJob job)
Success = true,
InputPath = job.Path,
OutputPath = outPath,
IsNewFile = !exists,
Message = "Compiled"
IsNewFile = !exists
};
}
return results;
Expand Down
4 changes: 1 addition & 3 deletions OrangeBits/WebMatrixExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ protected void SourceFileChanged(object source, FileSystemEventArgs e)
case ".less":
doIt = prefUtility.GetPref(e.FullPath, "AutoCompileLess", true) as bool?;
break;
case ".scss":
doIt = prefUtility.GetPref(e.FullPath, "AutoCompileScss", true) as bool?;
break;
case ".scss":
case ".sass":
doIt = prefUtility.GetPref(e.FullPath, "AutoCompileSass", true) as bool?;
break;
Expand Down
308 changes: 157 additions & 151 deletions OrangeBits/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,159 +6,165 @@
using OrangeBits.Compilers;
using System.Linq;
using System.Windows.Threading;

using Microsoft.WebMatrix.Extensibility;
using System.Collections.Concurrent;

namespace OrangeBits
{
/// <summary>
/// class that monitors the queue of file change events, and invokes the
/// compiler when neccessary
/// </summary>
public class Worker
{
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------

#region Properties

/// <summary>
/// queue that holds all of the files that need to be processed
/// </summary>
protected Queue<OrangeJob> queue = new Queue<OrangeJob>();

/// <summary>
/// timer for our background thread, this polls the queue
/// </summary>
protected Timer timer = new Timer();

/// <summary>
/// reference to the host that contains the worker
/// </summary>
protected IWebMatrixHost host;

/// <summary>
///
/// </summary>
protected Dispatcher mainDispatcher = Dispatcher.CurrentDispatcher;

#endregion


//--------------------------------------------------------------------------
//
// Constructors
//
//--------------------------------------------------------------------------

#region Constructor
/// <summary>
/// create a new timer, and check the queue periodically for new requests
/// </summary>
public Worker(IWebMatrixHost host)
{
this.host = host;
timer.Interval = 500;
timer.Elapsed += new ElapsedEventHandler(t_Elapsed);
timer.AutoReset = false;
timer.Start();
}
#endregion


//--------------------------------------------------------------------------
//
// Event Handlers
//
//--------------------------------------------------------------------------

#region t_Elapsed
/// <summary>
/// if there are any items in the queue which are at least 100 ms old, process them
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void t_Elapsed(object sender, ElapsedEventArgs e)
{
while ((queue.Count > 0) && (DateTime.Now > queue.Peek().Time.AddMilliseconds(100)))
{
var item = queue.Dequeue();
ProcessItem(item);
}
timer.Start();
}
#endregion


//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------

#region AddItem
/// <summary>
/// add a new file to process into the queue
/// </summary>
/// <param name="path">object that contains the path and times</param>
public void AddItem(OrangeJob job)
{
// only add to the queue if the file isn't already in the queue
var exists = queue.Where(x => x.Path.ToLower() == job.Path.ToLower()).Count() > 0;
if (!exists)
this.queue.Enqueue(job);
}
#endregion

#region ProcessItem
/// <summary>
/// process a file, generating the compiled output
/// </summary>
/// <param name="item"></param>
protected void ProcessItem(OrangeJob job)
{

var threadedOpenCmd = new Action(() =>
{
mainDispatcher.Invoke(new Action(() =>
{
var openCommand = host.HostCommands.OpenFileInEditor;
if (openCommand.CanExecute(job.OutputPath))
openCommand.Execute(job.OutputPath);
}));
});

try
{
// do the actual compilation work



CompileResults results = OrangeCompiler.Process(job);

// show the notification bar to notify the user it happened
//host.ShowNotification(results.Message, "Open File", threadedOpenCmd);

// refresh the tree so the new file (if created) shows up
if (results.IsNewFile)
{
mainDispatcher.Invoke(new Action(() =>
{
var refreshCommand = host.HostCommands.GetCommand(CommonCommandIds.GroupId, (int)CommonCommandIds.Ids.Refresh);
if (refreshCommand.CanExecute(null))
refreshCommand.Execute(null);
}));
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
host.ShowNotification("There was an error processing " + job.Path, "Open File", threadedOpenCmd);
}
}
#endregion
}
/// <summary>
/// class that monitors the queue of file change events, and invokes the
/// compiler when neccessary
/// </summary>
public class Worker
{
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------

#region Properties

/// <summary>
/// queue that holds all of the files that need to be processed
/// </summary>
protected ConcurrentQueue<OrangeJob> queue = new ConcurrentQueue<OrangeJob>();

/// <summary>
/// timer for our background thread, this polls the queue
/// </summary>
protected Timer timer = new Timer();

/// <summary>
/// reference to the host that contains the worker
/// </summary>
protected IWebMatrixHost host;

/// <summary>
///
/// </summary>
protected Dispatcher mainDispatcher = Dispatcher.CurrentDispatcher;

#endregion


//--------------------------------------------------------------------------
//
// Constructors
//
//--------------------------------------------------------------------------

#region Constructor
/// <summary>
/// create a new timer, and check the queue periodically for new requests
/// </summary>
public Worker(IWebMatrixHost host)
{
this.host = host;
timer.Interval = 500;
timer.Elapsed += new ElapsedEventHandler(t_Elapsed);
timer.AutoReset = false;
timer.Start();
}
#endregion


//--------------------------------------------------------------------------
//
// Event Handlers
//
//--------------------------------------------------------------------------

#region t_Elapsed
/// <summary>
/// if there are any items in the queue which are at least 100 ms old, process them
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void t_Elapsed(object sender, ElapsedEventArgs e)
{
OrangeJob job;
while (queue.TryPeek(out job) && (DateTime.Now > job.Time.AddMilliseconds(100)))
{
if (queue.TryDequeue(out job))
{
ProcessItem(job);
}
}
timer.Start();
}
#endregion


//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------

#region AddItem
/// <summary>
/// add a new file to process into the queue
/// </summary>
/// <param name="path">object that contains the path and times</param>
public void AddItem(OrangeJob job)
{
// only add to the queue if the file isn't already in the queue
lock (queue)
{
var exists = queue.Where(x => x.Path.ToLower() == job.Path.ToLower()).Count() > 0;
if (!exists)
this.queue.Enqueue(job);
}
}
#endregion

#region ProcessItem
/// <summary>
/// process a file, generating the compiled output
/// </summary>
/// <param name="item"></param>
protected void ProcessItem(OrangeJob job)
{

var threadedOpenCmd = new Action(() =>
{
mainDispatcher.Invoke(new Action(() =>
{
var openCommand = host.HostCommands.OpenFileInEditor;
if (openCommand.CanExecute(job.OutputPath))
openCommand.Execute(job.OutputPath);
}));
});

try
{
// do the actual compilation work
CompileResults results = OrangeCompiler.Process(job);

// show the notification bar to notify the user it happened
if (!String.IsNullOrEmpty(results.Message))
{
host.ShowNotification(results.Message, "Open File", threadedOpenCmd);
}

// refresh the tree so the new file (if created) shows up
if (results.IsNewFile)
{
mainDispatcher.Invoke(new Action(() =>
{
var refreshCommand = host.HostCommands.GetCommand(CommonCommandIds.GroupId, (int)CommonCommandIds.Ids.Refresh);
if (refreshCommand.CanExecute(null))
refreshCommand.Execute(null);
}));
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
host.ShowNotification("There was an error processing " + job.Path, "Open File", threadedOpenCmd);
}
}
#endregion
}
}

0 comments on commit 1547b4c

Please sign in to comment.