@beanstalk/core
Advanced tools
Comparing version 0.3.0 to 0.3.1
/// <reference types="node" /> | ||
import { IPutOptions, IReleaseOptions } from './types'; | ||
export declare class BeanstalkClient { | ||
private _connected; | ||
private _socket; | ||
@@ -19,3 +20,3 @@ private _pendingRequests; | ||
*/ | ||
quit(): Promise<void>; | ||
quit(): void; | ||
/** | ||
@@ -22,0 +23,0 @@ * The "put" command is for any process that wants to insert a job into the queue. |
@@ -13,2 +13,3 @@ "use strict"; | ||
constructor() { | ||
this._connected = false; | ||
this._socket = new net_1.Socket(); | ||
@@ -47,4 +48,22 @@ this._pendingRequests = []; | ||
return new Promise((resolve, reject) => { | ||
const errorHandler = (err) => reject(err); | ||
const timeoutHandler = () => this._socket.destroy(new Error('Connection timeout')); | ||
const connectHandler = () => { | ||
// when connected: remove error/timeout handlers | ||
this._socket.off('error', errorHandler); | ||
this._socket.off('timeout', timeoutHandler); | ||
// set inner state | ||
this._connected = true; | ||
resolve(); | ||
}; | ||
const errorHandler = (err) => { | ||
// when an error occurs: remove connect/timeout handlers | ||
this._socket.off('connect', connectHandler); | ||
this._socket.off('timeout', timeoutHandler); | ||
// set inner state | ||
this._connected = false; | ||
reject(err); | ||
}; | ||
const timeoutHandler = () => { | ||
// when connection times out: trigger error | ||
this._socket.destroy(new Error('Connection timeout')); | ||
}; | ||
if (timeout > 0) { | ||
@@ -54,9 +73,5 @@ this._socket.setTimeout(timeout); | ||
} | ||
this._socket.once('connect', connectHandler); | ||
this._socket.once('error', errorHandler); | ||
this._socket.connect(port, host, () => { | ||
// when connected: remove error/timeout handlers | ||
this._socket.off('timeout', timeoutHandler); | ||
this._socket.off('error', errorHandler); | ||
resolve(); | ||
}); | ||
this._socket.connect(port, host); | ||
}); | ||
@@ -70,12 +85,5 @@ } | ||
quit() { | ||
return new Promise((resolve, reject) => { | ||
this._socket.write(`quit\r\n`, (err) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
this._socket.write(`quit\r\n`); | ||
this._socket.destroy(); | ||
this._connected = false; | ||
} | ||
@@ -816,2 +824,5 @@ /** | ||
async _send(cmd) { | ||
if (!this._connected) { | ||
throw new Error(`Beanstalk client is not connected to a server.`); | ||
} | ||
const emitter = new events_1.EventEmitter(); | ||
@@ -826,3 +837,3 @@ const resultPromise = new Promise((resolve, reject) => { | ||
try { | ||
await new Promise((r, e) => this._socket.write(cmd, (err) => (err ? e(err) : r()))); | ||
this._socket.write(cmd); | ||
} | ||
@@ -829,0 +840,0 @@ catch (err) { |
{ | ||
"name": "@beanstalk/core", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Yet another Beanstalkd client library", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/maxleiko/beanstalk-core", |
@@ -9,2 +9,5 @@ ## @beanstalk/core | ||
> **NOTE** | ||
> While version is lower than `v1.0.0` the API might change slightly | ||
### Installation | ||
@@ -30,3 +33,3 @@ ```sh | ||
await client.quit(); | ||
client.quit(); | ||
} | ||
@@ -61,3 +64,3 @@ | ||
await client.quit(); | ||
client.quit(); | ||
} | ||
@@ -64,0 +67,0 @@ |
79011
2184
69