Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit 0b2926f

Browse files
authored
Merge pull request #15 from Idicious/typescript-definitions
2 parents dd8d336 + f55b793 commit 0b2926f

File tree

5 files changed

+421
-2
lines changed

5 files changed

+421
-2
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
22
"name": "webworker-promise",
3-
"version": "0.4.3",
3+
"version": "0.5.0",
44
"description": "Promise for webworkers",
55
"module": "src/index.js",
66
"main": "lib/index.js",
7+
"types": "lib/index.d.ts",
78
"scripts": {
8-
"prepublish": "npm run build; npm run dist",
9+
"prepublish": "npm run build; npm run dist; npm run copy-types",
910
"dist": "webpack",
1011
"build": "babel src --out-dir lib",
1112
"watch": "babel src --out-dir lib --watch",
1213
"test": "mocha ./test/*.test.js",
14+
"copy-types": "cp src/*.d.ts lib",
1315
"coverage": "istanbul cover _mocha test/*.test.js"
1416
},
1517
"author": {

src/index.d.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import NodeWorker = require("./node-worker");
2+
3+
export = PromiseWorker;
4+
5+
declare class PromiseWorker {
6+
constructor(worker: Worker | NodeWorker);
7+
8+
/**
9+
* Trigger the main worker function.
10+
* The `onEvent` function is scoped to the 'emit' passed into the main function and is not triggered by global emits from the worker.
11+
*
12+
* @param message Data to send to worker
13+
* @param transferableList List of transferables to send to worker
14+
* @param onEvent Eventhandler for `emit` calls from the worker
15+
*
16+
* @example
17+
*
18+
* ```
19+
* // worker.js
20+
* registerWebworker((message) => {
21+
* return `Hello ${message}!`;
22+
* });
23+
*
24+
* // main.js
25+
* worker.postMessage('world').then((response) => {
26+
* console.log(response); // Hello world!
27+
* })
28+
* ```
29+
*/
30+
postMessage(
31+
message: any,
32+
transferableList?: Transferable[],
33+
onEvent?: (eventName: string, message: any) => void
34+
): Promise<any>;
35+
36+
/**
37+
* Trigger an operation that is registered via the `operation` function in the worker.
38+
* The 'onEvent' function is scoped to the 'emit' passed into the `operation` function and is not triggered by global emits from the worker.
39+
*
40+
* @param operationName Name of the operation
41+
* @param message Data to send to worker
42+
* @param transferableList List of transferables to send to worker
43+
* @param onEvent Eventhandler for `emit` calls from the worker
44+
*
45+
* @example
46+
*
47+
* ```
48+
* // worker.js
49+
* registerWebworker().operation('greet', (message) => {
50+
* return `Hello ${message}!`;
51+
* });
52+
*
53+
* // main.js
54+
* worker.exec('greet', 'world').then((response) => {
55+
* console.log(response); // Hello world!
56+
* })
57+
* ```
58+
*/
59+
exec(
60+
operationName: string,
61+
message?: any,
62+
transferableList?: Transferable[],
63+
onEvent?: (eventName: string, message: any) => void
64+
): Promise<any>;
65+
/**
66+
* Register an event handler that can be triggered via the `emit` command from the worker.
67+
*
68+
* @param eventName Name of the operation
69+
* @param handler Event handler
70+
*
71+
* @example
72+
*
73+
* ```
74+
* // worker.js
75+
* const host = registerWebworker()
76+
* .on('add', (x, y) => {
77+
* host.emit('add:result', x + y);
78+
* });
79+
*
80+
* // main.js
81+
* worker.on('add:result', (result) => {
82+
* console.log(result); // 3
83+
* });
84+
*
85+
* worker.emit('add', 1, 2);
86+
* ```
87+
*/
88+
on(eventName: string, handler: (...args: any[]) => void): PromiseWorker;
89+
/**
90+
* Register an event handler that can be triggered via the `emit` command from the worker, will only be triggered once.
91+
*
92+
* @param eventName Name of the operation
93+
* @param handler Event handler
94+
*
95+
* @example
96+
*
97+
* ```
98+
* // worker.js
99+
* const host = registerWebworker()
100+
* .once('add', (x, y) => {
101+
* host.emit('add:result', x + y);
102+
* });
103+
*
104+
* // main.js
105+
* worker.once('add:result', (result) => {
106+
* console.log(result); // 3
107+
* });
108+
*
109+
* // Will only have effect first call
110+
* worker.emit('add', 1, 2);
111+
* ```
112+
*/
113+
once(eventName: string, handler: (...args: any[]) => void): PromiseWorker;
114+
/**
115+
* Emit an event to the worker that can be listened to via the `on` and `once` methods in the worker.
116+
*
117+
* @param eventName Name of the operation
118+
* @param args Values to emit
119+
*
120+
* @example
121+
*
122+
* ```
123+
* // worker.js
124+
* const host = registerWebworker()
125+
* .on('add', (x, y) => {
126+
* host.emit('add:result', x + y);
127+
* });
128+
*
129+
* // main.js
130+
* worker.on('add:result', (result) => {
131+
* console.log(result); // 3
132+
* });
133+
*
134+
* worker.emit('add', 1, 2);
135+
* ```
136+
*/
137+
emit(eventName: string, ...args: any[]): void;
138+
/**
139+
* Disposes the underlying worker, future calls will have no effect
140+
*/
141+
terminate(): void;
142+
}

src/node-worker.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export = NodeWorker;
2+
3+
/**
4+
* You can use webworker-promise with nodejs using shim
5+
*
6+
* @example
7+
*
8+
* ```
9+
* const WebworkerPromise = require('webworker-promise');
10+
* const Worker = require('webworker-promise/lib/node-worker');
11+
* const path = require('path');
12+
*
13+
* const workerPath = path.join(__dirname, './worker');
14+
* const worker = new WebworkerPromise(new Worker(workerPath));
15+
* ```
16+
*
17+
*/
18+
declare class NodeWorker {
19+
constructor(filePath: string);
20+
}

src/pool.d.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import NodeWorker = require("./node-worker");
2+
3+
export = WorkerPool;
4+
5+
/**
6+
* Dynamic pool for workers.
7+
*
8+
* Note: It's experimental feature, and api may be changed
9+
*
10+
* @example
11+
*
12+
* ```
13+
* const pool = WorkerPool.create({
14+
* src: './test.worker.js',
15+
* // or
16+
* create: () => new Worker('./test.worker.js'),
17+
* maxThreads: 3, // optional, default is 2, max numbers of workers to create if necessary
18+
* maxConcurrentPerWorker: 1, // optional, default is 1
19+
* });
20+
*
21+
* pool.postMessage('hello').then(() => {
22+
* console.log('result');
23+
* });
24+
* ```
25+
*/
26+
declare class WorkerPool {
27+
static create(
28+
workerPoolOptions:
29+
| WorkerPool.WorkerPoolSrcOptions
30+
| WorkerPool.WorkerPoolCreateOptions
31+
): WorkerPool;
32+
/**
33+
* Trigger the main worker function.
34+
* The 'onEvent' function is scoped to the 'emit' passed into the main function and is not triggered by global emits from the worker.
35+
*
36+
* @param message Data to send to worker
37+
* @param transferableList List of transferables to send to worker
38+
* @param onEvent Eventhandler for `emit` calls from the worker
39+
*
40+
* @example
41+
*
42+
* ```
43+
* // worker.js
44+
* registerWebworker((message) => {
45+
* return `Hello ${message}!`;
46+
* });
47+
*
48+
* // main.js
49+
* worker.postMessage('world').then((response) => {
50+
* console.log(response); // Hello world!
51+
* })
52+
* ```
53+
*/
54+
postMessage(
55+
message: any,
56+
transferableList?: Transferable[],
57+
onEvent?: (eventName: string, message: any) => void
58+
): Promise<any>;
59+
60+
/**
61+
* Trigger an operation that is registered via the `operation` function in the worker.
62+
* The 'onEvent' function is scoped to the 'emit' passed into the `operation` function and is not triggered by global emits from the worker.
63+
*
64+
* @param operationName Name of the operation
65+
* @param message Data to send to worker
66+
* @param transferableList List of transferables to send to worker
67+
* @param onEvent Eventhandler for `emit` calls from the worker
68+
*
69+
* @example
70+
*
71+
* ```
72+
* // worker.js
73+
* registerWebworker().operation('greet', (message) => {
74+
* return `Hello ${message}!`;
75+
* });
76+
*
77+
* // main.js
78+
* worker.exec('greet', 'world').then((response) => {
79+
* console.log(response); // Hello world!
80+
* })
81+
* ```
82+
*/
83+
exec(
84+
operationName: string,
85+
message?: any,
86+
transferableList?: Transferable[],
87+
onEvent?: (eventName: string, message: any) => void
88+
): Promise<any>;
89+
}
90+
91+
declare namespace WorkerPool {
92+
interface WorkerPoolSharedOptions {
93+
maxThreads?: number;
94+
maxConcurrentPerWorker?: number;
95+
}
96+
97+
interface WorkerPoolSrcOptions extends WorkerPoolSharedOptions {
98+
src: string;
99+
}
100+
101+
interface WorkerPoolCreateOptions extends WorkerPoolSharedOptions {
102+
create: () => Worker | NodeWorker;
103+
}
104+
}

0 commit comments

Comments
 (0)