Added cancel() support to File Search

Bryan Roe committed Nov 12, 2020 at 17:49 UTC 7386a3f8c2948318ca4355876ff0fd2c7bd9585f
1 file changed +7 -3
agents/meshcore.js
+7 -3
@@ -2221,6 +2221,8 @@ function createMeshCore(agent) {
2221 case 'findfile': {
2222 // Search for files
2223 var r = require('file-search').find(cmd.path, cmd.filter);
2224 + if (!r.cancel) { r.cancel = function cancel() { this.child.kill(); }; }
2225 + this._search = r;
2226 r.socket = this;
2227 r.socket.reqid = cmd.reqid; // Search request id. This is used to send responses and cancel the request.
2228 r.socket.path = cmd.path; // Search path
@@ -2229,9 +2231,11 @@ function createMeshCore(agent) {
2231 break;
2232 }
2233 case 'cancelfindfile': {
2232 - // TODO: Cancel a search for files
2233 - // cmd.reqid <-- Cancel this reqid
2234 - sendConsoleText('cancelfindfile: ' + cmd.reqid);
2234 + if (this._search)
2235 + {
2236 + this._search.cancel();
2237 + this._search = null;
2238 + }
2239 }
2240 case 'download': {
2241 // Download a file