mirror of
https://github.com/danbulant/Mangades
synced 2026-06-24 09:02:00 +00:00
ratelimit
This commit is contained in:
parent
a9a5779e92
commit
47b22f8f7d
2 changed files with 49 additions and 4 deletions
|
|
@ -1,15 +1,29 @@
|
||||||
<script>
|
<script>
|
||||||
import request from "../util/request";
|
|
||||||
import { params } from '@roxi/routify'
|
import { params } from '@roxi/routify'
|
||||||
|
import request from "../util/request";
|
||||||
|
import ratelimit from "../util/ratelimit";
|
||||||
|
|
||||||
var name = $params.search;
|
var name = $params.search;
|
||||||
$: {
|
$: {
|
||||||
const url = new URL(window.location.toString());
|
const url = new URL(window.location.toString());
|
||||||
url.searchParams.set("search", name);
|
url.searchParams.set("search", name);
|
||||||
history.replaceState(history.state, "", url.toString());
|
history.replaceState(history.state, "", url.toString());
|
||||||
}
|
}
|
||||||
var result = request("manga", "title=" + name + "&contentRating[]=safe&contentRating[]=suggestive");
|
/**
|
||||||
$: result = request("manga", "title=" + name + "&contentRating[]=safe&contentRating[]=suggestive");
|
* Searches for results
|
||||||
result.then(console.log);
|
* @param {string} title
|
||||||
|
* @returns {Promise<object>}
|
||||||
|
*/
|
||||||
|
function search(title) {
|
||||||
|
var query = "";
|
||||||
|
if(title) query += "title=" + encodeURIComponent(name) + "&";
|
||||||
|
query += "contentRating[]=safe&contentRating[]=suggestive";
|
||||||
|
return request("manga", query);
|
||||||
|
}
|
||||||
|
var result = search(name);
|
||||||
|
result.then(console.log);
|
||||||
|
$: result = ratelimit(search, name);
|
||||||
|
$: result.then(console.log);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
|
|
|
||||||
31
src/util/ratelimit.js
Normal file
31
src/util/ratelimit.js
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
const ratelimits = new Map();
|
||||||
|
|
||||||
|
function callback(func) {
|
||||||
|
const params = ratelimits.get(func);
|
||||||
|
func(...params.params).then(params.result.resolve).catch(params.result.reject);
|
||||||
|
ratelimits.delete(func);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {T} func
|
||||||
|
* @param {...Parameters<typeof T>} params
|
||||||
|
* @template T
|
||||||
|
* @returns {Promise<ReturnType<T>>}
|
||||||
|
*/
|
||||||
|
function ratelimit(func, ...params) {
|
||||||
|
const data = ratelimits.get(func) || {
|
||||||
|
timeout: setTimeout(callback, 200, func),
|
||||||
|
params,
|
||||||
|
result: {}
|
||||||
|
};
|
||||||
|
if(!data.promise) data.promise = new Promise((resolve, reject) => {
|
||||||
|
data.result.resolve = resolve;
|
||||||
|
data.result.reject = reject;
|
||||||
|
});
|
||||||
|
|
||||||
|
ratelimits.set(func, data);
|
||||||
|
return data.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ratelimit;
|
||||||
Loading…
Reference in a new issue