refactor: uniform namespace for task function(s)
[poolifier.git] / src / worker / task-functions.ts
diff --git a/src/worker/task-functions.ts b/src/worker/task-functions.ts
new file mode 100644 (file)
index 0000000..a61d696
--- /dev/null
@@ -0,0 +1,45 @@
+/**
+ * Task synchronous function that can be executed.
+ *
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
+ */
+export type TaskSyncFunction<Data = unknown, Response = unknown> = (
+  data?: Data
+) => Response
+
+/**
+ * Task asynchronous function that can be executed.
+ * This function must return a promise.
+ *
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
+ */
+export type TaskAsyncFunction<Data = unknown, Response = unknown> = (
+  data?: Data
+) => Promise<Response>
+
+/**
+ * Task function that can be executed.
+ * This function can be synchronous or asynchronous.
+ *
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
+ */
+export type TaskFunction<Data = unknown, Response = unknown> =
+  | TaskSyncFunction<Data, Response>
+  | TaskAsyncFunction<Data, Response>
+
+/**
+ * Tasks functions that can be executed.
+ * This object can contain synchronous or asynchronous functions.
+ * The key is the name of the function.
+ * The value is the function itself.
+ *
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
+ */
+export type TaskFunctions<Data = unknown, Response = unknown> = Record<
+string,
+TaskFunction<Data, Response>
+>