@d-zero/dealer 1.3.2 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/deal.d.ts CHANGED
@@ -1,14 +1,44 @@
1
1
  import type { DealerOptions } from './dealer.js';
2
2
  import type { LanesOptions } from './lanes.js';
3
+ import type { DelayOptions } from '@d-zero/shared/delay';
3
4
  export type DealOptions = DealerOptions & LanesOptions & {
4
5
  readonly header?: DealHeader;
5
6
  readonly debug?: boolean;
7
+ readonly interval?: number | DelayOptions;
6
8
  };
7
9
  export type DealHeader = (progress: number, done: number, total: number, limit: number) => string;
8
10
  /**
11
+ * Processes items in parallel with coordinated logging and optional interval delays.
9
12
  *
10
- * @param items
11
- * @param setup
12
- * @param options
13
+ * ## Architecture
14
+ *
15
+ * ### Execution Flow
16
+ * 1. `dealer.play()` initiates parallel processing
17
+ * 2. For each worker:
18
+ * - `start()` function is called (item is started)
19
+ * - **Interval delay executes** (if `options.interval` is specified)
20
+ * - Wait log is output via `delay()` callback with `%countdown()` format
21
+ * - This happens **after** the item starts but **before** its first output
22
+ * - Actual processing begins (first `update()` call from user code)
23
+ *
24
+ * ### Interval Delay
25
+ * - Interval delay is executed **inside** the `start()` function, not before it
26
+ * - This ensures the item is started first, then waits before producing output
27
+ * - Wait log is automatically displayed using `%countdown()` format
28
+ * - The delay duration is determined synchronously before the delay starts
29
+ *
30
+ * ### Line Header Management
31
+ * - `setLineHeader()` allows setting a prefix string for all log lines of an item
32
+ * - Once set, all subsequent `update()` calls automatically prepend the line header
33
+ * - This enables consistent formatting across multiple log updates
34
+ *
35
+ * ### Wait Logging
36
+ * - All wait logs (including interval delay) are output via `delay()` callback
37
+ * - This ensures the determined interval (even for random delays) is used for countdown
38
+ * - The `%countdown()` function displays remaining time based on the actual delay duration
39
+ * @param items - Collection of items to process
40
+ * @param setup - Function that initializes each item and returns a start function
41
+ * @param options - Configuration options including interval delay
42
+ * @returns Promise that resolves when all items are processed
13
43
  */
14
- export declare function deal<T extends WeakKey>(items: readonly T[], setup: (process: T, update: (log: string) => void, index: number) => Promise<() => void | Promise<void>> | (() => void | Promise<void>), options?: DealOptions): Promise<void>;
44
+ export declare function deal<T extends WeakKey>(items: readonly T[], setup: (process: T, update: (log: string) => void, index: number, setLineHeader: (lineHeader: string) => void) => Promise<() => void | Promise<void>> | (() => void | Promise<void>), options?: DealOptions): Promise<void>;
package/dist/deal.js CHANGED
@@ -1,11 +1,40 @@
1
+ import { delay } from '@d-zero/shared/delay';
1
2
  import { Dealer } from './dealer.js';
2
3
  import { Lanes } from './lanes.js';
3
4
  const DEBUG_ID = Number.MIN_SAFE_INTEGER;
4
5
  /**
6
+ * Processes items in parallel with coordinated logging and optional interval delays.
5
7
  *
6
- * @param items
7
- * @param setup
8
- * @param options
8
+ * ## Architecture
9
+ *
10
+ * ### Execution Flow
11
+ * 1. `dealer.play()` initiates parallel processing
12
+ * 2. For each worker:
13
+ * - `start()` function is called (item is started)
14
+ * - **Interval delay executes** (if `options.interval` is specified)
15
+ * - Wait log is output via `delay()` callback with `%countdown()` format
16
+ * - This happens **after** the item starts but **before** its first output
17
+ * - Actual processing begins (first `update()` call from user code)
18
+ *
19
+ * ### Interval Delay
20
+ * - Interval delay is executed **inside** the `start()` function, not before it
21
+ * - This ensures the item is started first, then waits before producing output
22
+ * - Wait log is automatically displayed using `%countdown()` format
23
+ * - The delay duration is determined synchronously before the delay starts
24
+ *
25
+ * ### Line Header Management
26
+ * - `setLineHeader()` allows setting a prefix string for all log lines of an item
27
+ * - Once set, all subsequent `update()` calls automatically prepend the line header
28
+ * - This enables consistent formatting across multiple log updates
29
+ *
30
+ * ### Wait Logging
31
+ * - All wait logs (including interval delay) are output via `delay()` callback
32
+ * - This ensures the determined interval (even for random delays) is used for countdown
33
+ * - The `%countdown()` function displays remaining time based on the actual delay duration
34
+ * @param items - Collection of items to process
35
+ * @param setup - Function that initializes each item and returns a start function
36
+ * @param options - Configuration options including interval delay
37
+ * @returns Promise that resolves when all items are processed
9
38
  */
10
39
  export async function deal(items, setup, options) {
11
40
  const dealer = new Dealer(items, options);
@@ -16,9 +45,16 @@ export async function deal(items, setup, options) {
16
45
  });
17
46
  }
18
47
  await dealer.setup(async (process, index) => {
19
- const update = (log) => lanes.update(index, log);
20
- const start = await setup(process, update, index);
48
+ let lineHeader = '';
49
+ const setLineHeader = (header) => {
50
+ lineHeader = header;
51
+ };
52
+ const update = (log) => lanes.update(index, lineHeader + log);
53
+ const start = await setup(process, update, index, setLineHeader);
21
54
  return async () => {
55
+ await delay(options?.interval ?? 0, (determinedInterval) => {
56
+ update(`Waiting interval: %countdown(${determinedInterval},${index}_interval)%ms`);
57
+ });
22
58
  await start();
23
59
  lanes.delete(index);
24
60
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/dealer",
3
- "version": "1.3.2",
3
+ "version": "1.4.1",
4
4
  "description": "A tool that provides an API and CLI for parallel processing of collections and sequential logging to standard output",
5
5
  "author": "D-ZERO",
6
6
  "license": "MIT",
@@ -23,7 +23,8 @@
23
23
  "clean": "tsc --build --clean"
24
24
  },
25
25
  "dependencies": {
26
+ "@d-zero/shared": "0.14.0",
26
27
  "ansi-colors": "4.1.3"
27
28
  },
28
- "gitHead": "1218a023e62c79efeece6350d561f2e1906be7ea"
29
+ "gitHead": "2397932a3e3fe5f0631c0d540bdcac732fc71f19"
29
30
  }