wilk / microjob

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
https://wilk.github.io/microjob/
MIT License
2.02k stars 47 forks source link

microjob method decorator #15

Closed wilk closed 5 years ago

wilk commented 6 years ago

microjob could export a decorator used on class methods. For instance:

const { thread } = require('microjob')

class Executor {
  @thread(config)
  execute(data) {
    // configured using config
    // heavy cpu load on data
    // capability to use "this"
  }

  @thread(config)
  static compute(data) {
    // configured using config
    // heavy cpu load on data
  }
}

const executor = new Executor()

// both methods are executed in a separated thread
await executor.execute(data)
await Executor.compute(data)

In this way, it should be easier to define "job methods" in a declarative way.

Feasibility test needed before all.

wilk commented 5 years ago

Unfortunately, I cannot implement this feature right now because of passing runtime context issue (https://wilk.github.io/microjob/GUIDE.html#job-context).

I'm closing it but I'll reopen if someone finds a way to pass class instances via message passing.