mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-01-24 15:26:46 -06:00
8857f0d179
1) String literals should not be duplicated. 2) Prefer using an optional chain expression instead, as it's more concise and easier to read. 3) Expected the Promise rejection reason to be an Error.
52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
/////////////////////////////////////////////////////////////
|
|
//
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
//
|
|
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
|
|
// This software is released under the PostgreSQL Licence
|
|
//
|
|
//////////////////////////////////////////////////////////////
|
|
import { io } from 'socketio';
|
|
import gettext from 'sources/gettext';
|
|
import url_for from 'sources/url_for';
|
|
|
|
export function openSocket(namespace, options) {
|
|
return new Promise((resolve, reject)=>{
|
|
const socketObj = io(namespace, {
|
|
path: `${url_for('pgadmin.root')}/socket.io`,
|
|
pingTimeout: 120000,
|
|
pingInterval: 25000,
|
|
...options,
|
|
});
|
|
|
|
/* Once the object is created, connect event is emitted.
|
|
Backend must implement connect and emit connected on success,
|
|
connect_error on failure.
|
|
*/
|
|
socketObj.on('connected', ()=>{
|
|
resolve(socketObj);
|
|
});
|
|
socketObj.on('connect_error', (err)=>{
|
|
reject(new Error(err));
|
|
});
|
|
socketObj.on('disconnect', (err)=>{
|
|
reject(new Error(err));
|
|
});
|
|
});
|
|
}
|
|
|
|
export function socketApiGet(socket, endpoint, params) {
|
|
return new Promise((resolve, reject)=>{
|
|
socket.emit(endpoint, params);
|
|
socket.on(`${endpoint}_success`, (data)=>{
|
|
resolve(data);
|
|
});
|
|
socket.on(`${endpoint}_failed`, (data)=>{
|
|
reject(new Error(data));
|
|
});
|
|
socket.on('disconnect', ()=>{
|
|
reject(new Error(gettext('Connection to pgAdmin server has been lost')));
|
|
});
|
|
});
|
|
}
|