From 167aaa35aa0daa9acdb77c102c317900641dde91 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Tue, 20 Dec 2016 19:31:11 -0500
Subject: [PATCH] fix #165: line 107 of index.js creates possible race

---
 pd/nw/index.js | 28 +++++++++-------------------
 pd/nw/pdgui.js |  9 ++++++---
 2 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/pd/nw/index.js b/pd/nw/index.js
index 2436ef49c..9c36a77db 100644
--- a/pd/nw/index.js
+++ b/pd/nw/index.js
@@ -90,7 +90,7 @@ function nw_window_focus_callback() {
 }
 
 function connect() {
-    var gui_path;
+    var gui_path, file_path;
     if (have_args() && gui.App.argv.length > 1) {
         // Pd started the GUI, so connect to it on port provided in our args
         pdgui.post("Pd has started the GUI");
@@ -99,25 +99,15 @@ function connect() {
         // create a tcp server, then spawn Pd with "-guiport" flag and port
         gui_path = window.location.pathname;
         gui_path = gui_path.substr(0, gui_path.lastIndexOf('/'));
-        pdgui.post("GUI is starting Pd...");
-        pdgui.connect_as_server(gui_path);
-        if (have_args()) {
-            // Quick bugfix for OSX-- handle case where user clicks on a
-            // file and Pd hasn't been started yet.
-            window.setTimeout(function () {
-                if (gui.App.argv[0].slice(0, 7) === "file://") {
-                // Clicking on a Pd file with an installed OSX app bundle sends
-                // a single argument which is a file:// URI.
-                // With the OSX app bundle it is the GUI which starts the
-                // Pd process. So in this case, we just need to parse the
-                // file and open it.
-                // Selecting multiple files and clicking "Open" will trigger
-                // a separate "open" event for each file, so luckily we don't
-                // have to parse them.
-                    pdgui.menu_open(decodeURI(gui.App.argv[0].slice(7)));
-                }
-            }, 6000);
+        // On OSX, if the user double-clicks a pd file we get a single
+        // argument prefixed with "file://". If so, we parse it and
+        // feed it to the Pd instance as an argument to the "-open" flag
+        file_path = gui.App.argv[0];
+        if (have_args() && file_path.slice(0, 7) === "file://") {
+            file_path = decodeURI(file_path.slice(7));
         }
+        pdgui.post("GUI is starting Pd...");
+        pdgui.connect_as_server(gui_path, file_path);
     }
 }
 
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index b318f1045..83c90fcc9 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -1338,7 +1338,7 @@ exports.canvas_map = canvas_map;
 
 // If the GUI is started first (as in a Mac OSX Bundle) we use this
 // function to actually start the core
-function spawn_pd(gui_path, port) {
+function spawn_pd(gui_path, port, file_to_open) {
     post("gui_path is " + gui_path);
     var pd_binary,
         platform = process.platform,
@@ -1347,6 +1347,9 @@ function spawn_pd(gui_path, port) {
         // OSX -- this is currently tailored to work with an app bundle. It
         // hasn't been tested with a system install of pd-l2ork
         pd_binary = path.join("bin", "pd-l2ork");
+        if (file_to_open) {
+            flags.push("-open", file_to_open);
+        }
     } else {
         pd_binary = path.join(gui_path, "..", "bin", "pd-l2ork");
         flags.push("-nrt"); // for some reason realtime causes watchdog to die
@@ -1469,7 +1472,7 @@ function connect_as_client() {
 
 exports.connect_as_client = connect_as_client;
 
-function connect_as_server(gui_path) {
+function connect_as_server(gui_path, file_path) {
     var server = net.createServer(function(c) {
             post("incoming connection to GUI");
             connection = c;
@@ -1479,7 +1482,7 @@ function connect_as_server(gui_path) {
         ntries = 0,
         listener_callback = function() {
             post("GUI listening on port " + port + " on host " + HOST);
-            spawn_pd(gui_path, port);
+            spawn_pd(gui_path, port, file_path);
         };
     server.listen(port, HOST, listener_callback);
     // try to reconnect if necessary
-- 
GitLab