test ts
This commit is contained in:
45
src/run-sync.ts
Normal file
45
src/run-sync.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
const main = (fn) => {
|
||||
return fn();
|
||||
};
|
||||
|
||||
const result = main(() => {
|
||||
return 42;
|
||||
});
|
||||
console.log(result); // 42
|
||||
|
||||
const resutl2 = main(async () => {
|
||||
return 'hello';
|
||||
});
|
||||
console.log(resutl2); // Promise { 'hello' }
|
||||
|
||||
const fn = async () => {
|
||||
const result3 = await main(async () => {
|
||||
// return Promise.resolve('world');
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve('world');
|
||||
}, 1000);
|
||||
});
|
||||
});
|
||||
console.log(result3); // world
|
||||
};
|
||||
fn();
|
||||
|
||||
const fn2 = (t) => {
|
||||
if (t === 1) return 'hello';
|
||||
if (t === 2) return Promise.resolve('world');
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve('world');
|
||||
}, 1000);
|
||||
});
|
||||
};
|
||||
|
||||
const rfn2 = async () => {
|
||||
console.log('\\\\\\\\'); // hello
|
||||
console.log('1', fn2(1)); // hello
|
||||
console.log('2', await fn2(2)); // hello
|
||||
console.log('3', await fn2(3)); // Promise { <pending> }
|
||||
console.log('-----------'); // world
|
||||
};
|
||||
rfn2();
|
||||
3
src/test-js/test-2.js
Normal file
3
src/test-js/test-2.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import { red } from './test';
|
||||
|
||||
red();
|
||||
12
src/test-js/test.d.ts
vendored
Normal file
12
src/test-js/test.d.ts
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
declare const a: number;
|
||||
declare const b: number;
|
||||
declare const c: number;
|
||||
|
||||
declare function func(): number;
|
||||
|
||||
declare function red(): number;
|
||||
|
||||
declare function green(): number;
|
||||
|
||||
declare function blue(): number;
|
||||
|
||||
15
src/test-js/test.js
Normal file
15
src/test-js/test.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const a = 1;
|
||||
const b = 2;
|
||||
const c = 3;
|
||||
const func = () => {
|
||||
console.log(a + b + c);
|
||||
return a + b + c;
|
||||
};
|
||||
console.log(a + b + c);
|
||||
|
||||
const bb = func();
|
||||
console.log(bb);
|
||||
|
||||
export const red = () => {
|
||||
console.log('red');
|
||||
};
|
||||
63
src/thread/thread.ts
Normal file
63
src/thread/thread.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Worker, isMainThread, MessageChannel, MessagePort } from 'worker_threads';
|
||||
import path from 'path';
|
||||
|
||||
const __filename = import.meta.url;
|
||||
const __dirname = path.dirname(__filename);
|
||||
const numThreads = 4;
|
||||
const workers: Worker[] = [];
|
||||
console.log('dirname',__dirname);
|
||||
for (let i = 0; i < numThreads; i++) {
|
||||
const worker = new Worker(path.resolve( 'worker.mjs'));
|
||||
workers.push(worker);
|
||||
}
|
||||
|
||||
const ports: MessagePort[] = [];
|
||||
for (let i = 0; i < numThreads + 1; i++) {
|
||||
const { port1, port2 } = new MessageChannel();
|
||||
ports.push(port1);
|
||||
port1.on('message', (message) => console.log('received', message));
|
||||
// You can use port2 to communicate with the worker or another part of your application
|
||||
}
|
||||
|
||||
ports[0].on('message', (data) => {
|
||||
console.log('主线程收到消息:', data);
|
||||
});
|
||||
|
||||
for (let i = 1; i < numThreads + 1; i++) {
|
||||
ports[i].on('message', (data) => {
|
||||
const result = data;
|
||||
console.log(`子线程${i - 1}计算结果: ${result}`);
|
||||
});
|
||||
}
|
||||
|
||||
const total = 1000;
|
||||
const numbersPerThread = Math.ceil(total / numThreads);
|
||||
|
||||
for (let i = 0; i < numThreads; i++) {
|
||||
const start = i * numbersPerThread + 1;
|
||||
const end = (i + 1) * numbersPerThread;
|
||||
workers[i].postMessage({ start, end });
|
||||
}
|
||||
|
||||
if (!isMainThread) {
|
||||
self.onmessage = ({ data }) => {
|
||||
const { start, end } = data;
|
||||
let sum = 0;
|
||||
for (let i = start; i <= end; i++) {
|
||||
sum += i;
|
||||
}
|
||||
ports[0].postMessage(sum);
|
||||
};
|
||||
}
|
||||
|
||||
let totalSum = 0;
|
||||
ports.forEach((port, index) => {
|
||||
if (index === 0) return;
|
||||
port.on('message', (data) => {
|
||||
totalSum += data;
|
||||
});
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
console.log(`所有子线程计算完成,总和为: ${totalSum}`);
|
||||
}, 1000);
|
||||
9
src/thread/worker.js
Normal file
9
src/thread/worker.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const { parentPort } = require('worker_threads');
|
||||
|
||||
parentPort.on('message', ({ start, end }) => {
|
||||
let sum = 0;
|
||||
for (let i = start; i <= end; i++) {
|
||||
sum += i;
|
||||
}
|
||||
parentPort.postMessage(sum);
|
||||
});
|
||||
13
src/vm/vm.ts
Normal file
13
src/vm/vm.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import vm from 'vm';
|
||||
|
||||
// Create a new context
|
||||
const context = vm.createContext({ console });
|
||||
|
||||
// Define a script to run
|
||||
const script = new vm.Script(`
|
||||
// console.log('Hello from the VM context!');
|
||||
c= a+b;
|
||||
`);
|
||||
|
||||
// Run the script in the context
|
||||
script.runInContext(context);
|
||||
30
src/worker/thread.js
Normal file
30
src/worker/thread.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname } from 'path';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
if (isMainThread) {
|
||||
// This code is executed in the main thread and will spawn a new worker.
|
||||
const worker = new Worker(__filename, {
|
||||
workerData: { value: 42 }
|
||||
});
|
||||
|
||||
worker.on('message', (message) => {
|
||||
console.log(`Received message from worker: ${message}`);
|
||||
});
|
||||
|
||||
worker.on('error', (error) => {
|
||||
console.error(`Worker error: ${error}`);
|
||||
});
|
||||
|
||||
worker.on('exit', (code) => {
|
||||
if (code !== 0)
|
||||
console.error(`Worker stopped with exit code ${code}`);
|
||||
});
|
||||
} else {
|
||||
// This code is executed in the worker thread.
|
||||
const { value } = workerData;
|
||||
parentPort?.postMessage(`The answer is ${value}`);
|
||||
}
|
||||
Reference in New Issue
Block a user