@kyo-services/schedulewise 2.0.2 โ†’ 2.1.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.
package/README.md CHANGED
@@ -20,10 +20,23 @@ A smart and efficient task scheduler for managing periodic jobs with precision t
20
20
 
21
21
  ## ๐Ÿ“ฆ Installation
22
22
 
23
+ You can install ScheduleWise using your preferred package manager:
24
+
25
+ Using npm:
23
26
  ```bash
24
27
  npm install @kyo-services/schedulewise
25
28
  ```
26
29
 
30
+ Using yarn:
31
+ ```bash
32
+ yarn add @kyo-services/schedulewise
33
+ ```
34
+
35
+ Using pnpm:
36
+ ```bash
37
+ pnpm add @kyo-services/schedulewise
38
+ ```
39
+
27
40
  ## ๐Ÿ”ง Usage
28
41
 
29
42
  ### Basic Task Scheduling
@@ -126,6 +139,38 @@ interface TaskOptions {
126
139
  | `removeTask(identifier)` | Removes a specific task |
127
140
  | `clearAllTasks()` | Removes all tasks |
128
141
  | `getAllTasks()` | Gets all scheduled tasks |
142
+ | `pause()` | Pauses all task executions |
143
+ | `resume()` | Resumes all task executions |
144
+ | `isPaused()` | Checks if scheduler is paused |
145
+ | `once(callback, options)` | Creates a one-time task with simplified API |
146
+
147
+ ### Scheduler Control
148
+
149
+ ```typescript
150
+ // Pause all task executions
151
+ sw.pause();
152
+
153
+ // Check if scheduler is paused
154
+ console.log('Scheduler paused:', sw.isPaused()); // true
155
+
156
+ // Resume all task executions
157
+ sw.resume();
158
+ ```
159
+
160
+ ### One-Time Task (Simplified API)
161
+
162
+ ```typescript
163
+ // Using the simplified 'once' method
164
+ sw.once(
165
+ () => {
166
+ console.log('This is a one-time task');
167
+ },
168
+ {
169
+ interval: 5000, // Execute after 5 seconds
170
+ name: 'simpleOneTimeTask' // Optional name
171
+ }
172
+ );
173
+ ```
129
174
 
130
175
  ## ๐Ÿงช Testing
131
176
 
@@ -1,5 +1,5 @@
1
- import { Task } from './Task.js';
2
- import { TaskCallback, TaskOptions } from './types.js';
1
+ import { Task } from "./Task.js";
2
+ import { TaskCallback, TaskOptions } from "./types.js";
3
3
  /**
4
4
  * A singleton class that manages scheduled tasks in the system
5
5
  * @class TaskScheduler
@@ -42,6 +42,26 @@ export declare class TaskScheduler {
42
42
  * Removes all tasks and stops the scheduler
43
43
  */
44
44
  clearAllTasks(): void;
45
+ /**
46
+ * Pauses the scheduler
47
+ */
48
+ pause(): void;
49
+ /**
50
+ * Resumes the scheduler
51
+ */
52
+ resume(): void;
53
+ /**
54
+ * Checks if the scheduler is paused
55
+ * @returns {boolean} True if the scheduler is paused, false otherwise
56
+ */
57
+ isPaused(): boolean;
58
+ /**
59
+ * Executes a callback once
60
+ * @param {() => void} callback - The callback to execute
61
+ * @param {TaskOptions} options - Configuration options for the task
62
+ * @returns {Task} The created task instance
63
+ */
64
+ once(callback: () => void, options: TaskOptions): Task;
45
65
  /**
46
66
  * Updates current timestamp and processes tasks
47
67
  * @internal For testing purposes
@@ -1,4 +1,4 @@
1
- import { Task } from './Task.js';
1
+ import { Task } from "./Task.js";
2
2
  /**
3
3
  * A singleton class that manages scheduled tasks in the system
4
4
  * @class TaskScheduler
@@ -76,6 +76,40 @@ export class TaskScheduler {
76
76
  }
77
77
  this.scheduledTasks.clear();
78
78
  }
79
+ /**
80
+ * Pauses the scheduler
81
+ */
82
+ pause() {
83
+ if (this.schedulerInterval) {
84
+ clearInterval(this.schedulerInterval);
85
+ this.schedulerInterval = null;
86
+ }
87
+ }
88
+ /**
89
+ * Resumes the scheduler
90
+ */
91
+ resume() {
92
+ if (!this.schedulerInterval) {
93
+ this.startTaskProcessor();
94
+ }
95
+ }
96
+ /**
97
+ * Checks if the scheduler is paused
98
+ * @returns {boolean} True if the scheduler is paused, false otherwise
99
+ */
100
+ isPaused() {
101
+ return this.schedulerInterval === null;
102
+ }
103
+ /**
104
+ * Executes a callback once
105
+ * @param {() => void} callback - The callback to execute
106
+ * @param {TaskOptions} options - Configuration options for the task
107
+ * @returns {Task} The created task instance
108
+ */
109
+ once(callback, options) {
110
+ const task = this.scheduleTask(callback, { ...options, once: true });
111
+ return task;
112
+ }
79
113
  /**
80
114
  * Updates current timestamp and processes tasks
81
115
  * @internal For testing purposes
@@ -85,7 +119,8 @@ export class TaskScheduler {
85
119
  this.currentTimestamp = new Date();
86
120
  for (const task of this.scheduledTasks.values()) {
87
121
  const elapsedTime = task.getInterval();
88
- const shouldRun = task.shouldExecute(elapsedTime) && task.lastExecutionTime.getTime() <= previousTimestamp.getTime();
122
+ const shouldRun = task.shouldExecute(elapsedTime) &&
123
+ task.lastExecutionTime.getTime() <= previousTimestamp.getTime();
89
124
  if (!shouldRun)
90
125
  continue;
91
126
  task.execute(this.currentTimestamp);
@@ -1 +1 @@
1
- {"version":3,"file":"TaskScheduler.js","sourceRoot":"","sources":["../src/TaskScheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC;;;GAGG;AACH,MAAM,OAAO,aAAa;IACd,MAAM,CAAC,QAAQ,CAAgB;IAC/B,gBAAgB,CAAO;IACvB,cAAc,GAA+B,IAAI,GAAG,EAAE,CAAC;IACvD,iBAAiB,GAA0C,IAAI,CAAC;IAExE;QACI,IAAI,CAAC,gBAAgB,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,IAAI,CAAC,kBAAkB,EAAE,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,WAAW;QACrB,IAAI,CAAC,aAAa,CAAC,QAAQ;YAAE,aAAa,CAAC,QAAQ,GAAG,IAAI,aAAa,EAAE,CAAC;QAC1E,OAAO,aAAa,CAAC,QAAQ,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACI,QAAQ,CAAC,UAA2B;QACvC,OAAO,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED;;;OAGG;IACI,WAAW;QACd,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,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BAAkB;QACzB,IAAI,IAAI,CAAC,iBAAiB;YAAE,aAAa,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QAElE,IAAI,CAAC,iBAAiB,GAAG,WAAW,CAAC,GAAG,EAAE;YACzC,IAAI,CAAC,gBAAgB,GAAG,IAAI,IAAI,EAAE,CAAC;YAEnC,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE,EAAE,CAAC;gBACjD,MAAM,WAAW,GAAG,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,iBAAiB,CAAC,OAAO,EAAE,CAAC;gBACvF,MAAM,SAAS,GAAG,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;gBAElD,IAAI,CAAC,SAAS;oBAAE,SAAS;gBAEzB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;gBACpC,IAAI,IAAI,CAAC,YAAY,EAAE;oBAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACnD,CAAC;QACF,CAAC,EAAE,GAAG,CAAC,CAAC;IACT,CAAC;CACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kyo-services/schedulewise",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=18.0.0"
@@ -1,130 +1,171 @@
1
- import { Task } from './Task.js';
2
- import { TaskCallback, TaskOptions } from './types.js';
1
+ import { Task } from "./Task.js";
2
+ import { TaskCallback, TaskOptions } from "./types.js";
3
3
 
4
4
  /**
5
5
  * A singleton class that manages scheduled tasks in the system
6
6
  * @class TaskScheduler
7
7
  */
8
8
  export class TaskScheduler {
9
- private static instance: TaskScheduler;
10
- private currentTimestamp: Date;
11
- private scheduledTasks: Map<number | string, Task> = new Map();
12
- private schedulerInterval: ReturnType<typeof setInterval> | null = null;
13
-
14
- private constructor() {
15
- this.currentTimestamp = new Date();
16
- this.startTaskProcessor();
17
- }
18
-
19
- /**
20
- * Gets the singleton instance of TaskScheduler
21
- * @returns {TaskScheduler} The singleton instance
22
- */
23
- public static getInstance(): TaskScheduler {
24
- if (!TaskScheduler.instance) TaskScheduler.instance = new TaskScheduler();
25
- return TaskScheduler.instance;
26
- }
27
-
28
- /**
29
- * Finds a task by its identifier (id or name)
30
- * @param {number | string} identifier - The task's ID or name
31
- * @returns {Task | undefined} The found task or undefined
32
- */
33
- public findTask(identifier: number | string): Task | undefined {
34
- return this.scheduledTasks.get(identifier);
35
- }
36
-
37
- /**
38
- * Gets all scheduled tasks in the system
39
- * @returns {Task[]} Array of all scheduled tasks
40
- */
41
- public getAllTasks(): Task[] {
42
- return Array.from(this.scheduledTasks.values());
43
- }
44
-
45
- /**
46
- * Creates and schedules a new task
47
- * @param {TaskCallback} callback - Function to be executed
48
- * @param {TaskOptions} options - Configuration options for the task
49
- * @returns {Task} The created task instance
50
- */
51
- public scheduleTask(callback: TaskCallback, options: TaskOptions): Task {
52
- const taskId = Math.floor(Math.random() * 1000000);
53
- const task = new Task(
54
- this,
55
- options.name,
56
- taskId,
57
- callback,
58
- options.interval,
59
- false,
60
- options.once || false
61
- );
62
-
63
- this.scheduledTasks.set(taskId, task);
64
- if (options.name) this.scheduledTasks.set(options.name, task);
65
-
66
- if (options.immediate) {
67
- task.execute(this.currentTimestamp);
68
- task.lastExecutionTime = this.currentTimestamp;
69
- }
70
-
71
- return task;
72
- }
73
-
74
- /**
75
- * Removes a task from the scheduler
76
- * @param {number | string} identifier - The task's ID or name
77
- */
78
- public removeTask(identifier: number | string): void {
79
- const task = this.findTask(identifier);
80
- if (!task) return;
81
-
82
- this.scheduledTasks.delete(task.id);
83
- if (task.name) this.scheduledTasks.delete(task.name);
84
- }
85
-
86
- /**
87
- * Removes all tasks and stops the scheduler
88
- */
89
- public clearAllTasks(): void {
90
- if (this.schedulerInterval) {
91
- clearInterval(this.schedulerInterval);
92
- this.schedulerInterval = null;
93
- }
94
- this.scheduledTasks.clear();
95
- }
96
-
97
- /**
98
- * Updates current timestamp and processes tasks
99
- * @internal For testing purposes
100
- */
101
- public processTasksNow(): void {
102
- const previousTimestamp = this.currentTimestamp;
103
- this.currentTimestamp = new Date();
104
-
105
- for (const task of this.scheduledTasks.values()) {
106
- const elapsedTime = task.getInterval();
107
- const shouldRun = task.shouldExecute(elapsedTime) && task.lastExecutionTime.getTime() <= previousTimestamp.getTime();
108
-
109
- if (!shouldRun) continue;
110
-
111
- task.execute(this.currentTimestamp);
112
- if (task.shouldRemove()) {
113
- this.removeTask(task.id);
114
- }
115
- }
116
- }
117
-
118
- /**
119
- * Starts the task processor that executes scheduled tasks
120
- * @private
121
- */
122
- private startTaskProcessor(): void {
123
- if (this.schedulerInterval) clearInterval(this.schedulerInterval);
124
-
125
- this.schedulerInterval = setInterval(() => {
9
+ private static instance: TaskScheduler;
10
+ private currentTimestamp: Date;
11
+ private scheduledTasks: Map<number | string, Task> = new Map();
12
+ private schedulerInterval: ReturnType<typeof setInterval> | null = null;
13
+
14
+ private constructor() {
15
+ this.currentTimestamp = new Date();
16
+ this.startTaskProcessor();
17
+ }
18
+
19
+ /**
20
+ * Gets the singleton instance of TaskScheduler
21
+ * @returns {TaskScheduler} The singleton instance
22
+ */
23
+ public static getInstance(): TaskScheduler {
24
+ if (!TaskScheduler.instance)
25
+ TaskScheduler.instance = new TaskScheduler();
26
+ return TaskScheduler.instance;
27
+ }
28
+
29
+ /**
30
+ * Finds a task by its identifier (id or name)
31
+ * @param {number | string} identifier - The task's ID or name
32
+ * @returns {Task | undefined} The found task or undefined
33
+ */
34
+ public findTask(identifier: number | string): Task | undefined {
35
+ return this.scheduledTasks.get(identifier);
36
+ }
37
+
38
+ /**
39
+ * Gets all scheduled tasks in the system
40
+ * @returns {Task[]} Array of all scheduled tasks
41
+ */
42
+ public getAllTasks(): Task[] {
43
+ return Array.from(this.scheduledTasks.values());
44
+ }
45
+
46
+ /**
47
+ * Creates and schedules a new task
48
+ * @param {TaskCallback} callback - Function to be executed
49
+ * @param {TaskOptions} options - Configuration options for the task
50
+ * @returns {Task} The created task instance
51
+ */
52
+ public scheduleTask(callback: TaskCallback, options: TaskOptions): Task {
53
+ const taskId = Math.floor(Math.random() * 1000000);
54
+ const task = new Task(
55
+ this,
56
+ options.name,
57
+ taskId,
58
+ callback,
59
+ options.interval,
60
+ false,
61
+ options.once || false
62
+ );
63
+
64
+ this.scheduledTasks.set(taskId, task);
65
+ if (options.name) this.scheduledTasks.set(options.name, task);
66
+
67
+ if (options.immediate) {
68
+ task.execute(this.currentTimestamp);
69
+ task.lastExecutionTime = this.currentTimestamp;
70
+ }
71
+
72
+ return task;
73
+ }
74
+
75
+ /**
76
+ * Removes a task from the scheduler
77
+ * @param {number | string} identifier - The task's ID or name
78
+ */
79
+ public removeTask(identifier: number | string): void {
80
+ const task = this.findTask(identifier);
81
+ if (!task) return;
82
+
83
+ this.scheduledTasks.delete(task.id);
84
+ if (task.name) this.scheduledTasks.delete(task.name);
85
+ }
86
+
87
+ /**
88
+ * Removes all tasks and stops the scheduler
89
+ */
90
+ public clearAllTasks(): void {
91
+ if (this.schedulerInterval) {
92
+ clearInterval(this.schedulerInterval);
93
+ this.schedulerInterval = null;
94
+ }
95
+ this.scheduledTasks.clear();
96
+ }
97
+
98
+ /**
99
+ * Pauses the scheduler
100
+ */
101
+ public pause(): void {
102
+ if (this.schedulerInterval) {
103
+ clearInterval(this.schedulerInterval);
104
+ this.schedulerInterval = null;
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Resumes the scheduler
110
+ */
111
+ public resume(): void {
112
+ if (!this.schedulerInterval) {
113
+ this.startTaskProcessor();
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Checks if the scheduler is paused
119
+ * @returns {boolean} True if the scheduler is paused, false otherwise
120
+ */
121
+ public isPaused(): boolean {
122
+ return this.schedulerInterval === null;
123
+ }
124
+
125
+ /**
126
+ * Executes a callback once
127
+ * @param {() => void} callback - The callback to execute
128
+ * @param {TaskOptions} options - Configuration options for the task
129
+ * @returns {Task} The created task instance
130
+ */
131
+ public once(callback: () => void, options: TaskOptions): Task {
132
+ const task = this.scheduleTask(callback, { ...options, once: true });
133
+ return task;
134
+ }
135
+
136
+ /**
137
+ * Updates current timestamp and processes tasks
138
+ * @internal For testing purposes
139
+ */
140
+ public processTasksNow(): void {
141
+ const previousTimestamp = this.currentTimestamp;
142
+ this.currentTimestamp = new Date();
143
+
144
+ for (const task of this.scheduledTasks.values()) {
145
+ const elapsedTime = task.getInterval();
146
+ const shouldRun =
147
+ task.shouldExecute(elapsedTime) &&
148
+ task.lastExecutionTime.getTime() <= previousTimestamp.getTime();
149
+
150
+ if (!shouldRun) continue;
151
+
152
+ task.execute(this.currentTimestamp);
153
+ if (task.shouldRemove()) {
154
+ this.removeTask(task.id);
155
+ }
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Starts the task processor that executes scheduled tasks
161
+ * @private
162
+ */
163
+ private startTaskProcessor(): void {
164
+ if (this.schedulerInterval) clearInterval(this.schedulerInterval);
165
+
166
+ this.schedulerInterval = setInterval(() => {
126
167
  this.currentTimestamp = new Date();
127
-
168
+
128
169
  for (const task of this.scheduledTasks.values()) {
129
170
  const elapsedTime = this.currentTimestamp.getTime() - task.lastExecutionTime.getTime();
130
171
  const shouldRun = task.shouldExecute(elapsedTime);
@@ -134,6 +175,6 @@ export class TaskScheduler {
134
175
  task.execute(this.currentTimestamp);
135
176
  if (task.shouldRemove()) this.removeTask(task.id);
136
177
  }
137
- }, 100);
138
- }
139
- }
178
+ }, 100);
179
+ }
180
+ }