mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-03 14:03:57 +00:00
datastore
This commit is contained in:
@@ -8,7 +8,7 @@ function handlePing() {
|
|||||||
lastPing = new Date().getTime();
|
lastPing = new Date().getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleOpen({file }) {
|
function handleOpen({ file }) {
|
||||||
handlePing();
|
handlePing();
|
||||||
datastore = new JsonLinesDatastore(file);
|
datastore = new JsonLinesDatastore(file);
|
||||||
}
|
}
|
||||||
@@ -19,15 +19,16 @@ async function handleRead({ msgid, offset, limit }) {
|
|||||||
process.send({ msgtype: 'response', msgid, rows });
|
process.send({ msgtype: 'response', msgid, rows });
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleNotifyChanged() {
|
async function handleNotify({ msgid }) {
|
||||||
datastore.notifyChanged();
|
await datastore.notifyChanged();
|
||||||
|
process.send({ msgtype: 'notify', msgid });
|
||||||
}
|
}
|
||||||
|
|
||||||
const messageHandlers = {
|
const messageHandlers = {
|
||||||
open: handleOpen,
|
open: handleOpen,
|
||||||
read: handleRead,
|
read: handleRead,
|
||||||
ping: handlePing,
|
ping: handlePing,
|
||||||
notifyChanged: handleNotifyChanged,
|
notify: handleNotify,
|
||||||
};
|
};
|
||||||
|
|
||||||
async function handleMessage({ msgtype, ...other }) {
|
async function handleMessage({ msgtype, ...other }) {
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ class DatastoreProxy {
|
|||||||
this.requests = {};
|
this.requests = {};
|
||||||
this.handle_response = this.handle_response.bind(this);
|
this.handle_response = this.handle_response.bind(this);
|
||||||
this.handle_ping = this.handle_ping.bind(this);
|
this.handle_ping = this.handle_ping.bind(this);
|
||||||
|
this.notifyChangedCallback = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle_response({ msgid, rows }) {
|
|
||||||
handle_response({ msgid, rows }) {
|
handle_response({ msgid, rows }) {
|
||||||
const [resolve, reject] = this.requests[msgid];
|
const [resolve, reject] = this.requests[msgid];
|
||||||
resolve(rows);
|
resolve(rows);
|
||||||
@@ -20,6 +20,11 @@ class DatastoreProxy {
|
|||||||
|
|
||||||
handle_ping() {}
|
handle_ping() {}
|
||||||
|
|
||||||
|
handle_notify({ msgid }) {
|
||||||
|
const [resolve, reject] = this.requests[msgid];
|
||||||
|
resolve();
|
||||||
|
delete this.requests[msgid];
|
||||||
|
}
|
||||||
|
|
||||||
async ensureSubprocess() {
|
async ensureSubprocess() {
|
||||||
if (!this.subprocess) {
|
if (!this.subprocess) {
|
||||||
@@ -49,7 +54,22 @@ class DatastoreProxy {
|
|||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
async notifyChanged() {}
|
async notifyChangedCore() {
|
||||||
|
const msgid = uuidv1();
|
||||||
|
const promise = new Promise((resolve, reject) => {
|
||||||
|
this.requests[msgid] = [resolve, reject];
|
||||||
|
this.subprocess.send({ msgtype: 'notify', msgid });
|
||||||
|
});
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
async notifyChanged(callback) {
|
||||||
|
this.notifyChangedCallback = callback;
|
||||||
|
await this.notifyChangedCore();
|
||||||
|
const call = this.notifyChangedCallback;
|
||||||
|
this.notifyChangedCallback = null;
|
||||||
|
if (call) call();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = DatastoreProxy;
|
module.exports = DatastoreProxy;
|
||||||
|
|||||||
Reference in New Issue
Block a user