@warlock.js/core 2.1.1 → 2.2.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.
Files changed (43) hide show
  1. package/cjs/index.js +1 -1
  2. package/cjs/mail/sendMail.js +2 -8
  3. package/cjs/mail/sendMail.js.map +1 -1
  4. package/cjs/repositories/repository-list-manager.d.ts +4 -0
  5. package/cjs/repositories/repository-list-manager.d.ts.map +1 -1
  6. package/cjs/repositories/repository-list-manager.js +6 -0
  7. package/cjs/repositories/repository-list-manager.js.map +1 -1
  8. package/cjs/router/router.d.ts +12 -0
  9. package/cjs/router/router.d.ts.map +1 -1
  10. package/cjs/router/router.js +26 -0
  11. package/cjs/router/router.js.map +1 -1
  12. package/cjs/utils/get-localized.d.ts +1 -1
  13. package/cjs/utils/get-localized.d.ts.map +1 -1
  14. package/cjs/utils/get-localized.js +2 -2
  15. package/cjs/utils/get-localized.js.map +1 -1
  16. package/cjs/utils/index.d.ts +1 -0
  17. package/cjs/utils/index.d.ts.map +1 -1
  18. package/cjs/utils/queue.d.ts +49 -0
  19. package/cjs/utils/queue.d.ts.map +1 -0
  20. package/cjs/utils/queue.js +89 -0
  21. package/cjs/utils/queue.js.map +1 -0
  22. package/esm/index.js +1 -1
  23. package/esm/mail/sendMail.js +2 -8
  24. package/esm/mail/sendMail.js.map +1 -1
  25. package/esm/repositories/repository-list-manager.d.ts +4 -0
  26. package/esm/repositories/repository-list-manager.d.ts.map +1 -1
  27. package/esm/repositories/repository-list-manager.js +6 -0
  28. package/esm/repositories/repository-list-manager.js.map +1 -1
  29. package/esm/router/router.d.ts +12 -0
  30. package/esm/router/router.d.ts.map +1 -1
  31. package/esm/router/router.js +26 -0
  32. package/esm/router/router.js.map +1 -1
  33. package/esm/utils/get-localized.d.ts +1 -1
  34. package/esm/utils/get-localized.d.ts.map +1 -1
  35. package/esm/utils/get-localized.js +2 -2
  36. package/esm/utils/get-localized.js.map +1 -1
  37. package/esm/utils/index.d.ts +1 -0
  38. package/esm/utils/index.d.ts.map +1 -1
  39. package/esm/utils/queue.d.ts +49 -0
  40. package/esm/utils/queue.d.ts.map +1 -0
  41. package/esm/utils/queue.js +89 -0
  42. package/esm/utils/queue.js.map +1 -0
  43. package/package.json +3 -3
