-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvgWorker.js
48 lines (39 loc) · 1.38 KB
/
svgWorker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* eslint-disable no-await-in-loop */
const { Project } = require('paper-jsdom');
const Debug = require('debug');
const debug = Debug('svgworker');
// Function for computing the preview of a paper.js board given JSON for a board
function computePreview(boardJSON) {
// Import the given board
const board = new Project();
board.importJSON(boardJSON);
// return the generated preview as a string
return board.exportSVG({ asString: true, bounds: 'content' });
}
// Wait for the board queue and generate previews when requested
async function waitForQueue(boardQueue) {
// Wait 0.5 seconds in between checking so we don't pin the CPU
while (boardQueue.length <= 0) {
await new Promise(resolve => setTimeout(resolve, 500));
}
return boardQueue.shift();
}
async function waitLoop(boardQueue) {
for (;;) {
// Wait until something's on the queue
const result = await waitForQueue(boardQueue);
const { boardId, boardJSON } = result;
// Generate the preview and send it back to the parent
debug(`generating preview for ${boardId}`);
const preview = computePreview(boardJSON);
debug(`generated preview for ${boardId}`);
process.send({ boardId, preview });
}
}
// Initialize an empty board queue
const boardQueue = [];
// Event for receiving a message from the parent process
process.on('message', (data) => {
boardQueue.push(data);
});
waitLoop(boardQueue);