-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor to use make-synchronized
#18
Merged
+50
−167
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,168 +1,10 @@ | ||
"use strict"; | ||
|
||
const { | ||
Worker, | ||
receiveMessageOnPort, | ||
MessageChannel, | ||
} = require("worker_threads"); | ||
const url = require("url"); | ||
const path = require("path"); | ||
const { makeSynchronizedModule } = require("make-synchronized"); | ||
|
||
/** | ||
@template {keyof PrettierSynchronizedFunctions} FunctionName | ||
@typedef {(...args: Parameters<Prettier[FunctionName]>) => Awaited<ReturnType<Prettier[FunctionName]>> } PrettierSyncFunction | ||
*/ | ||
|
||
/** | ||
@typedef {import("prettier")} Prettier | ||
@typedef {{ [FunctionName in typeof PRETTIER_ASYNC_FUNCTIONS[number]]: PrettierSyncFunction<FunctionName> }} PrettierSynchronizedFunctions | ||
@typedef {{ [PropertyName in typeof PRETTIER_STATIC_PROPERTIES[number]]: Prettier[PropertyName] }} PrettierStaticProperties | ||
@typedef {PrettierSynchronizedFunctions & PrettierStaticProperties} SynchronizedPrettier | ||
*/ | ||
|
||
const PRETTIER_ASYNC_FUNCTIONS = /** @type {const} */ ([ | ||
"formatWithCursor", | ||
"format", | ||
"check", | ||
"resolveConfig", | ||
"resolveConfigFile", | ||
"clearConfigCache", | ||
"getFileInfo", | ||
"getSupportInfo", | ||
]); | ||
|
||
const PRETTIER_STATIC_PROPERTIES = /** @type {const} */ ([ | ||
"version", | ||
"util", | ||
"doc", | ||
]); | ||
|
||
/** @type {Worker | undefined} */ | ||
let worker; | ||
function createWorker() { | ||
if (!worker) { | ||
worker = new Worker(require.resolve("./worker.js")); | ||
worker.unref(); | ||
} | ||
|
||
return worker; | ||
} | ||
|
||
/** | ||
* @template {keyof PrettierSynchronizedFunctions} FunctionName | ||
* @param {FunctionName} functionName | ||
* @param {string} prettierEntry | ||
* @returns {PrettierSyncFunction<FunctionName>} | ||
*/ | ||
function createSynchronizedFunction(functionName, prettierEntry) { | ||
return (...args) => { | ||
const signal = new Int32Array(new SharedArrayBuffer(4)); | ||
const { port1: localPort, port2: workerPort } = new MessageChannel(); | ||
const worker = createWorker(); | ||
|
||
worker.postMessage( | ||
{ signal, port: workerPort, functionName, args, prettierEntry }, | ||
[workerPort], | ||
); | ||
|
||
Atomics.wait(signal, 0, 0); | ||
|
||
const { | ||
message: { result, error, errorData }, | ||
} = receiveMessageOnPort(localPort); | ||
|
||
if (error) { | ||
throw Object.assign(error, errorData); | ||
} | ||
|
||
return result; | ||
}; | ||
} | ||
|
||
/** | ||
* @template {keyof PrettierStaticProperties} Property | ||
* @param {Property} property | ||
* @param {string} prettierEntry | ||
*/ | ||
function getProperty(property, prettierEntry) { | ||
return /** @type {Prettier} */ (require(prettierEntry))[property]; | ||
} | ||
|
||
/** | ||
* @template {keyof SynchronizedPrettier} ExportName | ||
* @param {() => SynchronizedPrettier[ExportName]} getter | ||
*/ | ||
function createDescriptor(getter) { | ||
let value; | ||
return { | ||
get: () => { | ||
value ??= getter(); | ||
return value; | ||
}, | ||
enumerable: true, | ||
}; | ||
} | ||
|
||
/** | ||
* @param {string | URL} entry | ||
*/ | ||
function toImportId(entry) { | ||
if (entry instanceof URL) { | ||
return entry.href; | ||
} | ||
|
||
if (typeof entry === "string" && path.isAbsolute(entry)) { | ||
return url.pathToFileURL(entry).href; | ||
} | ||
|
||
return entry; | ||
} | ||
|
||
/** | ||
* @param {string | URL} entry | ||
*/ | ||
function toRequireId(entry) { | ||
if (entry instanceof URL || entry.startsWith("file:")) { | ||
return url.fileURLToPath(entry); | ||
} | ||
|
||
return entry; | ||
} | ||
|
||
/** | ||
* @param {object} options | ||
* @param {string | URL} options.prettierEntry - Path or URL to prettier entry. | ||
* @returns {SynchronizedPrettier} | ||
*/ | ||
function createSynchronizedPrettier({ prettierEntry }) { | ||
const importId = toImportId(prettierEntry); | ||
const requireId = toRequireId(prettierEntry); | ||
|
||
const prettier = Object.defineProperties( | ||
Object.create(null), | ||
Object.fromEntries( | ||
[ | ||
...PRETTIER_ASYNC_FUNCTIONS.map((functionName) => { | ||
return /** @type {const} */ ([ | ||
functionName, | ||
() => createSynchronizedFunction(functionName, importId), | ||
]); | ||
}), | ||
...PRETTIER_STATIC_PROPERTIES.map((property) => { | ||
return /** @type {const} */ ([ | ||
property, | ||
() => getProperty(property, requireId), | ||
]); | ||
}), | ||
].map(([property, getter]) => { | ||
return /** @type {const} */ ([property, createDescriptor(getter)]); | ||
}), | ||
), | ||
); | ||
|
||
return prettier; | ||
return makeSynchronizedModule(prettierEntry); | ||
} | ||
|
||
module.exports = createSynchronizedPrettier({ prettierEntry: "prettier" }); | ||
// @ts-expect-error Property 'createSynchronizedPrettier' for named export compatibility | ||
module.exports.createSynchronizedPrettier = createSynchronizedPrettier; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also add the following?