@@ -0,0 +1,89 @@
1
+ /**
2
+ * A utility class for managing a queue of operations.
3
+ * Allows enqueuing values and executing a function when the queue reaches a certain size or after a specified interval.
4
+ * Supports both parallel and sequential execution of the queued operations.
5
+ */
6
+ class Queue {
7
+ /** The items currently in the queue. */
8
+ items = [];
9
+ /** The maximum size of the queue before triggering execution. */
10
+ maxSize;
11
+ /** The interval in milliseconds after which the queue will be executed if not already triggered by size. */
12
+ interval;
13
+ /** The function to execute with the items in the queue. */
14
+ executeFn;
15
+ /** Timer for managing the interval-based execution. */
16
+ timer = null;
17
+ /** Flag to determine if execution should be parallel or sequential. */
18
+ executeInParallel;
19
+ /** The batch size for processing items. */
20
+ batchSize;
21
+ /** Whether the current queue is busy executing */
22
+ isExecuting = false;
23
+ /**
24
+ * Constructs a new Queue instance.
25
+ * @param executeFn - The function to execute with the items in the queue.
26
+ * @param maxSize - The maximum number of items before the queue is executed.
27
+ * @param executeEvery - The time in milliseconds after which the queue is executed if not already triggered.
28
+ * @param executeInParallel - Whether to execute the function in parallel or sequentially.
29
+ * @param batchSize - The number of items to process in each batch.
30
+ */
31
+ constructor(executeFn, executeInParallel = true, executeEvery = 5000, batchSize, maxSize) {
32
+ this.executeFn = executeFn;
33
+ this.maxSize = maxSize;
34
+ this.interval = executeEvery;
35
+ this.executeInParallel = executeInParallel;
36
+ this.batchSize = batchSize;
37
+ }
38
+ /**
39
+ * Adds an item to the queue.
40
+ * Triggers execution if the queue reaches the maximum size.
41
+ * Starts a timer if not already running.
42
+ * @param item - The item to add to the queue.
43
+ */
44
+ enqueue(item) {
45
+ this.items.push(item);
46
+ if (this.maxSize && this.items.length >= this.maxSize) {
47
+ this.execute();
48
+ }
49
+ if (!this.timer) {
50
+ this.startTimer();
51
+ }
52
+ }
53
+ /**
54
+ * Starts a timer to execute the queue after the specified interval.
55
+ */
56
+ startTimer() {
57
+ this.timer = setInterval(() => {
58
+ if (this.items.length > 0) {
59
+ this.execute();
60
+ }
61
+ }, this.interval);
62
+ }
63
+ /**
64
+ * Executes the function with the current items in the queue.
65
+ * Processes items in batches and resets the timer.
66
+ */
67
+ async execute() {
68
+ if (this.timer) {
69
+ clearInterval(this.timer);
70
+ this.timer = null;
71
+ }
72
+ this.isExecuting = true;
73
+ // Now there are couple scenarios:
74
+ // 1. Batch size has value, we need to check if its going to be executed in parallel or sequentially
75
+ // 2. Batch size is not provided, we will execute all items in a single call
76
+ if (this.batchSize) {
77
+ const itemsToProcess = this.items.splice(0, this.batchSize);
78
+ if (this.executeInParallel) {
79
+ await Promise.all(itemsToProcess.map(item => this.executeFn([item])));
80
+ }
81
+ else {
82
+ for (const item of itemsToProcess) {
83
+ await this.executeFn([item]);
84
+ }
85
+ }
86
+ }
87
+ this.isExecuting = false;
88
+ }
89
+ }export{Queue};//# sourceMappingURL=queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.js","sources":["../../src/utils/queue.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;;AAIG;MACU,KAAK,CAAA;;IAER,KAAK,GAAQ,EAAE,CAAC;;AAGP,IAAA,OAAO,CAAU;;AAGjB,IAAA,QAAQ,CAAS;;AAGjB,IAAA,SAAS,CAAgC;;IAGlD,KAAK,GAA0B,IAAI,CAAC;;AAG3B,IAAA,iBAAiB,CAAU;;AAG3B,IAAA,SAAS,CAAS;;IAG3B,WAAW,GAAG,KAAK,CAAC;AAE5B;;;;;;;AAOG;IACH,WACE,CAAA,SAAwC,EACxC,iBAAA,GAA6B,IAAI,EACjC,eAAuB,IAAI,EAC3B,SAAiB,EACjB,OAAgB,EAAA;AAEhB,QAAA,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;AAC3B,QAAA,IAAI,CAAC,OAAO,GAAG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AM,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACvE,aAAA;AAAM,iBAAA;AACL,gBAAA,KAAK,MAAM,IAAI,IAAI,cAAc,EAAE;oBACjC,MAAM,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;AAC9B,iBAAA;AACF,aAAA;AACF,SAAA;AAED,QAAA,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;KAC1B;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warlock.js/core",
3
- "version": "2.1.1",
3
+ "version": "2.2.1",
4
4
  "description": "A robust nodejs framework for building blazing fast applications",
5
5
  "main": "./cjs/index.js",
6
6
  "bin": {
@@ -27,8 +27,8 @@
27
27
  "@mongez/fs": "^3.0.5",
28
28
  "@mongez/http": "^2.2.10",
29
29
  "@mongez/localization": "^3.0.0",
30
- "@warlock.js/logger": "2.1.1",
31
- "@warlock.js/cascade": "2.1.1",
30
+ "@warlock.js/logger": "2.2.1",
31
+ "@warlock.js/cascade": "2.2.1",
32
32
  "@mongez/reinforcements": "^2.3.10",
33
33
  "commander": "^12.0.0",
34
34
  "@mongez/slug": "^1.0.7",