aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorVendicated <vendicated@riseup.net>2022-11-25 19:25:35 +0100
committerVendicated <vendicated@riseup.net>2022-11-25 19:25:35 +0100
commita85ec594a77557e5f48c4cf1aa33680e0da94654 (patch)
treee88fa38f588fef187a0caec884763fc50da8385d /src/utils
parentc2c6c9fccb3b94340037d2136d647817d0a1c916 (diff)
downloadVencord-a85ec594a77557e5f48c4cf1aa33680e0da94654.tar.gz
Vencord-a85ec594a77557e5f48c4cf1aa33680e0da94654.tar.bz2
Vencord-a85ec594a77557e5f48c4cf1aa33680e0da94654.zip
[skip ci] docs docs docs
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/Queue.ts23
-rw-r--r--src/utils/debounce.ts7
-rw-r--r--src/utils/misc.tsx6
-rw-r--r--src/utils/modal.tsx12
4 files changed, 45 insertions, 3 deletions
diff --git a/src/utils/Queue.ts b/src/utils/Queue.ts
index 6153d40..86eb791 100644
--- a/src/utils/Queue.ts
+++ b/src/utils/Queue.ts
@@ -18,14 +18,18 @@
import { Promisable } from "type-fest";
+/**
+ * A queue that can be used to run tasks consecutively.
+ * Highly recommended for things like fetching data from Discord
+ */
export class Queue {
/**
* @param maxSize The maximum amount of functions that can be queued at once.
- * If the queue is full, the oldest function will be removed.
+ * If the queue is full, the oldest function will be removed.
*/
constructor(public maxSize = Infinity) { }
- queue = [] as Array<() => Promisable<unknown>>;
+ private queue = [] as Array<() => Promisable<unknown>>;
private promise?: Promise<any>;
@@ -34,7 +38,7 @@ export class Queue {
if (func)
this.promise = Promise.resolve()
.then(func)
- .then(() => this.next());
+ .finally(() => this.next());
else
this.promise = undefined;
}
@@ -44,6 +48,11 @@ export class Queue {
this.next();
}
+ /**
+ * Append a task at the end of the queue. This task will be executed after all other tasks
+ * If the queue exceeds the specified maxSize, the first task in queue will be removed.
+ * @param func Task
+ */
push<T>(func: () => Promisable<T>) {
if (this.size >= this.maxSize)
this.queue.shift();
@@ -52,6 +61,11 @@ export class Queue {
this.run();
}
+ /**
+ * Prepend a task at the beginning of the queue. This task will be executed next
+ * If the queue exceeds the specified maxSize, the last task in queue will be removed.
+ * @param func Task
+ */
unshift<T>(func: () => Promisable<T>) {
if (this.size >= this.maxSize)
this.queue.pop();
@@ -60,6 +74,9 @@ export class Queue {
this.run();
}
+ /**
+ * The amount of tasks in the queue
+ */
get size() {
return this.queue.length;
}
diff --git a/src/utils/debounce.ts b/src/utils/debounce.ts
index d9e19de..6e5bba6 100644
--- a/src/utils/debounce.ts
+++ b/src/utils/debounce.ts
@@ -16,6 +16,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+/**
+ * Returns a new function that will call the wrapped function
+ * after the specified delay. If the function is called again
+ * within the delay, the timer will be reset.
+ * @param func The function to wrap
+ * @param delay The delay in milliseconds
+ */
export function debounce<T extends Function>(func: T, delay = 300): T {
let timeout: NodeJS.Timeout;
return function (...args: any[]) {
diff --git a/src/utils/misc.tsx b/src/utils/misc.tsx
index 44fc819..7389cc2 100644
--- a/src/utils/misc.tsx
+++ b/src/utils/misc.tsx
@@ -70,6 +70,9 @@ export function useAwaiter<T>(factory: () => Promise<T>, fallbackValue: T | null
return [state.value, state.error, state.pending, () => setSignal(signal + 1)];
}
+/**
+ * Returns a function that can be used to force rerender react components
+ */
export function useForceUpdater() {
const [, set] = React.useState(0);
return () => set(s => s + 1);
@@ -144,6 +147,9 @@ export function classes(...classes: string[]) {
return classes.join(" ");
}
+/**
+ * Returns a promise that resolves after the specified amount of time
+ */
export function sleep(ms: number): Promise<void> {
return new Promise(r => setTimeout(r, ms));
}
diff --git a/src/utils/modal.tsx b/src/utils/modal.tsx
index 2affbd7..886e325 100644
--- a/src/utils/modal.tsx
+++ b/src/utils/modal.tsx
@@ -76,14 +76,26 @@ const ModalAPI = mapMangledModuleLazy("onCloseRequest:null!=", {
openModalLazy: m => m?.length === 1 && filters.byCode(".apply(this,arguments)")(m),
});
+/**
+ * Wait for the render promise to resolve, then open a modal with it.
+ * This is equivalent to render().then(openModal)
+ * You should use the Modal components exported by this file
+ */
export function openModalLazy(render: () => Promise<RenderFunction>, options?: ModalOptions & { contextKey?: string; }): Promise<string> {
return ModalAPI.openModalLazy(render, options);
}
+/**
+ * Open a Modal with the given render function.
+ * You should use the Modal components exported by this file
+ */
export function openModal(render: RenderFunction, options?: ModalOptions, contextKey?: string): string {
return ModalAPI.openModal(render, options, contextKey);
}
+/**
+ * Close a modal by its key
+ */
export function closeModal(modalKey: string, contextKey?: string): void {
return ModalAPI.closeModal(modalKey, contextKey);
}