Add spacer utility (v0.0.1-alpha.6)
diff --git a/demo/src/routes/index.ts b/demo/src/routes/index.ts
index d4d4c6a..d5f0113 100644
--- a/demo/src/routes/index.ts
+++ b/demo/src/routes/index.ts
@@ -5,4 +5,5 @@
export * from "./def";
export * from "./goodbye";
export * from "./hij";
-export * from "./memo";
\ No newline at end of file
+export * from "./memo";
+export * from "./spacer";
\ No newline at end of file
diff --git a/demo/src/routes/spacer.ts b/demo/src/routes/spacer.ts
new file mode 100644
index 0000000..641ac2f
--- /dev/null
+++ b/demo/src/routes/spacer.ts
@@ -0,0 +1,32 @@
+// Yggdrasil
+// Copyright (c) jottocraft 2024
+
+import { YggdrasilSpacer } from "@jottocraft/yggdrasil";
+import { DemoServer } from "../server";
+
+export const spacer = DemoServer.defineRoute("GET", "/spacer", async (ygg) => {
+ const spacer = new YggdrasilSpacer(100);
+
+ const tasks = [];
+ for (let i = 0; i < 10; ++i) {
+ tasks.push((async () => {
+ console.log(i + " is waiting for dispatch...");
+ await spacer.waitForDispatch();
+ console.log(i);
+ })());
+ }
+
+ await Promise.all(tasks);
+
+ await new Promise<void>((resolve, reject) => {
+ setTimeout(() => {
+ resolve();
+ }, 2000);
+ });
+
+ console.log("10 is waiting for dispatch...");
+ await spacer.waitForDispatch();
+ console.log(10);
+
+ return new Response("Done", { status: 200 });
+});
\ No newline at end of file
diff --git a/docs/docs/features/spacer.md b/docs/docs/features/spacer.md
new file mode 100644
index 0000000..3c1e70e
--- /dev/null
+++ b/docs/docs/features/spacer.md
@@ -0,0 +1,32 @@
+---
+sidebar_position: 6
+---
+
+# Spacer
+
+Yggdrasil Server includes a spacer utility to help you apply
+a minimal amount of time spacing between tasks, such as API
+requests your server may make.
+
+## Usage
+
+Simply create a `YggdrasilSpacer` object with your desired spacing,
+then await `waitForDispatch` before doing each task:
+
+```ts title="src/handler.ts"
+// Wait 100ms between each API request
+const spacer = new YggdrasilSpacer(100);
+
+const tasks = [];
+
+for (const item of items) {
+ tasks.push((async () => {
+ await spacer.waitForDispatch();
+ await api.updateItem(item);
+ })());
+}
+
+await Promise.all(tasks);
+
+console.log("Done updating all items (with minimum 100ms spacing between each update)!");
+```
\ No newline at end of file
diff --git a/yggdrasil/package.json b/yggdrasil/package.json
index c6fd21e..2ada862 100644
--- a/yggdrasil/package.json
+++ b/yggdrasil/package.json
@@ -1,7 +1,7 @@
{
"name": "@jottocraft/yggdrasil",
"description": "A simple and flexible TypeScript library for streamlining web request handling.",
- "version": "0.0.1-alpha.5",
+ "version": "0.0.1-alpha.6",
"license": "LGPL-2.1-only",
"author": "jottocraft <[email protected]>",
"main": "lib/index.js",
diff --git a/yggdrasil/src/YggdrasilSpacer.ts b/yggdrasil/src/YggdrasilSpacer.ts
new file mode 100644
index 0000000..6922b07
--- /dev/null
+++ b/yggdrasil/src/YggdrasilSpacer.ts
@@ -0,0 +1,39 @@
+// Yggdrasil
+// Copyright (c) jottocraft 2024
+
+/**
+ * A helper for spacing tasks by some minimum amount of time.
+ */
+export class YggdrasilSpacer {
+ /**
+ * @param spacingMs The minimum number of milliseconds that tasks should be separated by.
+ */
+ constructor(spacingMs: number) {
+ this.#spacingMs = spacingMs;
+ }
+
+ /**
+ * Registers a new task and returns a Promise that resolves when the task should be executed.
+ */
+ waitForDispatch() {
+ return new Promise<void>((resolve, reject) => {
+ const now = new Date().getTime();
+ if (((now - this.#lastDispatch) >= this.#spacingMs) && !this.#tasks.length) {
+ this.#lastDispatch = now;
+ resolve();
+ } else {
+ const thisTaskEndsAt = (this.#tasks.length ? this.#tasks[this.#tasks.length - 1] : this.#lastDispatch) + this.#spacingMs;
+ this.#tasks.push(thisTaskEndsAt);
+ setTimeout(() => {
+ this.#lastDispatch = thisTaskEndsAt;
+ this.#tasks.shift();
+ resolve();
+ }, thisTaskEndsAt - now);
+ }
+ });
+ }
+
+ #tasks: number[] = [];
+ #lastDispatch: number = 0;
+ #spacingMs: number;
+};
\ No newline at end of file
diff --git a/yggdrasil/src/index.ts b/yggdrasil/src/index.ts
index fc84449..f511a3f 100644
--- a/yggdrasil/src/index.ts
+++ b/yggdrasil/src/index.ts
@@ -6,6 +6,7 @@
export * from "~/YggdrasilConduitServer";
export * from "~/YggdrasilRouter";
export * from "~/YggdrasilServer";
+export * from "~/YggdrasilSpacer";
export * from "~/YggdrasilStatus";
export * from "~/util/applyHeaders";