arendvw / ScriptParasite

A component that allows editting of C# definitions in external editors
22 stars 6 forks source link

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

Closed dilomo closed 3 years ago

dilomo commented 3 years ago

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

arendvw commented 3 years ago

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:

Visual Studio Code:

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?

dilomo commented 3 years ago

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

arendvw commented 3 years ago

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