Commit 00e8a809 authored by martin hou's avatar martin hou

feat: 增加实例内handler注册机制

parent 68b04970
......@@ -80,11 +80,11 @@ function Jensen(log, conn) {
let sequence = 0;
let current = null;
let commands = [];
let handlers = [];
let statusTimeout = null;
let recv = false;
let ready = false;
let totalBytes = 0;
let handlers = {};
let self = this;
this.data = {};
......@@ -419,7 +419,8 @@ function Jensen(log, conn) {
'recv: ' + cname + ', seq: ' + msg.sequence + ', data bytes: ' + msg.body?.byteLength + ', data: ' + heading.join(' ')
);
try {
let handler = Jensen.handlers[msg.id];
let handler = handlers[msg.id];
if (!handler) handler = Jensen.handlers[msg.id];
let r = handler(msg, self);
if (r) trigger(r, msg.id);
} catch (e) {
......@@ -434,7 +435,8 @@ function Jensen(log, conn) {
// 5是msgid
let msgid = parseInt(current.replace(/^cmd-(\d+)-(\d+)$/gi, '$1'));
try {
let handler = Jensen.handlers[msgid];
let handler = handlers[msgid];
if (!handler) handler = Jensen.handlers[msgid];
handler(null, self);
} catch (e) {
trigger(e);
......@@ -523,6 +525,10 @@ function Jensen(log, conn) {
}
return str;
};
this.registerHandler = function (cmdid, handler) {
handlers[cmdid] = handler;
};
}
function Command(id) {
......@@ -631,7 +637,7 @@ Jensen.prototype.getBluetoothStatus = async function (seconds) {
};
Jensen.prototype.listFiles = async function () {
let tag = 'filelist';
let tag = 'filelist-' + this.serialNumber;
if (this[tag] != null) return null;
let fc = null;
......@@ -645,7 +651,7 @@ Jensen.prototype.listFiles = async function () {
let self = this;
// let tag = 'data_' + new Date().getTime();
this[tag] = [];
Jensen.registerHandler(QUERY_FILE_LIST, (msg, jensen) => {
this.registerHandler(QUERY_FILE_LIST, (msg, jensen) => {
if (msg.body.length == 0) {
jensen[tag] = null;
return [];
......@@ -944,7 +950,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
onprogress?.(recvBytes);
}
}
Jensen.registerHandler(TRANSFER_FILE, handler);
this.registerHandler(TRANSFER_FILE, handler);
this.send(new Command(TRANSFER_FILE).body(fname));
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment