Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/DependencyProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ const collator = new Intl.Collator('en', { numeric: true }); // for sorting SemV

// Initialization work done only once.
wasm.init();
const syncGetWorker /*: {| get: (string) => string, shutDown: () => void |} */ =
SyncGet.startWorker();
// Lazily start the worker until needed.
// This is important for the tests, which never exit otherwise.
let syncGetWorker_ /*: void | typeof SyncGet.SyncGetWorker */ = undefined;
function syncGetWorker() /*: typeof SyncGet.SyncGetWorker */ {
if (syncGetWorker_ === undefined) {
syncGetWorker_ = SyncGet.startWorker();
}
return syncGetWorker_;
}
Comment on lines +15 to +20
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, clean workaround.


// Cache of existing versions according to the package website.
class OnlineVersionsCache {
Expand Down Expand Up @@ -55,7 +62,7 @@ class OnlineVersionsCache {

// Complete cache with a remote call to the package server.
const remoteUrl = remotePackagesUrl + '/since/' + (versionsCount - 1); // -1 to check if no package was deleted.
const newVersions = JSON.parse(syncGetWorker.get(remoteUrl));
const newVersions = JSON.parse(syncGetWorker().get(remoteUrl));
if (newVersions.length === 0) {
// Reload from scratch since it means at least one package was deleted from the registry.
this.map = onlineVersionsFromScratch(cachePath, remotePackagesUrl);
Expand Down Expand Up @@ -230,7 +237,7 @@ function fetchElmJsonOnline(
// or because there was an error parsing `pkg` and `version`.
// In such case, this will throw again with `cacheElmJsonPath()` so it's fine.
const remoteUrl = remoteElmJsonUrl(pkg, version);
const elmJson = syncGetWorker.get(remoteUrl);
const elmJson = syncGetWorker().get(remoteUrl);
const cachePath = cacheElmJsonPath(pkg, version);
const parentDir = path.dirname(cachePath);
fs.mkdirSync(parentDir, { recursive: true });
Expand Down Expand Up @@ -261,7 +268,7 @@ function onlineVersionsFromScratch(
cachePath /*: string */,
remotePackagesUrl /*: string */
) /*: Map<string, Array<string>> */ {
const onlineVersionsJson = syncGetWorker.get(remotePackagesUrl);
const onlineVersionsJson = syncGetWorker().get(remotePackagesUrl);
fs.writeFileSync(cachePath, onlineVersionsJson);
const onlineVersions = JSON.parse(onlineVersionsJson);
try {
Expand Down
16 changes: 12 additions & 4 deletions lib/SyncGet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ const {
// $FlowFixMe[cannot-resolve-module]: Flow doesn’t seem to know about the `worker_threads` module yet.
} = require('worker_threads');

// Start a worker thread and return a `syncGetWorker`
// capable of making sync requests until shut down.
function startWorker() /*: {
// Poor man’s type alias. We can’t use /*:: type SyncGetWorker = ... */ because of:
// https://github.com/prettier/prettier/issues/2597
const SyncGetWorker /*: {
get: (string) => string,
shutDown: () => void,
} */ {
} */ = {
get: (string) => string,
shutDown: () => {},
};
Comment on lines +11 to +19
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha, i was trying to figure out how to do type aliases in old flow!


// Start a worker thread and return a `syncGetWorker`
// capable of making sync requests until shut down.
function startWorker() /*: typeof SyncGetWorker */ {
const { port1: localPort, port2: workerPort } = new MessageChannel();
const sharedLock = new SharedArrayBuffer(4);
// $FlowFixMe[incompatible-call]: Flow is wrong and says `sharedLock` is not an accepted parameter here.
Expand Down Expand Up @@ -41,5 +48,6 @@ function startWorker() /*: {
}

module.exports = {
SyncGetWorker,
startWorker,
};
Loading