Comparing version 1.0.0-beta.6 to 1.0.0-beta.7
@@ -1,3 +0,2 @@ | ||
import Observable from "zen-observable"; | ||
import { FunctionThread, ModuleThread, Worker as WorkerType } from "../types/master"; | ||
import { FunctionThread, ModuleThread, StripAsync, Worker as WorkerType } from "../types/master"; | ||
import { WorkerFunction, WorkerModule } from "../types/worker"; | ||
@@ -9,3 +8,2 @@ declare type ArbitraryWorkerInterface = WorkerFunction & WorkerModule<string> & { | ||
declare type ExposedToThreadType<Exposed extends WorkerFunction | WorkerModule<any>> = Exposed extends ArbitraryWorkerInterface ? ArbitraryFunctionOrModuleThread : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>> : Exposed extends WorkerModule<any> ? ModuleThread<Exposed> : never; | ||
declare type StripAsync<Type> = Type extends Promise<infer PromiseBaseType> ? PromiseBaseType : Type extends Observable<infer ObservableBaseType> ? ObservableBaseType : Type; | ||
/** | ||
@@ -12,0 +10,0 @@ * Spawn a new thread. Takes a fresh worker instance, wraps it in a thin |
@@ -30,2 +30,3 @@ "use strict"; | ||
}); | ||
this[$observers] = []; | ||
} | ||
@@ -32,0 +33,0 @@ complete() { |
import Observable from "zen-observable"; | ||
import { ObservablePromise } from "../observable-promise"; | ||
import { $errors, $events, $terminate, $worker } from "../symbols"; | ||
interface ObservableLikeSubscription { | ||
unsubscribe(): any; | ||
} | ||
interface ObservableLike<T> { | ||
subscribe(onNext: (value: T) => any, onError?: (error: any) => any, onComplete?: () => any): ObservableLikeSubscription; | ||
subscribe(listeners: { | ||
next?(value: T): any; | ||
error?(error: any): any; | ||
complete?(): any; | ||
}): ObservableLikeSubscription; | ||
} | ||
export declare type StripAsync<Type> = Type extends Promise<infer PromiseBaseType> ? PromiseBaseType : Type extends ObservableLike<infer ObservableBaseType> ? ObservableBaseType : Type; | ||
export declare type ModuleMethods = { | ||
[methodName: string]: (...args: any) => any; | ||
}; | ||
export declare type ProxyableFunction<Args extends any[], ReturnType> = Args extends [] ? () => ObservablePromise<ReturnType> : (...args: Args) => ObservablePromise<ReturnType>; | ||
export declare type ProxyableFunction<Args extends any[], ReturnType> = Args extends [] ? () => ObservablePromise<StripAsync<ReturnType>> : (...args: Args) => ObservablePromise<StripAsync<ReturnType>>; | ||
export declare type ModuleProxy<Methods extends ModuleMethods> = { | ||
@@ -9,0 +21,0 @@ [method in keyof Methods]: ProxyableFunction<Parameters<Methods[method]>, ReturnType<Methods[method]>>; |
{ | ||
"name": "threads", | ||
"version": "1.0.0-beta.6", | ||
"version": "1.0.0-beta.7", | ||
"description": "Easy to use, yet powerful multi-threading library for node.js, web browsers and Electron", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -13,7 +13,7 @@ <h1 align="center">threads</h1> | ||
* **Speed up** code by parallel processing | ||
* First-class support for **async functions** & **observables** | ||
* Write code once, run it **on all platforms** | ||
* Manage bulk task executions with **thread pools** | ||
* Use **require()** and **import**/**export** in workers | ||
* Works great with **webpack** | ||
* Allows using **require()** and **import**/**export** in workers | ||
@@ -32,2 +32,4 @@ ### Version 0.x | ||
## Platform support | ||
<details> | ||
@@ -145,20 +147,17 @@ <summary>Run on node.js</summary> | ||
async function main() { | ||
const add = await spawn(new Worker("./workers/add")) | ||
const sum = await add(2, 3) | ||
const hashPassword = await spawn(new Worker("./hash")) | ||
const hashed = await hashPassword("Super secret password", "1234") | ||
console.log(`2 + 3 = ${sum}`) | ||
console.log("Hashed password:", hashed) | ||
await Thread.terminate(add) | ||
} | ||
main().catch(console.error) | ||
await Thread.terminate(hashPassword) | ||
``` | ||
```js | ||
// workers/add.js | ||
// workers/hash.js | ||
import sha256 from "js-sha256" | ||
import { expose } from "threads/worker" | ||
expose(function add(a, b) { | ||
return a + b | ||
expose(function hashPassword(password, salt) { | ||
return sha256(password + salt) | ||
}) | ||
@@ -169,5 +168,5 @@ ``` | ||
The return value of `add()` in the master code depends on the `add()` return value in the worker: | ||
The `hashPassword()` function in the master code proxies the call to the `hashPassword()` function in the worker: | ||
If the function returns a promise or an observable then you can just use the return value as such in the master code. If the function returns a primitive value, expect the master function to return a promise resolving to that value. | ||
If the worker function returns a promise or an observable then you can just use the return value as such in the master code. If the function returns a primitive value, expect the master function to return a promise resolving to that value. | ||
@@ -174,0 +173,0 @@ ### expose() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84084
1816
221