@makano/rew 1.1.1 → 1.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/rew/pkgs/modules/threads/worker.js +37 -0
- package/lib/rew/pkgs/threads.js +67 -0
- package/package.json +2 -6
package/README.md
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
const { parentPort, workerData } = require('worker_threads');
|
2
|
+
const { exec } = require('../../../modules/runtime');
|
3
|
+
|
4
|
+
const target = {};
|
5
|
+
target.events = [];
|
6
|
+
target.on = (e, cb) => {
|
7
|
+
target.events.push({ e, cb });
|
8
|
+
}
|
9
|
+
target.off = (e) => {
|
10
|
+
target.events = target.events.filter(i => i.e !== e);
|
11
|
+
}
|
12
|
+
target.emit = (e, data) => {
|
13
|
+
target.events.filter(i => i.e == e).forEach(i => i.cb(data));
|
14
|
+
}
|
15
|
+
|
16
|
+
parentPort.on('message', (data) => {
|
17
|
+
if (data.type === 'event') {
|
18
|
+
target.emit(data.event, data.data);
|
19
|
+
} else if (data.type === 'start') {
|
20
|
+
(async () => {
|
21
|
+
try {
|
22
|
+
exec(`(${workerData.cb}).call({ process }, context)`, { print: (...a) => console.log(...a), process: {
|
23
|
+
on: (e, cb) => { target.on(e, cb) },
|
24
|
+
off: (e) => { target.off(e) },
|
25
|
+
emit: (e) => { parentPort.postMessage({ type: 'event', event: e, data }) },
|
26
|
+
exit: (code) => process.exit(code),
|
27
|
+
finish: (data) => {
|
28
|
+
parentPort.postMessage({ type: 'result', result: data });
|
29
|
+
process.exit(0)
|
30
|
+
}
|
31
|
+
}, context: workerData.context })
|
32
|
+
} catch (e) {
|
33
|
+
parentPort.postMessage({ type: 'error', error: e.message });
|
34
|
+
}
|
35
|
+
})();
|
36
|
+
}
|
37
|
+
});
|
@@ -0,0 +1,67 @@
|
|
1
|
+
const { Worker } = require('worker_threads');
|
2
|
+
const emitter = require('../functions/emitter');
|
3
|
+
const future = require('../functions/future');
|
4
|
+
const { struct } = require('../models/struct');
|
5
|
+
const path = require('path');
|
6
|
+
|
7
|
+
module.exports = (context) => ({
|
8
|
+
thread: (cb) => {
|
9
|
+
const workers = [];
|
10
|
+
|
11
|
+
const stopWorker = (worker) => {
|
12
|
+
workers.splice(workers.indexOf(worker), 1);
|
13
|
+
worker.terminate()
|
14
|
+
};
|
15
|
+
|
16
|
+
const athread = {
|
17
|
+
stopAll: () => {
|
18
|
+
if (!run) return;
|
19
|
+
workers.forEach(stopWorker);
|
20
|
+
},
|
21
|
+
start: (context) => {
|
22
|
+
let dataResult = future(() => {}), listener = emitter();
|
23
|
+
const worker = new Worker(path.resolve(__dirname, './modules/threads/worker.js'), {
|
24
|
+
workerData: { context, cb: cb.toString() }
|
25
|
+
});
|
26
|
+
workers.push(worker);
|
27
|
+
|
28
|
+
const stop = () => stopWorker(worker);
|
29
|
+
|
30
|
+
worker.on('message', (data) => {
|
31
|
+
if (data.type === 'result') {
|
32
|
+
dataResult.resolve(data.result);
|
33
|
+
stop();
|
34
|
+
} else if (data.type === 'error') {
|
35
|
+
reject(new Error(data.error));
|
36
|
+
stop();
|
37
|
+
} else if (data.type === 'event') {
|
38
|
+
listener.emit(data.event, data.data);
|
39
|
+
}
|
40
|
+
});
|
41
|
+
|
42
|
+
worker.on('error', (error) => {
|
43
|
+
console.log(error);
|
44
|
+
stop();
|
45
|
+
});
|
46
|
+
|
47
|
+
worker.on('exit', (code) => {
|
48
|
+
stop();
|
49
|
+
if (code !== 0) {
|
50
|
+
throw new Error(`Worker stopped with exit code ${code}`);
|
51
|
+
}
|
52
|
+
});
|
53
|
+
|
54
|
+
worker.postMessage({ type: 'start' });
|
55
|
+
|
56
|
+
return {
|
57
|
+
on: (e, cb) => listener.on(e, cb),
|
58
|
+
off: (e, cb) => listener.off(e, cb),
|
59
|
+
emit: (e, data) => worker?.postMessage({ type: 'event', event: e, data }),
|
60
|
+
get: () => dataResult.wait()
|
61
|
+
};
|
62
|
+
}
|
63
|
+
};
|
64
|
+
|
65
|
+
return athread;
|
66
|
+
}
|
67
|
+
});
|
package/package.json
CHANGED
@@ -1,16 +1,13 @@
|
|
1
1
|
{
|
2
2
|
"name": "@makano/rew",
|
3
|
-
"version": "1.1.
|
3
|
+
"version": "1.1.12",
|
4
4
|
"description": "A simple coffescript runtime",
|
5
5
|
"main": "lib/rew/main.js",
|
6
6
|
"directories": {
|
7
7
|
"lib": "lib"
|
8
8
|
},
|
9
9
|
"scripts": {
|
10
|
-
"test": "node test/test.js"
|
11
|
-
"docs:dev": "vitepress dev docs",
|
12
|
-
"docs:build": "vitepress build docs",
|
13
|
-
"docs:preview": "vitepress preview docs"
|
10
|
+
"test": "node test/test.js"
|
14
11
|
},
|
15
12
|
"files": [
|
16
13
|
"lib/",
|
@@ -32,7 +29,6 @@
|
|
32
29
|
"chokidar": "^3.6.0",
|
33
30
|
"js-yaml": "^4.1.0",
|
34
31
|
"vm": "^0.1.0",
|
35
|
-
"ws": "^8.17.0",
|
36
32
|
"yargs": "^17.7.2"
|
37
33
|
},
|
38
34
|
"devDependencies": {
|