-
branch currently with this issue I am trying to use a worker_thread like so: worker.jsconst path = require('path');
const { workerData } = require('worker_threads');
require('ts-node').register(workerData.registerOptions);
require('tsconfig-paths').register();
require(path.resolve(__dirname, workerData.path)); worker.tsimport { parentPort, workerData } from 'worker_threads'
import { getBlockAtDifficulty } from './utilities'
const { block, difficulty, index, step } = workerData
parentPort!.postMessage(getBlockAtDifficulty(block, difficulty, index, step)) parent.ts/**
* spawns threads to resolve hash at difficulty, publishing result in
* MultiService
* @param block Block to resolve hash of
* @param difficulty Difficulty of hash to resolve
* @returns uuid of multiservice to subscribe to
* usage:
* const multiService = new MultiService()
* const task = getBlockAtDifficultyMultiThreaded(myBlock, someDifficulty)
* multiService.resolve$
* .pipe(first(resolution => resolution.hasOwnProperty(task)))
* .subscribe(resolution => onSuccess(resolution[task]))
*/
export function getBlockAtDifficultyMultiThreaded(block: Partial<Block>, difficulty: number): UUID {
const uuid = Hash.uuid() // our task identifier
const workers: Array<Worker> = []
const threads = os.cpus()
const registerOptions = getRegisterOptions()
threads.forEach((_, index) => {
const args = { block, difficulty, index, step: threads.length }
const worker: Worker = new Worker('./worker.js', {
workerData: {
path: 'src/blockchain/get-block-at-difficulty.worker.ts',
args,
registerOptions
}
})
workers.push(worker)
worker.on('message', (result: Block) => {
MultiService.resolve(new Promise(res => res(result)), uuid)
workers.forEach(w => w.terminate())
})
})
return uuid
}
function getRegisterOptions() {
const registerOptions = require('../../tsconfig.json')
registerOptions.files = true
return registerOptions
} I've almost got it working now! but, my project makes use of a custom transformer ts-transformer-keys. I installed ttypescript compiler to process this transformer, following the instructions on the project page for ts-transformer-keys. I also go it working with ts-jest in the same fashion. To get it working in the worker thread, I need to specify the custom compiler ttypescript. How do I do that? my tsconfig looks like this: {
"compilerOptions": {
"outDir": "dist/bundle",
"baseUrl": "./src",
"paths": {
"@blockchain/*": [ "blockchain/*" ],
"@hyperledger/*": [ "hyperledger/*" ],
"@lib/*": [ "lib/*" ]
},
"plugins": [
{ "transform": "ts-transformer-keys/transformer" }
],
"module": "CommonJS",
"moduleResolution": "node",
"target": "ES2020",
"lib": [ "ESNext" ],
"allowJs": false,
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"alwaysStrict": true,
"declaration": true,
"declarationMap": true,
"emitBOM": false,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"newLine": "LF",
"noEmitOnError": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"pretty": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"stripInternal": true,
"useDefineForClassFields": true
},
"include": [
"src/**/*",
"types/*.d.ts"
],
"sourceRoot": "src"
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Check out our For example:
|
Beta Was this translation helpful? Give feedback.
Check out our
compiler
option which is documented in the README. Like many of our options, it can be specified via your tsconfig.json for convenience.For example: