Skip to content

Commit

Permalink
Switch to Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Nov 27, 2024
1 parent f611b14 commit 8568e8b
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
32 changes: 16 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-latest
platform: linux
arch: x64
- os: macos-latest
platform: darwin
arch: arm64
- os: windows-latest
platform: win32
arch: x64
- os: ubuntu-latest
platform: linux
arch: x64
- os: macos-latest
platform: darwin
arch: arm64
- os: windows-latest
platform: win32
arch: x64
runs-on: ${{ matrix.os }}
name: ${{ matrix.platform }}-${{ matrix.arch }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime
- run: npm install
- run: npm test
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime
- run: npm install
- run: npm test
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"prettier-config-standard"
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ npm i bare-worker

## Usage

``` js
```js
const Worker = require('bare-worker')

if (Worker.isMainThread) {
const worker = new Worker(__filename)

worker
.on('message', console.log)
.on('exit', (code) => {
console.log('Worker exited with code', code)
})
worker.on('message', console.log).on('exit', (code) => {
console.log('Worker exited with code', code)
})
} else {
Worker.parentPort.postMessage('Hello worker')
}
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const constants = require('./lib/constants')
const { Thread } = Bare

module.exports = exports = class Worker extends MessagePort {
constructor (filename, opts = {}) {
constructor(filename, opts = {}) {
const channel = new Channel({ interfaces: [MessagePort] })

super(channel)
Expand All @@ -24,33 +24,33 @@ module.exports = exports = class Worker extends MessagePort {
this.start()
}

terminate () {
terminate() {
this._terminate()
}

[Symbol.for('bare.inspect')] () {
[Symbol.for('bare.inspect')]() {
return {
__proto__: { constructor: Worker },

detached: this.detached
}
}

async _ononline () {
async _ononline() {
await super._ononline()

this.emit('online')
}

async _onexit (exitCode) {
async _onexit(exitCode) {
await super._onexit()

this._thread.join()

this.emit('exit', exitCode)
}

async _onerror (err) {
async _onerror(err) {
await super._onerror()

this.emit('error', err)
Expand Down
6 changes: 3 additions & 3 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = class WorkerError extends Error {
constructor (msg, code, fn = WorkerError) {
constructor(msg, code, fn = WorkerError) {
super(`${code}: ${msg}`)
this.code = code

Expand All @@ -8,11 +8,11 @@ module.exports = class WorkerError extends Error {
}
}

get name () {
get name() {
return 'WorkerError'
}

static ALREADY_STARTED (msg) {
static ALREADY_STARTED(msg) {
return new WorkerError(msg, 'ALREADY_STARTED', WorkerError.ALREADY_STARTED)
}
}
2 changes: 1 addition & 1 deletion lib/message-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Channel = require('bare-channel')
const MessagePort = require('./message-port')

module.exports = class MessageChannel {
constructor () {
constructor() {
const channel = new Channel({ interfaces: [MessagePort] })

this.port1 = new MessagePort(channel)
Expand Down
70 changes: 38 additions & 32 deletions lib/message-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ const constants = require('./constants')
const errors = require('./errors')

module.exports = exports = class MessagePort extends EventEmitter {
constructor (channel) {
constructor(channel) {
super()

this._state = 0
this._inflight = 0
this._channel = channel
this._port = null

this
.on('newListener', this._onnewlistener)
.on('removeListener', this._onremovelistener)
this.on('newListener', this._onnewlistener).on(
'removeListener',
this._onremovelistener
)
}

get detached () {
get detached() {
return (this._state & constants.state.DETACHED) !== 0
}

start () {
start() {
if (this._state & constants.state.STARTED) return

this._state |= constants.state.STARTED
Expand All @@ -42,35 +43,40 @@ module.exports = exports = class MessagePort extends EventEmitter {
MessagePort._ports.add(this)
}

postMessage (message, transferList) {
this._write({ type: constants.message.MESSAGE, value: message }, { transfer: transferList })
postMessage(message, transferList) {
this._write(
{ type: constants.message.MESSAGE, value: message },
{ transfer: transferList }
)
}

close () {
close() {
this._close()
}

ref () {
ref() {
this._state |= constants.state.REFED
this._ref()
}

unref () {
unref() {
this._state &= ~constants.state.REFED
this._unref()
}

[Symbol.for('bare.inspect')] () {
[Symbol.for('bare.inspect')]() {
return {
__proto__: { constructor: MessagePort },

detached: this.detached
}
}

[Symbol.for('bare.detach')] () {
[Symbol.for('bare.detach')]() {
if (this._state & constants.state.STARTED) {
throw errors.ALREADY_STARTED('Worker has already started receiving messages')
throw errors.ALREADY_STARTED(
'Worker has already started receiving messages'
)
}

this._state |= constants.state.DETACHED
Expand All @@ -82,33 +88,33 @@ module.exports = exports = class MessagePort extends EventEmitter {
return handle
}

static [Symbol.for('bare.attach')] (handle) {
static [Symbol.for('bare.attach')](handle) {
return new MessagePort(Channel.from(handle, { interfaces: [MessagePort] }))
}

_close () {
_close() {
if (this._port === null) this.start()

this._port.close()
}

_ref () {
_ref() {
if (this._port === null) return

if (this._inflight > 0 || (this._state & constants.state.REFED) !== 0) {
this._port.ref()
}
}

_unref () {
_unref() {
if (this._port === null) return

if (this._inflight === 0 && (this._state & constants.state.REFED) === 0) {
this._port.unref()
}
}

async _write (data, opts = {}) {
async _write(data, opts = {}) {
if (this._port === null) this.start()

this._inflight++
Expand All @@ -120,31 +126,31 @@ module.exports = exports = class MessagePort extends EventEmitter {
this._unref()
}

async _online () {
async _online() {
if (this._state & constants.state.ONLINE) return

this._state |= constants.state.ONLINE

await this._write({ type: constants.message.ONLINE })
}

async _exit (exitCode) {
async _exit(exitCode) {
await this._write({ type: constants.message.EXIT, exitCode })
}

async _error (error) {
async _error(error) {
await this._write({ type: constants.message.ERROR, error })
}

async _terminate () {
async _terminate() {
if (this._state & constants.state.TERMINATING) return

this._state |= constants.state.TERMINATING

await this._write({ type: constants.message.TERMINATE })
}

async _read () {
async _read() {
for await (const message of this._port) {
switch (message.type) {
case constants.message.MESSAGE:
Expand All @@ -165,7 +171,7 @@ module.exports = exports = class MessagePort extends EventEmitter {
}
}

_onnewlistener (name) {
_onnewlistener(name) {
if (name !== 'message') return

if (this.listenerCount('message') === 0) {
Expand All @@ -174,37 +180,37 @@ module.exports = exports = class MessagePort extends EventEmitter {
}
}

_onremovelistener (name) {
_onremovelistener(name) {
if (name !== 'message') return

if (this.listenerCount('message') === 0) this.unref()
}

_onclose () {
_onclose() {
this._state |= constants.state.CLOSED

MessagePort._ports.delete(this)

this.emit('close')
}

async _onmessage (message) {
async _onmessage(message) {
this.emit('message', message)
}

async _ononline () {
async _ononline() {
this._state |= constants.state.ONLINE
}

async _onexit () {
async _onexit() {
await this._port.close()

this._state |= constants.state.EXITED
}

async _onerror () {}
async _onerror() {}

async _onterminate () {
async _onterminate() {
Bare.exit()
}

Expand Down
15 changes: 5 additions & 10 deletions lib/worker-thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ const MessagePort = require('./message-port')
const worker = require('..')
const { Thread } = Bare

const {
channel: handle,
filename,
data
} = Thread.self.data
const { channel: handle, filename, data } = Thread.self.data

const channel = Channel.from(handle, { interfaces: [MessagePort] })

Bare
.on('newListener', onnewlistener)
Bare.on('newListener', onnewlistener)
.on('removeListener', onremovelistener)
.on('uncaughtException', onerror)
.on('unhandledRejection', onerror)
Expand All @@ -28,7 +23,7 @@ worker.workerData = data

Module.load(Module.resolve(filename, url.pathToFileURL(os.cwd() + '/')))

function onnewlistener (name, fn) {
function onnewlistener(name, fn) {
if (fn === onremovelistener || fn === onerror) return

switch (name) {
Expand All @@ -38,7 +33,7 @@ function onnewlistener (name, fn) {
}
}

function onremovelistener (name, fn) {
function onremovelistener(name, fn) {
if (fn === onremovelistener || fn === onerror) return

switch (name) {
Expand All @@ -48,7 +43,7 @@ function onremovelistener (name, fn) {
}
}

async function onerror (error) {
async function onerror(error) {
await worker.parentPort._error(error)

Bare.exit(1)
Expand Down
Loading

0 comments on commit 8568e8b

Please sign in to comment.