-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rm: default ignore, ipc throttle, unused
- Loading branch information
Showing
7 changed files
with
37 additions
and
229 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,26 @@ | ||
import { isMainThread, parentPort } from "worker_threads" | ||
import { type SyncMessage } from "../types" | ||
|
||
const postMessageToMainProgressThrottle: Record< | ||
string, | ||
{ | ||
next: number | ||
storedBytes: number | ||
} | ||
> = {} | ||
|
||
// We have to throttle the "progress" events of the "download"/"upload" message type. The SDK sends too many events for the IPC to handle properly. | ||
// It freezes the main process if we don't throttle it. | ||
export function throttlePostMessageToMain(message: SyncMessage, callback: (message: SyncMessage) => void) { | ||
const now = Date.now() | ||
let key = "" | ||
|
||
if (message.type === "transfer" && message.data.type === "progress") { | ||
key = `${message.type}:${message.data.relativePath}:${message.data.localPath}:${message.data.type}` | ||
|
||
if (!postMessageToMainProgressThrottle[key]) { | ||
postMessageToMainProgressThrottle[key] = { | ||
next: 0, | ||
storedBytes: 0 | ||
} | ||
} | ||
|
||
postMessageToMainProgressThrottle[key]!.storedBytes += message.data.bytes | ||
|
||
if (postMessageToMainProgressThrottle[key]!.next > now) { | ||
return | ||
} | ||
|
||
message = { | ||
...message, | ||
data: { | ||
...message.data, | ||
bytes: postMessageToMainProgressThrottle[key]!.storedBytes | ||
} | ||
} | ||
} | ||
|
||
callback(message) | ||
|
||
if (key.length > 0 && postMessageToMainProgressThrottle[key] && message.type === "transfer") { | ||
postMessageToMainProgressThrottle[key]!.storedBytes = 0 | ||
postMessageToMainProgressThrottle[key]!.next = now + 100 | ||
|
||
if ( | ||
message.data.type === "error" || | ||
message.data.type === "queued" || | ||
// TODO: Stopped event (message.data.type === "stopped") | ||
message.data.type === "finished" | ||
) { | ||
delete postMessageToMainProgressThrottle[key] | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Send a message to the main thread if we are running inside a worker thread, otherwise send it to the process interface. | ||
* | ||
* @export | ||
* @param {SyncMessage} message | ||
*/ | ||
export function postMessageToMain(message: SyncMessage): void { | ||
throttlePostMessageToMain(message, throttledMessage => { | ||
if (process.onMessage) { | ||
process.onMessage(message) | ||
if (process.onMessage) { | ||
process.onMessage(message) | ||
|
||
return | ||
} | ||
|
||
if (isMainThread || !parentPort) { | ||
if (process.send) { | ||
process.send(throttledMessage) | ||
} | ||
return | ||
} | ||
|
||
return | ||
if (isMainThread || !parentPort) { | ||
if (process.send) { | ||
process.send(message) | ||
} | ||
|
||
parentPort.postMessage(throttledMessage) | ||
}) | ||
return | ||
} | ||
|
||
parentPort.postMessage(message) | ||
} |
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