Skip to content

feat: 补齐类型声明文件,index.js增加暴露threadPool中的8个类,可简写成const { ThreadPool } =… #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "1.0.1",
"description": "基于nodejs worker_threads的线程池。耗时操作或nodejs没有提供异步模式的api(例如解密、同步的文件api)都可以在线程池中执行,业务代码只需要返回一个Promise或async函数给线程池库,至于业务逻辑做什么操作,其实都可以,比如setTimeout,异步操作,async await等",
"main": "src/index.js",
"module": "src/index.js",
"types": "src/index.d.ts",
"directories": {
"test": "test"
},
Expand Down
4 changes: 4 additions & 0 deletions src/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare const MAX_THREADS = 50;
export declare const MAX_WORK: number;
export declare const CORE_THREADS = 10;
export declare const MAX_IDLE_TIME: number;
18 changes: 18 additions & 0 deletions src/constants.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export declare const DISCARD_POLICY: {
ABORT: number;
CALLER_RUN: number;
OLDEST_DISCARD: number;
DISCARD: number;
NOT_DISCARD: number;
};
export declare const THREAD_STATE: {
IDLE: number;
BUSY: number;
DEAD: number;
};
export declare const WORK_STATE: {
PENDDING: number;
RUNNING: number;
END: number;
CANCELED: number;
};
4 changes: 4 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as constants from './constants';
export * as config from './config';
export * as threadPool from './threadPool';
export { defaultCpuThreadPool, defaultFixedThreadPool, defaultSingleThreadPool, defaultThreadPool, ThreadPool, CPUThreadPool, FixedThreadPool, SingleThreadPool } from './threadPool';
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const threadPool = require('./threadPool');

module.exports = {
constants: require('./constants'),
config: require('./config'),
threadPool: require('./threadPool'),
threadPool,
...threadPool
};
84 changes: 84 additions & 0 deletions src/threadPool.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/// <reference types="node" />
import { Worker } from 'worker_threads';
import { EventEmitter } from 'events';
import { Work } from './work';
interface IPropsOptions {
workId: number;
}
declare class UserWork extends EventEmitter {
workId: number;
timer: any;
state: number;
terminate?: () => void;
constructor({ workId }: IPropsOptions);
setTimeout(timeout: number): void;
clearTimeout(): void;
cancel(): boolean;
setState(state: number): void;
}
interface IThreadOptions {
worker: Worker;
}
declare class Thread {
worker: Worker;
threadId: number;
state: number;
lastWorkTime: number;
constructor({ worker }: IThreadOptions);
setState(state: number): void;
setLastWorkTime(time: number): void;
}
interface IThreadPoolOptions {
coreThreads?: number;
maxThreads?: number;
discardPolicy?: number;
preCreate?: boolean;
timeout?: number;
maxIdleTime?: number;
maxWork?: number;
expansion?: boolean;
}
export declare class ThreadPool {
options: IThreadPoolOptions;
workerQueue: Thread[];
coreThreads: number;
maxThreads: number;
discardPolicy: number;
preCreate: boolean;
maxIdleTime: number;
workPool: {
[props: number]: UserWork;
};
workId: number;
queue: Work[];
totalWork: number;
maxWork: number;
timeout: number;
constructor(options?: IThreadPoolOptions);
pollIdle(): void;
preCreateThreads(): void;
newThread(): Thread;
selectThead(): Thread;
generateWorkId(): number;
submit(filename: string, options?: {
[props: string]: any;
}): Promise<UserWork>;
submitWorkToThread(thread: Thread, work: Work): void;
addWork(userWork: UserWork): void;
endWork(userWork: UserWork): void;
cancelWork(userWork: UserWork): void;
}
export declare class CPUThreadPool extends ThreadPool {
constructor(options?: IThreadPoolOptions);
}
export declare class SingleThreadPool extends ThreadPool {
constructor(options?: IThreadPoolOptions);
}
export declare class FixedThreadPool extends ThreadPool {
constructor(options?: IThreadPoolOptions);
}
export declare const defaultThreadPool: ThreadPool;
export declare const defaultCpuThreadPool: CPUThreadPool;
export declare const defaultFixedThreadPool: FixedThreadPool;
export declare const defaultSingleThreadPool: SingleThreadPool;
export {};
3 changes: 3 additions & 0 deletions src/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export declare function isFunction(func: unknown): boolean;
export declare function isJSFile(file: string): boolean;
export declare function isMJSFile(file: string): boolean;
16 changes: 16 additions & 0 deletions src/work.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface IPropsOption {
workId: number;
filename: string;
options: {
[props: string]: any;
};
}
export declare class Work {
workId: number;
filename: string;
data: any;
error: any;
options: any;
constructor({ workId, filename, options }: IPropsOption);
}
export {};
1 change: 1 addition & 0 deletions src/worker.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};