metarhia / noroutine

Goroutine analogue for Node.js, spreads I/O-bound routine calls to utilize thread pool (worker_threads) using balancer with event loop utilization. 🌱
https://metarhia.com
MIT License
121 stars 11 forks source link

Pass function as a parameter #17

Open Wansmer opened 3 years ago

Wansmer commented 3 years ago

Is your feature request related to a problem? Please describe.

Now methods of modules can work with with primitives and objects only, bun no with a functions.

Code below throw Error, because postMessage no work with function:

(async () => {
  const res = await module1.method1(x => x ** x);
  console.log({ res });
})();

Describe the solution you'd like

There an idea of implementation with vm: https://github.com/Wansmer/noroutine/pull/1

But there is open questions:

  1. Is a good approach to create new sandbox for every higher order function?
  2. Is need disable modules like os and fs for safety or user must make decision himself? (set flags in options or generate and pass own context, for example);
  3. If 'dangerous' code not expected (from end user), maybe eval in this case will be better and lighter solution instead vm.Script?
vk0novalov commented 2 years ago

Example of alternative implementation: https://github.com/virpool/noroutine/commit/de553bb7287c3c7bec876e6c2dab803cddf3affd

Based on concept from https://github.com/josdejong/workerpool/blob/143b89b6909b07b8cdf72fe5813f96b3e6d38b5c/src/worker.js#L99-L102