Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement workers utility #22

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Common functionality shared throughout Clinic.js.
- [Utils](#utils)
- [`getLoggingPaths(toolName, toolSpecificFiles=[])`](#getloggingpathstoolname-toolspecificfiles)
- [`checkForTranspiledCode(fileName)`](#checkfortranspiledcode-filename)
- [`workers`](#workers)
- [Scripts](#scripts)
- [`buildJs(opts = {})`](#buildjsopts)
- [Usage](#usage)
Expand Down Expand Up @@ -69,6 +70,19 @@ A function that will check file contents for transpiled code. Check also include
Arguments:
- `fileName` - Path to the file being passed in.

### `workers`

An object that provides an information about Node.js worker thread support.

```js
const { workers } = require('@nearform/node-clinic-common')

console.log(workers.isMainThread)
console.log(workers.threadId)
```

If Worker Threads are not supported on the Node.js version used, the `isMainThread` property will be `true`, and the `threadId` property will be `0`.

***

## Scripts
Expand Down Expand Up @@ -408,7 +422,7 @@ style can be customised by defining these CSS vars in your CSS
### contexOverlay
Displays an overlay containig the given `msg` right next to the targetElement or the targetRect

If **targetElement** and **targetRect** are `null||undefined` then the overlay content will be centerd on the page.
If **targetElement** and **targetRect** are `null||undefined` then the overlay content will be centerd on the page.
```js
// const options = {
// msg,
Expand All @@ -426,7 +440,7 @@ If **targetElement** and **targetRect** are `null||undefined` then the overlay c
offset: { y: 10, height: 20 },
targetElement: document.querySelector('.my-cool-element'),
showArrow: true
})
})
```

### Walkthrough
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

const getLoggingPaths = require('./lib/get-logging-paths')
const checkForTranspiledCode = require('./lib/source-check')
const workers = require('./lib/workers')

module.exports = {
getLoggingPaths,
checkForTranspiledCode
checkForTranspiledCode,
workers
}
16 changes: 16 additions & 0 deletions lib/workers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

function maybeWorkerThreads () {
let isMainThread = true
let threadId = 0
try {
const workerThreads = require('worker_threads')
isMainThread = workerThreads.isMainThread
threadId = workerThreads.threadId
} catch (err) {
// Do nothing
}
return { isMainThread, threadId }
}

module.exports = maybeWorkerThreads()
34 changes: 34 additions & 0 deletions test/workers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const test = require('tap').test
const workers = require('../lib/workers.js')

function hasWorkerThreads () {
let hasWorkerThreads = false
try {
hasWorkerThreads = !!require('worker_threads')
} catch (err) {
// Do nothing
}
return hasWorkerThreads
}

test('test worker thread detection', (t) => {
t.strictEqual(workers.isMainThread, true)
t.strictEqual(workers.threadId, 0)

if (hasWorkerThreads()) {
const { Worker } = require('worker_threads')
const worker = new Worker(`
const test = require('tap').test
const workers = require('./lib/workers.js')
const assert = require('assert')
assert.strictEqual(workers.isMainThread, false)
assert.strictEqual(workers.threadId, 1)
`, { eval: true })
worker.on('exit', (code) => {
if (code !== 0) t.fail(`worker test failed with code ${code}`)
t.end()
})
} else {
t.end()
}
})