arendvw / ScriptParasite

A component that allows editting of C# definitions in external editors

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filesystem event handling: Do we need wait timer for files as we have another handle now for OnCreated?

dilomo opened this issue · comments

I'm wondering if we need that code anymore to slow down the plugin waiting:

public void OnFileChanged(Object sender, FileSystemEventArgs e)
        {
            if (IsBusy) return;
            IsBusy = true;
            try
            {
                if (!ShouldContinue())
                {
                    Cleanup();
                    return;
                }

--> Do we need it? --
                //// Visual studio will rename a file to a temp file,
                //// Then write new contents to the original file
                //// So here we check if a file that stopped existing, starts existing again..
                //if (!File.Exists(FileNameSafe))
                //{
                //    Thread.Sleep(200);
                //    if (!File.Exists(FileNameSafe))
                //    {
                //        Cleanup();
                //        return;
                //    }
                //}
-------------------

                // Modify the script at the end of the solution, rather than now
                // so we know for sure that nothing is in the middle of a 
                // solution, and could botch up things.
                OnPingDocument().SolutionEnd += WriteCodeToComponent;

                OnPingDocument().ScheduleSolution(10, doc =>
                {
                    // expire the script in the next solution, so it will recompute.
                    TargetComponent.ExpireSolution(false);
                });
            }
            catch (Exception exp)
            {
                RhinoApp.WriteLine($"Error in filehandler:{exp.Message}\\n{exp.StackTrace}");
            }
        }

I did a few tests and it seems to work ok but not sure what was your intention on cleaning up t so better consult with you

You're right in suggesting this code monstrosity should not exist!

The heart of this problem is that many editors don't send one event, but send many events.

Visual Studio:

  • Writes to {Filename}_Temp.cs
  • Removes {Filename}.cs
  • Moves {Filename}_Temp to {Filename}.cs

Visual Studio Code:

  • Deletes existing file, Creates new file and updates this file? (not completely sure).

In the master branch I've deleted this code monstrosity with the class ScriptFileWatcher().
This code waits for 300ms after each event to wait if there is a new event coming (Debounce), and once new events are no longer coming, only one event is thrown.

Does this also work for you?

That's fine with me but is 300ms not too long? I have no idea.

I've updated it to 100ms, should be more than enough for all the slow hard drives out there.