Skip to content

Commit

Permalink
build: type exports (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpwallace22 authored Dec 31, 2023
1 parent ced162f commit a476fb2
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"experimentalDecorators": false,
"allowJs": false,
"isolatedModules": false,
"removeComments": true,

/* Strictness Checks */
"alwaysStrict": true,
Expand Down
4 changes: 2 additions & 2 deletions use-react-workers/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1 align="center">
use-react-workers - Reacts hooks for Web Workers
`use-react-workers` Reacts hooks for Web Workers
</h1>

## 🎨 Features
Expand Down Expand Up @@ -169,7 +169,7 @@ The library is experimental so if you find a **bug** or would like to request a

_How to contribute?_

Read [CONTRIBUTE.md](docs/CONTRIBUTE.md)
Read [CONTRIBUTE.md](CONTRIBUTE.md)

## 📜 License

Expand Down
2 changes: 1 addition & 1 deletion use-react-workers/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "use-react-workers",
"description": "React Hooks for Web Workers & Web Worker utilities",
"version": "0.3.0",
"version": "0.3.1",
"source": "src/index.ts",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
9 changes: 6 additions & 3 deletions use-react-workers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { useWorkerFunc } from './useWorkerFunc';
export { useWorkerState } from './useWorkerState';
export { useWorker } from './useWorker';
// Hooks
export * from './useWorkerFunc';
export * from './useWorkerState';
export * from './useWorker';

// Types
export * from './types';
12 changes: 11 additions & 1 deletion use-react-workers/src/useWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ const defaultOptions = {
transferable: TRANSFERABLE_TYPE.AUTO,
};

export type UseWorker = <T extends (...funcArgs: any[]) => any>(
func: T,
options?: Options
) => {
postMessage: (...funcArgs: Parameters<T>) => void;
onMessage: (callBack: (e: MessageEvent) => void) => void;
terminate: () => void;
status: WorkerStatus;
};

/**
* @param {Function} func the function to run with web worker
* @param {Options} options useWorkerFunc option params
*/
export const useWorker = <T extends (...funcArgs: any[]) => any>(
export const useWorker: UseWorker = <T extends (...funcArgs: any[]) => any>(
func: T,
options: Options = defaultOptions
): typeof workerHook => {
Expand Down
15 changes: 11 additions & 4 deletions use-react-workers/src/useWorkerFunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface Controller {
terminate: () => void;
}

interface Promise {
interface PromiseResponse {
resolve: (result: any | ErrorEvent) => void;
reject: (result: any) => void;
}
Expand All @@ -29,16 +29,23 @@ const defaultOptions = {
transferable: TRANSFERABLE_TYPE.AUTO,
};

const defaultPromise: Promise = {
const defaultPromise: PromiseResponse = {
resolve: () => null,
reject: () => null,
};

export type UseWorkerFunc = <T extends (...funcArgs: any[]) => any>(
func: T,
options?: Options
) => [(...funcArgs: Parameters<T>) => Promise<ReturnType<T>>, Controller];

/**
* @param {Function} func the function to run with web worker
* @param {Options} options useWorkerFunc option params
*/
export const useWorkerFunc = <T extends (...funcArgs: any[]) => any>(
export const useWorkerFunc: UseWorkerFunc = <
T extends (...funcArgs: any[]) => any
>(
func: T,
options: Options = defaultOptions
): [typeof workerHook, Controller] => {
Expand All @@ -51,7 +58,7 @@ export const useWorkerFunc = <T extends (...funcArgs: any[]) => any>(
WorkerStatus.IDLE
);
const worker = useRef<Worker & { _url?: string }>();
const promise = useRef<Promise>(defaultPromise);
const promise = useRef<PromiseResponse>(defaultPromise);
const timeoutId = useRef<NodeJS.Timeout>();

const killWorker = useCallback(() => {
Expand Down
14 changes: 13 additions & 1 deletion use-react-workers/src/useWorkerState.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { useEffect, useState } from 'react';
import { Controller, useWorkerFunc } from './useWorkerFunc';

export type UseWorkerState = <
R extends ReturnType<T>,
T extends (args: any) => any = (args: any) => any
>(
func: T,
defaultState: R
) => [
ReturnType<T> | null,
(...args: Parameters<T>) => Promise<void>,
Controller
];

/**
* Executes a function in [useWorkerFunc](./useWorkerFunc) and retrieves its result in React state.
* @param {T} func - The function to be executed in the web worker.
* @param {ReturnType<T>} defaultState - The arguments to be passed to the function.
* @returns {[ReturnType<T> | null, (input: Parameters<T>) => Promise<void>, Controller]} - An array containing the result of the function and a controller object.
*/
export const useWorkerState = <
export const useWorkerState: UseWorkerState = <
R extends ReturnType<T>,
T extends (args: any) => any = (args: any) => any
>(
Expand Down
1 change: 0 additions & 1 deletion use-react-workers/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export default defineConfig(async () => ({
plugins: [
react(),
dts({
rollupTypes: true,
outDir: OUT_DIR,
}),
],
Expand Down

0 comments on commit a476fb2

Please sign in to comment.