2.0.45 • Published 10 months ago

wormise v2.0.45

Weekly downloads
-
License
GNU
Repository
github
Last release
10 months ago

Wormise

npm.io npm.io npm.io npm.io npm.io npm.io npm.io

wormise npm.io

RU

Эта Node.js библиотека позволяет выполнять функцию в новом потоке и получать доступ к результатам вычислений через Promise.

Благодаря wormise вы можете получить удобный интерфейс-обертку для работы с вычислениями в новом потоке.

Описание

wormise(executedFunction, dir, params): Promise

dir - папка в которой выполняется вызов wormise

executedFunction - функция, выполняемая в отдельном потоке.

params - параметры для executedFunction

Поддерживает ESM (wormise/esm) и CJS (wormise/cjs).

EN

This Node.js library allows you to execute a function in a new thread and access the results of the calculation through Promise.

With wormise, you can get a convenient wrapper interface to work with computations in a new thread.

Supports ESM (wormise/esm) and CJS (wormise/cjs).

Description

wormise(executedFunction, dir, params): Promise

dir - the folder where the wormise call is made.

executedFunction - function executed in a separate thread.

params - arguments for executedFunction

Usage example

Codesandbox example link

Without imports

import wormise, { wormiseDafaultDirname } from 'wormise/esm';
const dir = wormiseDafaultDirname(import.meta.url);
async function getCalculationsResult() {
  try {
    const result = await wormise(
      params => {
        // Complicated calculations
        return new Date(params);
      },
      dir,
      Date.now(),
    );
    console.log(result);
  } catch (error) {
    console.error(error);
  }
}
getCalculationsResult();

With imports

import wormise, { wormiseDafaultDirname } from 'wormise/esm';
const dir = wormiseDafaultDirname(import.meta.url);
import { threadId } from 'worker_threads';
console.log({ threadId });
const data = wormise(
  async () => {
    const logWormiseThreadId = async () => {
      const { threadId } = await import('worker_threads');
      console.log({ threadId });
    };
    await logWormiseThreadId();
  },
  dir,
  undefined,
);

// Output:
// { threadId: 0 }
// { threadId: 1 }

Example tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": true,
    "skipLibCheck": true,
    "moduleResolution": "NodeNext",
    "noEmitHelpers": true,
    "outDir": "dist"
  }
}

Using in CommonJS

Just import from wormise/cjs like this:

import wormise, { wormiseDafaultDirname } from 'wormise/cjs';
async function getCalculationsResult() {
  try {
    const result = await wormise(
      params => {
        // Complicated calculations
        return new Date(params);
      },
      __dirname,
      Date.now(),
    );
    console.log(result);
  } catch (error) {
    console.error(error);
  }
}
getCalculationsResult();
2.0.45

10 months ago

2.0.44

10 months ago

2.0.43

10 months ago

2.0.42

10 months ago

2.0.41

10 months ago

2.0.40

10 months ago

2.0.39

10 months ago

2.0.38

10 months ago

2.0.36

10 months ago

2.0.35

10 months ago

2.0.34

10 months ago

2.0.33

10 months ago

2.0.32

10 months ago

2.0.31

10 months ago

2.0.28

10 months ago

2.0.27

10 months ago

2.0.26

11 months ago

2.0.25

11 months ago

2.0.24

11 months ago

2.0.23

11 months ago

2.0.22

11 months ago

2.0.21

11 months ago

2.0.20

11 months ago

2.0.19

11 months ago

2.0.18

11 months ago

2.0.17

11 months ago

2.0.16

11 months ago

2.0.14

11 months ago

2.0.13

11 months ago

2.0.12

11 months ago

2.0.11

11 months ago

2.0.10

11 months ago

2.0.9

11 months ago

2.0.8

11 months ago

2.0.7

11 months ago

2.0.5

11 months ago

2.0.4

11 months ago

2.0.3

11 months ago

2.0.2

11 months ago

2.0.1

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago