Skip to content

Commit 876226a

Browse files
Tweaks to ensure only one instance per window
1 parent e16b6b2 commit 876226a

5 files changed

Lines changed: 136 additions & 23 deletions

File tree

src/nsfw-watcher-worker.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,6 @@ process.on('uncaughtException', (error) => {
208208

209209
process.title = `Pulsar file watcher worker (NSFW) [PID: ${process.pid}]`;
210210

211+
process.on('disconnect', () => process.exit(0));
212+
211213
module.exports = run;

src/parcel-watcher-worker.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,6 @@ process.on('uncaughtException', (error) => {
184184

185185
process.title = `Pulsar file watcher worker (Parcel) [PID: ${process.pid}]`;
186186

187+
process.on('disconnect', () => process.exit(0));
188+
187189
module.exports = run;

src/path-watcher.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class NativeWatcher {
177177
// been fully tracked down. That's fine, though; we can run it in its own
178178
// long-running task, much like VS Code does.
179179
class ParcelWatcherNativeWatcher extends NativeWatcher {
180-
static task = new Task(require.resolve('./parcel-watcher-worker.js'));
180+
static task = new Task(require.resolve('./parcel-watcher-worker.js'), { autoStart: false });
181181

182182
// Whether the task has been started.
183183
static started = false;
@@ -204,7 +204,7 @@ class ParcelWatcherNativeWatcher extends NativeWatcher {
204204
this.initialized = false;
205205
// Once a task is terminated, it cannot be started again. We have to
206206
// replace it with a new instance.
207-
this.task = new Task(require.resolve('./parcel-watcher-worker.js'));
207+
this.task = new Task(require.resolve('./parcel-watcher-worker.js'), { autoStart: false });
208208
this.PROMISE_META.clear();
209209
this.initialize();
210210
}
@@ -272,8 +272,8 @@ class ParcelWatcherNativeWatcher extends NativeWatcher {
272272
meta.promise = promise;
273273
this.PROMISE_META.set('self:start', meta);
274274
}
275-
await meta.promise;
276275
this.started = true;
276+
await meta.promise;
277277
}
278278

279279
static async sendEvent(event, args) {
@@ -371,7 +371,7 @@ class ParcelWatcherNativeWatcher extends NativeWatcher {
371371

372372
// A file-watcher implementation that uses `nsfw`. Runs in a separate process.
373373
class NSFWNativeWatcher extends NativeWatcher {
374-
static task = new Task(require.resolve('./nsfw-watcher-worker.js'));
374+
static task = new Task(require.resolve('./nsfw-watcher-worker.js'), { autoStart: false });
375375

376376
// Whether the task has been started.
377377
static started = false;
@@ -398,7 +398,7 @@ class NSFWNativeWatcher extends NativeWatcher {
398398
this.initialized = false;
399399
// Once a task is terminated, it cannot be started again. We have to
400400
// replace it with a new instance.
401-
this.task = new Task(require.resolve('./nsfw-watcher-worker.js'));
401+
this.task = new Task(require.resolve('./nsfw-watcher-worker.js'), { autoStart: false });
402402
this.PROMISE_META.clear();
403403
this.initialize();
404404
}
@@ -466,8 +466,8 @@ class NSFWNativeWatcher extends NativeWatcher {
466466
meta.promise = promise;
467467
this.PROMISE_META.set('self:start', meta);
468468
}
469-
await meta.promise;
470469
this.started = true;
470+
await meta.promise;
471471
}
472472

473473
static async sendEvent(event, args) {

src/task.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,13 @@ module.exports = class Task {
6666
//
6767
// * `taskPath` The {String} path to the CoffeeScript/JavaScript file that
6868
// exports a single {Function} to execute.
69-
constructor(taskPath) {
69+
constructor(taskPath, { autoStart = true } = {}) {
7070
this.emitter = new Emitter();
71-
const compileCachePath = require('./compile-cache').getCacheDirectory();
72-
taskPath = require.resolve(taskPath);
73-
const env = Object.assign({}, process.env, { userAgent: navigator.userAgent });
71+
this.autoStart = autoStart;
72+
this.taskPath = require.resolve(taskPath);
7473

75-
if (atom.unloading) {
76-
this.childProcess = null;
77-
} else {
78-
this.childProcess = ChildProcess.fork(
79-
require.resolve('./task-bootstrap'),
80-
[compileCachePath, taskPath],
81-
{ env, silent: true, windowsHide: true }
82-
);
74+
if (autoStart) {
75+
this.createChildProcess();
8376
}
8477

8578
this.on("task:log", (...args) => console.log(...args));
@@ -96,6 +89,20 @@ module.exports = class Task {
9689
this.callback(...args)
9790
}
9891
});
92+
}
93+
94+
createChildProcess () {
95+
const compileCachePath = require('./compile-cache').getCacheDirectory();
96+
const env = Object.assign({}, process.env, { userAgent: navigator.userAgent });
97+
if (window.atom?.unloading) {
98+
this.childProcess = null;
99+
} else {
100+
this.childProcess = ChildProcess.fork(
101+
require.resolve('./task-bootstrap'),
102+
[compileCachePath, this.taskPath],
103+
{ env, silent: true, windowsHide: true }
104+
);
105+
}
99106
this.handleEvents();
100107
}
101108

@@ -130,10 +137,13 @@ module.exports = class Task {
130137
// Don't spawn any new tasks during shutdown.
131138
if (atom.unloading) return;
132139
const [callback] = args.splice(-1);
133-
if (this.childProcess == null) {
134-
throw new Error('Cannot start terminated process');
140+
if (this.autoStart && !this.childProcess) {
141+
if (this.childProcess == null) {
142+
throw new Error('Cannot start terminated process');
143+
}
144+
} else if (!this.autoStart && !this.childProcess) {
145+
this.createChildProcess();
135146
}
136-
this.handleEvents();
137147
if (_.isFunction(callback)) {
138148
this.callback = callback;
139149
} else {

yarn.lock

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,95 @@
16541654
dependencies:
16551655
"@noble/hashes" "^1.1.5"
16561656

1657+
"@parcel/watcher-android-arm64@2.5.6":
1658+
version "2.5.6"
1659+
resolved "https://registry.yarnpkg.com/@parcel/watcher-android-arm64/-/watcher-android-arm64-2.5.6.tgz#5f32e0dba356f4ac9a11068d2a5c134ca3ba6564"
1660+
integrity sha512-YQxSS34tPF/6ZG7r/Ih9xy+kP/WwediEUsqmtf0cuCV5TPPKw/PQHRhueUo6JdeFJaqV3pyjm0GdYjZotbRt/A==
1661+
1662+
"@parcel/watcher-darwin-arm64@2.5.6":
1663+
version "2.5.6"
1664+
resolved "https://registry.yarnpkg.com/@parcel/watcher-darwin-arm64/-/watcher-darwin-arm64-2.5.6.tgz#88d3e720b59b1eceffce98dac46d7c40e8be5e8e"
1665+
integrity sha512-Z2ZdrnwyXvvvdtRHLmM4knydIdU9adO3D4n/0cVipF3rRiwP+3/sfzpAwA/qKFL6i1ModaabkU7IbpeMBgiVEA==
1666+
1667+
"@parcel/watcher-darwin-x64@2.5.6":
1668+
version "2.5.6"
1669+
resolved "https://registry.yarnpkg.com/@parcel/watcher-darwin-x64/-/watcher-darwin-x64-2.5.6.tgz#bf05d76a78bc15974f15ec3671848698b0838063"
1670+
integrity sha512-HgvOf3W9dhithcwOWX9uDZyn1lW9R+7tPZ4sug+NGrGIo4Rk1hAXLEbcH1TQSqxts0NYXXlOWqVpvS1SFS4fRg==
1671+
1672+
"@parcel/watcher-freebsd-x64@2.5.6":
1673+
version "2.5.6"
1674+
resolved "https://registry.yarnpkg.com/@parcel/watcher-freebsd-x64/-/watcher-freebsd-x64-2.5.6.tgz#8bc26e9848e7303ac82922a5ae1b1ef1bdb48a53"
1675+
integrity sha512-vJVi8yd/qzJxEKHkeemh7w3YAn6RJCtYlE4HPMoVnCpIXEzSrxErBW5SJBgKLbXU3WdIpkjBTeUNtyBVn8TRng==
1676+
1677+
"@parcel/watcher-linux-arm-glibc@2.5.6":
1678+
version "2.5.6"
1679+
resolved "https://registry.yarnpkg.com/@parcel/watcher-linux-arm-glibc/-/watcher-linux-arm-glibc-2.5.6.tgz#1328fee1deb0c2d7865079ef53a2ba4cc2f8b40a"
1680+
integrity sha512-9JiYfB6h6BgV50CCfasfLf/uvOcJskMSwcdH1PHH9rvS1IrNy8zad6IUVPVUfmXr+u+Km9IxcfMLzgdOudz9EQ==
1681+
1682+
"@parcel/watcher-linux-arm-musl@2.5.6":
1683+
version "2.5.6"
1684+
resolved "https://registry.yarnpkg.com/@parcel/watcher-linux-arm-musl/-/watcher-linux-arm-musl-2.5.6.tgz#bad0f45cb3e2157746db8b9d22db6a125711f152"
1685+
integrity sha512-Ve3gUCG57nuUUSyjBq/MAM0CzArtuIOxsBdQ+ftz6ho8n7s1i9E1Nmk/xmP323r2YL0SONs1EuwqBp2u1k5fxg==
1686+
1687+
"@parcel/watcher-linux-arm64-glibc@2.5.6":
1688+
version "2.5.6"
1689+
resolved "https://registry.yarnpkg.com/@parcel/watcher-linux-arm64-glibc/-/watcher-linux-arm64-glibc-2.5.6.tgz#b75913fbd501d9523c5f35d420957bf7d0204809"
1690+
integrity sha512-f2g/DT3NhGPdBmMWYoxixqYr3v/UXcmLOYy16Bx0TM20Tchduwr4EaCbmxh1321TABqPGDpS8D/ggOTaljijOA==
1691+
1692+
"@parcel/watcher-linux-arm64-musl@2.5.6":
1693+
version "2.5.6"
1694+
resolved "https://registry.yarnpkg.com/@parcel/watcher-linux-arm64-musl/-/watcher-linux-arm64-musl-2.5.6.tgz#da5621a6a576070c8c0de60dea8b46dc9c3827d4"
1695+
integrity sha512-qb6naMDGlbCwdhLj6hgoVKJl2odL34z2sqkC7Z6kzir8b5W65WYDpLB6R06KabvZdgoHI/zxke4b3zR0wAbDTA==
1696+
1697+
"@parcel/watcher-linux-x64-glibc@2.5.6":
1698+
version "2.5.6"
1699+
resolved "https://registry.yarnpkg.com/@parcel/watcher-linux-x64-glibc/-/watcher-linux-x64-glibc-2.5.6.tgz#ce437accdc4b30f93a090b4a221fd95cd9b89639"
1700+
integrity sha512-kbT5wvNQlx7NaGjzPFu8nVIW1rWqV780O7ZtkjuWaPUgpv2NMFpjYERVi0UYj1msZNyCzGlaCWEtzc+exjMGbQ==
1701+
1702+
"@parcel/watcher-linux-x64-musl@2.5.6":
1703+
version "2.5.6"
1704+
resolved "https://registry.yarnpkg.com/@parcel/watcher-linux-x64-musl/-/watcher-linux-x64-musl-2.5.6.tgz#02400c54b4a67efcc7e2327b249711920ac969e2"
1705+
integrity sha512-1JRFeC+h7RdXwldHzTsmdtYR/Ku8SylLgTU/reMuqdVD7CtLwf0VR1FqeprZ0eHQkO0vqsbvFLXUmYm/uNKJBg==
1706+
1707+
"@parcel/watcher-win32-arm64@2.5.6":
1708+
version "2.5.6"
1709+
resolved "https://registry.yarnpkg.com/@parcel/watcher-win32-arm64/-/watcher-win32-arm64-2.5.6.tgz#caae3d3c7583ca0a7171e6bd142c34d20ea1691e"
1710+
integrity sha512-3ukyebjc6eGlw9yRt678DxVF7rjXatWiHvTXqphZLvo7aC5NdEgFufVwjFfY51ijYEWpXbqF5jtrK275z52D4Q==
1711+
1712+
"@parcel/watcher-win32-ia32@2.5.6":
1713+
version "2.5.6"
1714+
resolved "https://registry.yarnpkg.com/@parcel/watcher-win32-ia32/-/watcher-win32-ia32-2.5.6.tgz#9ac922550896dfe47bfc5ae3be4f1bcaf8155d6d"
1715+
integrity sha512-k35yLp1ZMwwee3Ez/pxBi5cf4AoBKYXj00CZ80jUz5h8prpiaQsiRPKQMxoLstNuqe2vR4RNPEAEcjEFzhEz/g==
1716+
1717+
"@parcel/watcher-win32-x64@2.5.6":
1718+
version "2.5.6"
1719+
resolved "https://registry.yarnpkg.com/@parcel/watcher-win32-x64/-/watcher-win32-x64-2.5.6.tgz#73fdafba2e21c448f0e456bbe13178d8fe11739d"
1720+
integrity sha512-hbQlYcCq5dlAX9Qx+kFb0FHue6vbjlf0FrNzSKdYK2APUf7tGfGxQCk2ihEREmbR6ZMc0MVAD5RIX/41gpUzTw==
1721+
1722+
"@parcel/watcher@^2.5.1":
1723+
version "2.5.6"
1724+
resolved "https://registry.yarnpkg.com/@parcel/watcher/-/watcher-2.5.6.tgz#3f932828c894f06d0ad9cfefade1756ecc6ef1f1"
1725+
integrity sha512-tmmZ3lQxAe/k/+rNnXQRawJ4NjxO2hqiOLTHvWchtGZULp4RyFeh6aU4XdOYBFe2KE1oShQTv4AblOs2iOrNnQ==
1726+
dependencies:
1727+
detect-libc "^2.0.3"
1728+
is-glob "^4.0.3"
1729+
node-addon-api "^7.0.0"
1730+
picomatch "^4.0.3"
1731+
optionalDependencies:
1732+
"@parcel/watcher-android-arm64" "2.5.6"
1733+
"@parcel/watcher-darwin-arm64" "2.5.6"
1734+
"@parcel/watcher-darwin-x64" "2.5.6"
1735+
"@parcel/watcher-freebsd-x64" "2.5.6"
1736+
"@parcel/watcher-linux-arm-glibc" "2.5.6"
1737+
"@parcel/watcher-linux-arm-musl" "2.5.6"
1738+
"@parcel/watcher-linux-arm64-glibc" "2.5.6"
1739+
"@parcel/watcher-linux-arm64-musl" "2.5.6"
1740+
"@parcel/watcher-linux-x64-glibc" "2.5.6"
1741+
"@parcel/watcher-linux-x64-musl" "2.5.6"
1742+
"@parcel/watcher-win32-arm64" "2.5.6"
1743+
"@parcel/watcher-win32-ia32" "2.5.6"
1744+
"@parcel/watcher-win32-x64" "2.5.6"
1745+
16571746
"@playwright/test@1.22.2":
16581747
version "1.22.2"
16591748
resolved "https://registry.yarnpkg.com/@playwright/test/-/test-1.22.2.tgz#b848f25f8918140c2d0bae8e9227a40198f2dd4a"
@@ -3897,7 +3986,7 @@ detect-libc@^1.0.3:
38973986
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
38983987
integrity sha512-pGjwhsmsp4kL2RTz08wcOlGN83otlqHeD/Z5T8GXZB+/YcpQ/dgo+lbU8ZsGxV0HIvqqxo9l7mqYwyYMD9bKDg==
38993988

3900-
detect-libc@^2.0.0, detect-libc@^2.0.1:
3989+
detect-libc@^2.0.0, detect-libc@^2.0.1, detect-libc@^2.0.3:
39013990
version "2.1.2"
39023991
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-2.1.2.tgz#689c5dcdc1900ef5583a4cb9f6d7b473742074ad"
39033992
integrity sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==
@@ -4801,6 +4890,11 @@ fd-slicer@~1.1.0:
48014890
dependencies:
48024891
pend "~1.2.0"
48034892

4893+
fdir@6.4.6:
4894+
version "6.4.6"
4895+
resolved "https://registry.yarnpkg.com/fdir/-/fdir-6.4.6.tgz#2b268c0232697063111bbf3f64810a2a741ba281"
4896+
integrity sha512-hiFoqpyZcfNm1yc4u8oWCf9A2c4D3QjCrks3zmoVKVxpQRzmPNar1hUJcBG2RQHvEVGDN+Jm81ZheVLAQMK6+w==
4897+
48044898
file-entry-cache@^6.0.1:
48054899
version "6.0.1"
48064900
resolved "https://registry.yarnpkg.com/file-entry-cache/-/file-entry-cache-6.0.1.tgz#211b2dd9659cb0394b073e7323ac3c933d522027"
@@ -7494,7 +7588,7 @@ node-addon-api@^6.1.0:
74947588
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-6.1.0.tgz#ac8470034e58e67d0c6f1204a18ae6995d9c0d76"
74957589
integrity sha512-+eawOlIgy680F0kBzPUNFhMZGtJ1YmqM6l4+Crf4IkImjYrO/mqPwRMh352g23uIaQKFItcQ64I7KMaJxHgAVA==
74967590

7497-
node-addon-api@^7.1.1:
7591+
node-addon-api@^7.0.0, node-addon-api@^7.1.1:
74987592
version "7.1.1"
74997593
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-7.1.1.tgz#1aba6693b0f255258a049d621329329322aad558"
75007594
integrity sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==
@@ -7995,6 +8089,11 @@ picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.3.1:
79958089
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
79968090
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==
79978091

8092+
picomatch@^4.0.3:
8093+
version "4.0.3"
8094+
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-4.0.3.tgz#796c76136d1eead715db1e7bad785dedd695a042"
8095+
integrity sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==
8096+
79988097
pify@^4.0.1:
79998098
version "4.0.1"
80008099
resolved "https://registry.yarnpkg.com/pify/-/pify-4.0.1.tgz#4b2cd25c50d598735c50292224fd8c6df41e3231"

0 commit comments

Comments
 (0)