@degenerate/generic 0.0.1 → 0.0.2
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/index.js +1 -1
- package/package.json +1 -1
- package/tsconfig.json +2 -2
- package/src/scheduler/rapture-scheduler.ts +0 -98
- package/src/tasks/rapture-tasks.ts +0 -61
package/dist/index.js
CHANGED
|
@@ -2,4 +2,4 @@ import { CryptoSettings } from './_settings/cryptography-settings';
|
|
|
2
2
|
// import * as crypto from './crypto'; this would become crypto.calculateHash ...
|
|
3
3
|
import { calculateHash } from './crypto';
|
|
4
4
|
export { calculateHash, CryptoSettings };
|
|
5
|
-
//# sourceMappingURL=
|
|
5
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLGNBQWMsRUFBRSxNQUFNLG1DQUFtQyxDQUFDO0FBQ25FLGlGQUFpRjtBQUNqRixPQUFPLEVBQUUsYUFBYSxFQUFFLE1BQU0sVUFBVSxDQUFDO0FBRXpDLE9BQU8sRUFDSCxhQUFhLEVBQ2IsY0FBYyxFQUNqQixDQUFDIn0=
|
package/package.json
CHANGED
package/tsconfig.json
CHANGED
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
56
56
|
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
57
57
|
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
58
|
-
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
59
|
-
|
|
58
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
59
|
+
"inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
60
60
|
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
61
61
|
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
62
62
|
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { TaskNotFoundError } from '../errors/scheduler/scheduler-errors';
|
|
2
|
-
import { RaptureTask } from '../tasks/rapture-tasks';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* This is a task scheduler for the rapture network.
|
|
6
|
-
*/
|
|
7
|
-
export class RaptureScheduler {
|
|
8
|
-
private tasks: RaptureTask[] = []; // an array of tasks
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Add a new task to be executed at a specific date and time
|
|
12
|
-
*
|
|
13
|
-
* @param time the date and time to run the task
|
|
14
|
-
* @param callback the function to call
|
|
15
|
-
* @param interval the interval in milliseconds for repeating tasks
|
|
16
|
-
* @returns the task ID
|
|
17
|
-
*/
|
|
18
|
-
scheduleTask(time: Date, callback: () => void, interval: number | null = null): string {
|
|
19
|
-
const task = new RaptureTask(time, callback, interval); // Create a new task
|
|
20
|
-
const taskId = task.id; // Get the task ID
|
|
21
|
-
this.tasks.push(task); // Add the task to the array
|
|
22
|
-
this.scheduleNextTask(task); // Schedule the task to run
|
|
23
|
-
return taskId;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Cancel a task by its ID and remove it from the scheduler
|
|
28
|
-
*
|
|
29
|
-
* @param taskId the ID of the task to remove
|
|
30
|
-
*/
|
|
31
|
-
removeTask(taskId: string): void {
|
|
32
|
-
const taskIndex = this.tasks.findIndex(task => task.id === taskId);
|
|
33
|
-
// make sure the task exists.
|
|
34
|
-
if (taskIndex !== -1) {
|
|
35
|
-
const task = this.tasks[taskIndex];
|
|
36
|
-
if (task.timeoutId) {
|
|
37
|
-
clearTimeout(task.timeoutId);
|
|
38
|
-
}
|
|
39
|
-
this.tasks.splice(taskIndex, 1); // Remove the task from the array
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
// if the task does not exist, throw an error.
|
|
43
|
-
throw new TaskNotFoundError(taskId.toString());
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* This function updates the time of a task by its ID.
|
|
48
|
-
*
|
|
49
|
-
* @param taskId the ID of the task to update
|
|
50
|
-
* @param newTime the new time to run the task
|
|
51
|
-
*/
|
|
52
|
-
updateTaskTime(taskId: string, newTime: Date): void {
|
|
53
|
-
const task = this.tasks.find(task => task.id === taskId);
|
|
54
|
-
if (task) {
|
|
55
|
-
if (task.timeoutId) {
|
|
56
|
-
clearTimeout(task.timeoutId); // Clear the existing timeout
|
|
57
|
-
}
|
|
58
|
-
task.time = newTime; // Update the time
|
|
59
|
-
this.scheduleNextTask(task); // Reschedule the task
|
|
60
|
-
console.log(`Task ${taskId} rescheduled for ${newTime.toUTCString()}.`);
|
|
61
|
-
}
|
|
62
|
-
throw new TaskNotFoundError(taskId);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Helper to schedule the task based on date and time
|
|
66
|
-
/**
|
|
67
|
-
* the function to schedule the next task based on the time.
|
|
68
|
-
* @param task the task to schedule or run right away.
|
|
69
|
-
*/
|
|
70
|
-
private scheduleNextTask(task: RaptureTask) {
|
|
71
|
-
const now = new Date().getTime();
|
|
72
|
-
const delay = task.time.getTime() - now;
|
|
73
|
-
|
|
74
|
-
if (delay <= 0) {
|
|
75
|
-
// If the time has already passed, execute the task immediately
|
|
76
|
-
this.runTask(task);
|
|
77
|
-
} else {
|
|
78
|
-
// Schedule the task to run after the delay
|
|
79
|
-
task.timeoutId = setTimeout(() => {
|
|
80
|
-
this.runTask(task);
|
|
81
|
-
}, delay);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Executes the task and reschedules it if it's a recurring task
|
|
86
|
-
private runTask(task: RaptureTask) {
|
|
87
|
-
task.callback();
|
|
88
|
-
|
|
89
|
-
if (task.interval !== null) {
|
|
90
|
-
// If the task is recurring, reschedule it based on the interval
|
|
91
|
-
task.time = new Date(new Date().getTime() + task.interval);
|
|
92
|
-
this.scheduleNextTask(task);
|
|
93
|
-
} else {
|
|
94
|
-
// Remove the task once it has run if it's non-recurring
|
|
95
|
-
this.removeTask(task.id);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { calculateHash } from '../crypto/calculate-hash';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This is a task scheduler that allows you to schedule tasks to run at a specific time or at a specific interval.
|
|
5
|
-
*/
|
|
6
|
-
export class RaptureTask {
|
|
7
|
-
private _id: string; // the task id
|
|
8
|
-
private _time: Date; // the time to run the task
|
|
9
|
-
private _callback: () => void; // the function to call
|
|
10
|
-
private _interval: number | null; // in milliseconds for repeating tasks
|
|
11
|
-
private _timeoutId: NodeJS.Timeout | null; // store the timeout id to cancel if needed
|
|
12
|
-
|
|
13
|
-
constructor(time: Date, callback: () => void, interval: number | null = null, id = '') {
|
|
14
|
-
this._time = time;
|
|
15
|
-
this._callback = callback;
|
|
16
|
-
this._interval = interval;
|
|
17
|
-
this._timeoutId = null;
|
|
18
|
-
if (id) {
|
|
19
|
-
this._id = id; // use the provided id if available
|
|
20
|
-
} else {
|
|
21
|
-
this._id = this.generateId(); // generate a unique id for the task based on the hash.
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
//#region Getters and Setters
|
|
26
|
-
get id(): string {
|
|
27
|
-
return this._id;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get time(): Date {
|
|
31
|
-
return this._time;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
set time(value: Date) {
|
|
35
|
-
this._time = value;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public callback(): () => void {
|
|
39
|
-
return this._callback;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
get interval(): number | null {
|
|
43
|
-
return this._interval;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
get timeoutId(): NodeJS.Timeout | null {
|
|
47
|
-
return this._timeoutId;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
set timeoutId(value: NodeJS.Timeout | null) {
|
|
51
|
-
this._timeoutId = value;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Setter methods to modify private properties if needed
|
|
55
|
-
|
|
56
|
-
private generateId(): string {
|
|
57
|
-
const stringForId = this._time.toISOString() + JSON.stringify(this._callback);
|
|
58
|
-
const hashId = calculateHash(stringForId, 'hex');
|
|
59
|
-
return hashId;
|
|
60
|
-
}
|
|
61
|
-
}
|