UNPKG

dbd.db

Version:

A Lightweight Schema-Free Object-Oriented LocalDatabase for Development and Production Purpose

63 lines (50 loc) 1.37 kB
const WorkerPool = require('workerpool') class JsonPool { constructor() { this.pool = WorkerPool.pool( __dirname + '/jsonScript.js', { maxWorkers: process.env.MAX_WORKER_THREADS || 3, workerType: 'thread' }) this.terminating = false } parse(data) { return this.pool.exec('parse', [data]).then(res => { if (!this.pool.tasks.length && !this.terminating) { this.terminating = true this.pool.terminate() .then(() => (this.terminating = false)) .catch(() => (this.terminating = false)) } return res }).catch(rej => { if (!this.pool.tasks.length && !this.terminating) { this.terminating = true this.pool.terminate() .then(() => (this.terminating = false)) .catch(() => (this.terminating = false)) } throw rej }) } stringify(data) { return this.pool.exec('stringify', [data]).then(res => { if (!this.pool.tasks.length && !this.terminating) { this.terminating = true this.pool.terminate() .then(() => (this.terminating = false)) .catch(() => (this.terminating = false)) } return res }).catch(rej => { if (!this.pool.tasks.length && !this.terminating) { this.terminating = true this.pool.terminate() .then(() => (this.terminating = false)) .catch(() => (this.terminating = false)) } throw rej }) } } module.exports = JsonPool