Skip to content
Snippets Groups Projects
Verified Commit ec7cde16 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: connect return now a promise

parent 37000689
Branches
Tags
No related merge requests found
...@@ -56,52 +56,31 @@ const connectionStatusCode = { ...@@ -56,52 +56,31 @@ const connectionStatusCode = {
}; };
/** /**
* The RestAPI is a class that enables a REST API server. * @private
* * @this {WebSocketDatasource}
* @externalExample ../../../example/data/storage/restapi.mjs * @throws {Error} No url defined for websocket datasource.
* @license AGPLv3
* @since 3.1.0
* @copyright schukai GmbH
* @memberOf Monster.Data.Datasource
* @summary The LocalStorage class encapsulates the access to data objects.
*/
class WebSocketDatasource extends Datasource {
/**
*
* @param {Object} [options] options contains definitions for the datasource.
*/ */
constructor(options) { function connectServer(resolve, reject) {
super(); const self = this;
if (isString(options)) { let promiseAllredyResolved = false;
options = {url: options}; let connectionTimeout = self.getOption('connection.timeout');
if (!isInteger(connectionTimeout) || connectionTimeout < 100) {
connectionTimeout = 5000;
} }
if (!isObject(options)) options = {}; setTimeout(() => {
this.setOptions(options); if (promiseAllredyResolved) {
this[receiveQueueSymbol] = new Queue(); return;
this[connectionSymbol] = {};
this[connectionSymbol].socket = null;
this[connectionSymbol].reconnectCounter = 0;
this[manualCloseSymbol]=false;
} }
reject(new Error("Connection timeout"));
}, connectionTimeout);
/** let reconnectTimeout = self.getOption('connection.reconnect.timeout');
*
* @returns {Websocketdatasource}
* @throws {Error} No url defined for websocket datasource.
*/
connect() {
const self = this;
let connected = false;
let reconnectTimeout = self.getOption('reconnect.timeout');
if (!isInteger(reconnectTimeout) || reconnectTimeout < 1000) reconnectTimeout = 1000; if (!isInteger(reconnectTimeout) || reconnectTimeout < 1000) reconnectTimeout = 1000;
let reconnectAttempts = self.getOption('reconnect.attempts'); let reconnectAttempts = self.getOption('connection.reconnect.attempts');
if (!isInteger(reconnectAttempts) || reconnectAttempts < 1) reconnectAttempts = 1; if (!isInteger(reconnectAttempts) || reconnectAttempts < 1) reconnectAttempts = 1;
let reconnectEnabled = self.getOption('reconnect.enabled'); let reconnectEnabled = self.getOption('connection.reconnect.enabled');
if (reconnectEnabled !== true) reconnectEnabled = false; if (reconnectEnabled !== true) reconnectEnabled = false;
self[manualCloseSymbol] = false; self[manualCloseSymbol] = false;
...@@ -113,7 +92,10 @@ class WebSocketDatasource extends Datasource { ...@@ -113,7 +92,10 @@ class WebSocketDatasource extends Datasource {
self[connectionSymbol].socket = null; self[connectionSymbol].socket = null;
const url = self.getOption('url'); const url = self.getOption('url');
if (!url) throw new Error('No url defined for websocket datasource.'); if (!url) {
reject('No url defined for websocket datasource.');
return;
}
self[connectionSymbol].socket = new WebSocket(url); self[connectionSymbol].socket = new WebSocket(url);
...@@ -121,12 +103,15 @@ class WebSocketDatasource extends Datasource { ...@@ -121,12 +103,15 @@ class WebSocketDatasource extends Datasource {
self[receiveQueueSymbol].add(event); self[receiveQueueSymbol].add(event);
setTimeout(function () { setTimeout(function () {
self.read(); self.read();
}, 0); }, 1);
}; };
self[connectionSymbol].socket.onopen = function () { self[connectionSymbol].socket.onopen = function () {
connected = true;
self[connectionSymbol].reconnectCounter = 0; self[connectionSymbol].reconnectCounter = 0;
if (typeof resolve === 'function' && !promiseAllredyResolved) {
promiseAllredyResolved = true;
resolve();
}
}; };
self[connectionSymbol].socket.close = function (event) { self[connectionSymbol].socket.close = function (event) {
...@@ -150,13 +135,66 @@ class WebSocketDatasource extends Datasource { ...@@ -150,13 +135,66 @@ class WebSocketDatasource extends Datasource {
setTimeout(() => { setTimeout(() => {
self.connect(); self.connect();
}, reconnectTimeout * this[connectionSymbol].reconnectCounter); }, reconnectTimeout * this[connectionSymbol].reconnectCounter);
} else {
if (typeof reject === 'function' && !promiseAllredyResolved) {
promiseAllredyResolved = true;
reject(error);
}
} }
}; };
} }
/**
* The RestAPI is a class that enables a REST API server.
*
* @externalExample ../../../example/data/storage/restapi.mjs
* @license AGPLv3
* @since 3.1.0
* @copyright schukai GmbH
* @memberOf Monster.Data.Datasource
* @summary The LocalStorage class encapsulates the access to data objects.
*/
class WebSocketDatasource extends Datasource {
/**
*
* @param {Object} [options] options contains definitions for the datasource.
*/
constructor(options) {
super();
if (isString(options)) {
options = {url: options};
}
if (!isObject(options)) options = {};
this.setOptions(options);
this[receiveQueueSymbol] = new Queue();
this[connectionSymbol] = {};
this[connectionSymbol].socket = null;
this[connectionSymbol].reconnectCounter = 0;
this[manualCloseSymbol] = false;
}
/**
*
* @returns {Promise}
*/
connect() {
const self = this;
return new Promise((resolve, reject) => {
connectServer.call(this, resolve, reject);
});
}
/**
* @returns {boolean}
*/
isConnected() { isConnected() {
return this[connectionSymbol].socket && this[connectionSymbol].socket.readyState === 1; return this[connectionSymbol]?.socket?.readyState === 1;
} }
/** /**
...@@ -169,9 +207,11 @@ class WebSocketDatasource extends Datasource { ...@@ -169,9 +207,11 @@ class WebSocketDatasource extends Datasource {
/** /**
* @property {string} url=undefined Defines the resource that you wish to fetch. * @property {string} url=undefined Defines the resource that you wish to fetch.
* @property {Number} reconnect.timeout The timeout in milliseconds for the reconnect. * @property {Object} connection
* @property {Number} reconnect.attempts The maximum number of reconnects. * @property {Object} connection.timeout=5000 Defines the timeout for the connection.
* @property {Bool} reconnect.enabled If the reconnect is enabled. * @property {Number} connection.reconnect.timeout The timeout in milliseconds for the reconnect.
* @property {Number} connection.reconnect.attempts The maximum number of reconnects.
* @property {Bool} connection.reconnect.enabled If the reconnect is enabled.
* @property {Object} write={} Options * @property {Object} write={} Options
* @property {Object} write.mapping the mapping is applied before writing. * @property {Object} write.mapping the mapping is applied before writing.
* @property {String} write.mapping.transformer Transformer to select the appropriate entries * @property {String} write.mapping.transformer Transformer to select the appropriate entries
...@@ -208,10 +248,13 @@ class WebSocketDatasource extends Datasource { ...@@ -208,10 +248,13 @@ class WebSocketDatasource extends Datasource {
callbacks: [] callbacks: []
}, },
}, },
connection: {
timeout: 5000,
reconnect: { reconnect: {
timeout: 1000, timeout: 1000,
attempts: 10, attempts: 1,
enabled: true enabled: false,
}
} }
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment