Skip to content

Commit

Permalink
Panel search: abort previous requests
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Apr 28, 2024
1 parent 065b695 commit cb8223b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
30 changes: 3 additions & 27 deletions panel/src/panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Notification from "./notification.js";
import Language from "./language.js";
import Plugins from "./plugins.js";
import Menu from "./menu.js";
import Search from "./search.js";
import System from "./system.js";
import Translation from "./translation.js";
import { buildUrl, isUrl } from "@/helpers/url.js";
Expand Down Expand Up @@ -72,6 +73,7 @@ export default {
this.activation = Activation(this);
this.drag = Drag(this);
this.events = Events(this);
this.searcher = Search(this);
this.upload = Upload(this);

// state objects
Expand Down Expand Up @@ -284,34 +286,8 @@ export default {
});
},

/**
* Use one of the installed search types
* to search for content in the panel
*
* @param {String} type
* @param {Object} query
* @param {Object} options { limit, page }
* @returns {Object} { code, path, referrer, results, timestamp }
*/
async search(type, query, options) {
// open the search dialog
if (!query) {
// close menu on mobile
this.menu.escape();

return this.dialog.open({
component: "k-search-dialog",
props: {
type: type
}
});
}

const { $search } = await this.get(`/search/${type}`, {
query: { query, ...options }
});

return $search;
return this.searcher.search(type, query, options);
},

/**
Expand Down
44 changes: 44 additions & 0 deletions panel/src/panel/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export default (panel) => {
return {
controller: null,

open(type) {
// close menu on mobile
panel.menu.escape();

panel.dialog.open({
component: "k-search-dialog",
props: {
type
}
});
},

/**
* Use one of the installed search types
* to search for content in the panel
*
* @param {String} type
* @param {Object} query
* @param {Object} options { limit, page }
* @returns {Object} { code, path, referrer, results, timestamp }
*/
async search(type, query, options) {
// open the search dialog
if (!query) {
return this.open(type);
}

// abort previous search requests
this.controller?.abort();
this.controller = new AbortController();

const { $search } = await panel.get(`/search/${type}`, {
query: { query, ...options },
signal: this.controller.signal
});

return $search;
}
};
};

0 comments on commit cb8223b

Please sign in to comment.