mongodash 2.5.2 → 2.6.0

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.
@@ -5,60 +5,87 @@ const _debug = require("debug");
5
5
  const reactiveTasks_1 = require("../reactiveTasks");
6
6
  const waitUntil_1 = require("./waitUntil");
7
7
  const debug = _debug('mongodash:testing');
8
- /**
9
- * Waits until the reactive task system is idle.
10
- * "Idle" means:
11
- * 1. No changes are buffered in the Planner.
12
- * 2. No workers are currently executing tasks (active count is 0).
13
- * 3. No tasks in the database are in a pending or processing state.
14
- *
15
- * This enables robust E2E testing by ensuring that all side effects and cascading tasks have finished.
16
- */
17
8
  async function waitUntilReactiveTasksIdle(customOptions = {}) {
18
9
  const options = Object.assign({ timeoutMs: 10000, pollIntervalMs: 50, stabilityDurationMs: 200 }, customOptions);
10
+ const hasWhitelist = customOptions.whitelist && customOptions.whitelist.length > 0;
19
11
  await (0, waitUntil_1.waitUntil)(async () => {
20
- // 1. Check Internal Buffers (Planner)
21
- // Accessing private planner via exposed getter for testing
12
+ // Access scheduler internals
22
13
  const planner = reactiveTasks_1._scheduler.taskPlannerInstance;
14
+ const runner = reactiveTasks_1._scheduler.concurrentRunnerInstance;
15
+ const registry = reactiveTasks_1._scheduler.getRegistry();
16
+ // --- 1. Global Checks (Always run) ---
17
+ // 1. Check Internal Buffers (Planner)
23
18
  if (planner && !planner.isEmpty) {
24
19
  debug('Planner not empty');
25
20
  return false;
26
21
  }
27
22
  // 2. Check Active Workers (Runner)
28
- const runner = reactiveTasks_1._scheduler.concurrentRunnerInstance;
29
23
  if (runner && runner.activeWorkers > 0) {
30
24
  debug(`Active workers: ${runner.activeWorkers}`);
31
25
  return false;
32
26
  }
33
- // 3. Check Database
34
- const registry = reactiveTasks_1._scheduler.getRegistry();
27
+ // --- 2. Check Database ---
35
28
  const entries = registry.getAllEntries();
36
29
  // Optimized check: If any collection has pending work, we are not idle.
37
30
  for (const entry of entries) {
38
- // We count documents that are "active"
39
- // status IN [pending, processing, processing_dirty]
40
- // AND dueAt <= Now (approx) - actually, for "settled" we might want to wait for EVERYTHING?
41
- // Usually we want to wait for anything that is currently actionable.
42
- // If a task is scheduled for tomorrow, we shouldn't wait for it.
43
- // However, the user requirement is "guarantees that all reactive tasks that are currently planned... have been completed".
44
- // "Currently planned" usually implies "executable now".
45
- // But if we ignore future tasks, we are safe.
46
- const horizon = Date.now() + (options.timeoutMs || 0) + (options.stabilityDurationMs || 0) + 100;
47
- // We count documents that are "active"
48
- // ONE OF:
49
- // 1. status IN [processing, processing_dirty] (always active)
50
- // 2. status = pending AND (nextRunAt <= horizon OR nextRunAt IS NULL)
51
- // (NULL nextRunAt usually means "now" or "asap" in some contexts, or "never",
52
- // but for pending it usually means "ready").
53
- const count = await entry.tasksCollection.countDocuments({
31
+ // If whitelisting is active, we only check tasks that match the whitelist
32
+ let whitelistFilter = null;
33
+ if (hasWhitelist) {
34
+ const rules = customOptions.whitelist.filter((rule) => rule.collection === entry.sourceCollection.collectionName);
35
+ if (rules.length === 0) {
36
+ continue;
37
+ }
38
+ const criteria = [];
39
+ let matchAll = false;
40
+ for (const rule of rules) {
41
+ let ruleIds = null;
42
+ if (rule.filter) {
43
+ // If we have a filter, we need to find which docs match it.
44
+ // We can't filter tasks directly by source properties efficiently without joining,
45
+ // so we find the matching source docs first.
46
+ const matchingDocs = (await entry.sourceCollection.find(rule.filter, { projection: { _id: 1 } }).toArray());
47
+ ruleIds = matchingDocs.map((d) => d._id);
48
+ }
49
+ if (ruleIds === null && !rule.task) {
50
+ // One rule validates 'all', so we wait for everything in this collection
51
+ matchAll = true;
52
+ break;
53
+ }
54
+ if (rule.task) {
55
+ criteria.push({ task: rule.task });
56
+ }
57
+ if (ruleIds !== null) {
58
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
+ criteria.push({ sourceDocId: { $in: ruleIds } });
60
+ }
61
+ }
62
+ if (!matchAll) {
63
+ if (criteria.length > 0) {
64
+ whitelistFilter = { $or: criteria };
65
+ }
66
+ else {
67
+ // Whitelist is active, but we have rules that result in effectively "nothing"
68
+ // (e.g. filter returned no docs).
69
+ // If we have NO criteria and NO matchAll, it implies we wait for nothing on this collection?
70
+ // Or should we treat it as blocking?
71
+ // If filter didn't match any doc, then we effectively wait for nothing for that rule.
72
+ // If ALL rules resulted in nothing, we continue to next entry.
73
+ continue;
74
+ }
75
+ }
76
+ }
77
+ const stableThresholdMs = (options.timeoutMs || 0) + (options.stabilityDurationMs || 0) + 100;
78
+ const baseQuery = {
54
79
  $or: [
55
80
  { status: { $in: ['processing', 'processing_dirty'] } },
56
81
  {
57
82
  status: 'pending',
58
- $or: [{ nextRunAt: { $lte: new Date(horizon) } }, { nextRunAt: null }],
83
+ $or: [{ nextRunAt: { $lte: new Date(Date.now() + stableThresholdMs) } }, { nextRunAt: null }],
59
84
  },
60
85
  ],
61
- });
86
+ };
87
+ const query = whitelistFilter ? { $and: [baseQuery, whitelistFilter] } : baseQuery;
88
+ const count = await entry.tasksCollection.countDocuments(query);
62
89
  if (count > 0) {
63
90
  debug(`Collection ${entry.tasksCollection.collectionName} has ${count} active tasks`);
64
91
  return false;
@@ -1 +1 @@
1
- {"version":3,"file":"waitUntilReactiveTasksIdle.js","sourceRoot":"","sources":["../../../src/testing/waitUntilReactiveTasksIdle.ts"],"names":[],"mappings":";;AAeA,gEAmEC;AAlFD,gCAAgC;AAChC,oDAAkE;AAClE,2CAA0D;AAE1D,MAAM,KAAK,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAE1C;;;;;;;;GAQG;AACI,KAAK,UAAU,0BAA0B,CAAC,gBAA2C,EAAE;IAC1F,MAAM,OAAO,mBACT,SAAS,EAAE,KAAK,EAChB,cAAc,EAAE,EAAE,EAClB,mBAAmB,EAAE,GAAG,IACrB,aAAa,CACnB,CAAC;IAEF,MAAM,IAAA,qBAAS,EAAC,KAAK,IAAI,EAAE;QACvB,sCAAsC;QACtC,2DAA2D;QAC3D,MAAM,OAAO,GA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AAE,IAAI,EAAE,CAAC;qBAChG;iBACJ;aACJ,CAAC;YAEF,MAAM,KAAK,GAA+B,eAAe,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,SAAS,EAAE,eAAe,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;YAE/G,MAAM,KAAK,GAAG,MAAM,KAAK,CAAC,eAAe,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;YAEhE,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;gBACZ,KAAK,CAAC,cAAc,KAAK,CAAC,eAAe,CAAC,cAAc,QAAQ,KAAK,eAAe,CAAC,CAAC;gBACtF,OAAO,KAAK,CAAC;YACjB,CAAC;QACL,CAAC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC,EAAE,OAAO,CAAC,CAAC;AAChB,CAAC"}
@@ -1,3 +1,4 @@
1
+ import { Document, Filter } from 'mongodb';
1
2
  import { WaitUntilOptions } from './waitUntil';
2
3
  /**
3
4
  * Waits until the reactive task system is idle.
@@ -8,4 +9,23 @@ import { WaitUntilOptions } from './waitUntil';
8
9
  *
9
10
  * This enables robust E2E testing by ensuring that all side effects and cascading tasks have finished.
10
11
  */
11
- export declare function waitUntilReactiveTasksIdle(customOptions?: Partial<WaitUntilOptions>): Promise<void>;
12
+ export interface WaitUntilReactiveTasksIdleOptions extends Partial<WaitUntilOptions> {
13
+ /**
14
+ * If provided, the function will only wait for tasks related to these specific entities.
15
+ * Global checks (Planner buffer, Active workers) are SKIPPED in this mode to ensure isolation
16
+ * from other running tests.
17
+ */
18
+ whitelist?: Array<{
19
+ collection: string;
20
+ /**
21
+ * Filter to find relevant documents.
22
+ * If not provided, ALL documents in the collection are considered (use carefully!).
23
+ */
24
+ filter?: Filter<Document>;
25
+ /**
26
+ * Optional task name filter.
27
+ */
28
+ task?: string;
29
+ }>;
30
+ }
31
+ export declare function waitUntilReactiveTasksIdle(customOptions?: WaitUntilReactiveTasksIdleOptions): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodash",
3
- "version": "2.5.2",
3
+ "version": "2.6.0",
4
4
  "type": "commonjs",
5
5
  "description": "An utility library delivering super-useful and super-simple tools using MongoDB",
6
6
  "main": "./dist/lib/index.js",