| 126 | private taskQueue: Array<QueuedTask<ThreadType, any>> = [] |
| 127 | |
| 128 | constructor( |
| 129 | spawnWorker: () => Promise<ThreadType>, |
| 130 | optionsOrSize?: number | PoolOptions |
| 131 | ) { |
| 132 | const options: PoolOptions = typeof optionsOrSize === "number" |
| 133 | ? { size: optionsOrSize } |
| 134 | : optionsOrSize || {} |
| 135 | |
| 136 | const { size = defaultPoolSize } = options |
| 137 | |
| 138 | this.debug = DebugLogger(`threads:pool:${slugify(options.name || String(nextPoolID++))}`) |
| 139 | this.options = options |
| 140 | this.workers = spawnWorkers(spawnWorker, size) |
| 141 | |
| 142 | this.eventObservable = multicast(Observable.from(this.eventSubject)) |
| 143 | |
| 144 | Promise.all(this.workers.map(worker => worker.init)).then( |
| 145 | () => this.eventSubject.next({ |
| 146 | type: PoolEventType.initialized, |
| 147 | size: this.workers.length |
| 148 | }), |
| 149 | error => { |
| 150 | this.debug("Error while initializing pool worker:", error) |
| 151 | this.eventSubject.error(error) |
| 152 | this.initErrors.push(error) |
| 153 | } |
| 154 | ) |
| 155 | } |
| 156 | |
| 157 | private findIdlingWorker(): WorkerDescriptor<ThreadType> | undefined { |
| 158 | const { concurrency = 1 } = this.options |