@degenerate/events 0.0.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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @Degenerate/<new-package-name> Package
2
+
3
+ This package contains the functions for <new-package-name>. The package <what-the-purpose-of-the-package-is> in the SAFE environment.
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ //#region Scheduler
3
+ //#endregion Scheduler
4
+ //#region Task
5
+ //#endregion Task
@@ -0,0 +1,2 @@
1
+ export * from './scheduler';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/scheduler/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./scheduler"), exports);
@@ -0,0 +1,35 @@
1
+ /**
2
+ * This is a task scheduler for the safe network.
3
+ */
4
+ export declare class SafeScheduler {
5
+ private tasks;
6
+ /**
7
+ * Add a new task to be executed at a specific date and time
8
+ *
9
+ * @param time the date and time to run the task
10
+ * @param callback the function to call
11
+ * @param interval the interval in milliseconds for repeating tasks
12
+ * @returns the task ID
13
+ */
14
+ scheduleTask(time: Date, callback: () => void, interval?: number | null): string;
15
+ /**
16
+ * Cancel a task by its ID and remove it from the scheduler
17
+ *
18
+ * @param taskId the ID of the task to remove
19
+ */
20
+ removeTask(taskId: string): void;
21
+ /**
22
+ * This function updates the time of a task by its ID.
23
+ *
24
+ * @param taskId the ID of the task to update
25
+ * @param newTime the new time to run the task
26
+ */
27
+ updateTaskTime(taskId: string, newTime: Date): void;
28
+ /**
29
+ * the function to schedule the next task based on the time.
30
+ * @param task the task to schedule or run right away.
31
+ */
32
+ private scheduleNextTask;
33
+ private runTask;
34
+ }
35
+ //# sourceMappingURL=scheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../../src/scheduler/scheduler.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,qBAAa,aAAa;IACtB,OAAO,CAAC,KAAK,CAAkB;IAE/B;;;;;;;OAOG;IACH,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,IAAI,EAAE,QAAQ,GAAE,MAAM,GAAG,IAAW,GAAG,MAAM;IAQtF;;;;OAIG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAehC;;;;;OAKG;IACH,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,GAAG,IAAI;IAcnD;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAgBxB,OAAO,CAAC,OAAO;CAYlB"}
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SafeScheduler = void 0;
4
+ const generic_1 = require("@degenerate/generic");
5
+ const task_1 = require("../task/task");
6
+ /**
7
+ * This is a task scheduler for the safe network.
8
+ */
9
+ class SafeScheduler {
10
+ constructor() {
11
+ this.tasks = []; // an array of tasks
12
+ }
13
+ /**
14
+ * Add a new task to be executed at a specific date and time
15
+ *
16
+ * @param time the date and time to run the task
17
+ * @param callback the function to call
18
+ * @param interval the interval in milliseconds for repeating tasks
19
+ * @returns the task ID
20
+ */
21
+ scheduleTask(time, callback, interval = null) {
22
+ const task = new task_1.SafeTask(time, callback, interval); // Create a new task
23
+ const taskId = task.id; // Get the task ID
24
+ this.tasks.push(task); // Add the task to the array
25
+ this.scheduleNextTask(task); // Schedule the task to run
26
+ return taskId;
27
+ }
28
+ /**
29
+ * Cancel a task by its ID and remove it from the scheduler
30
+ *
31
+ * @param taskId the ID of the task to remove
32
+ */
33
+ removeTask(taskId) {
34
+ const taskIndex = this.tasks.findIndex(task => task.id === taskId);
35
+ // make sure the task exists.
36
+ if (taskIndex !== -1) {
37
+ const task = this.tasks[taskIndex];
38
+ if (task.timeoutId) {
39
+ clearTimeout(task.timeoutId);
40
+ }
41
+ this.tasks.splice(taskIndex, 1); // Remove the task from the array
42
+ return;
43
+ }
44
+ // if the task does not exist, throw an error.
45
+ throw new generic_1.TaskNotFoundError(taskId.toString());
46
+ }
47
+ /**
48
+ * This function updates the time of a task by its ID.
49
+ *
50
+ * @param taskId the ID of the task to update
51
+ * @param newTime the new time to run the task
52
+ */
53
+ updateTaskTime(taskId, newTime) {
54
+ const task = this.tasks.find(task => task.id === taskId);
55
+ if (task) {
56
+ if (task.timeoutId) {
57
+ clearTimeout(task.timeoutId); // Clear the existing timeout
58
+ }
59
+ task.time = newTime; // Update the time
60
+ this.scheduleNextTask(task); // Reschedule the task
61
+ console.log(`Task ${taskId} rescheduled for ${newTime.toUTCString()}.`);
62
+ }
63
+ throw new generic_1.TaskNotFoundError(taskId);
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
+ scheduleNextTask(task) {
71
+ const now = new Date().getTime();
72
+ const delay = task.time.getTime() - now;
73
+ if (delay <= 0) {
74
+ // If the time has already passed, execute the task immediately
75
+ this.runTask(task);
76
+ }
77
+ else {
78
+ // Schedule the task to run after the delay
79
+ task.timeoutId = setTimeout(() => {
80
+ this.runTask(task);
81
+ }, delay);
82
+ }
83
+ }
84
+ // Executes the task and reschedules it if it's a recurring task
85
+ runTask(task) {
86
+ task.callback();
87
+ if (task.interval !== null) {
88
+ // If the task is recurring, reschedule it based on the interval
89
+ task.time = new Date(new Date().getTime() + task.interval);
90
+ this.scheduleNextTask(task);
91
+ }
92
+ else {
93
+ // Remove the task once it has run if it's non-recurring
94
+ this.removeTask(task.id);
95
+ }
96
+ }
97
+ }
98
+ exports.SafeScheduler = SafeScheduler;
@@ -0,0 +1,2 @@
1
+ export * from './task';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/task/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./task"), exports);
@@ -0,0 +1,21 @@
1
+ /**
2
+ * This is a task scheduler that allows you to schedule tasks to run at a
3
+ * specific time or at a specific interval.
4
+ */
5
+ export declare class SafeTask {
6
+ private _id;
7
+ private _time;
8
+ private _callback;
9
+ private _interval;
10
+ private _timeoutId;
11
+ constructor(time: Date, callback: () => void, interval?: number | null, id?: string);
12
+ get id(): string;
13
+ get time(): Date;
14
+ set time(value: Date);
15
+ callback(): () => void;
16
+ get interval(): number | null;
17
+ get timeoutId(): NodeJS.Timeout | null;
18
+ set timeoutId(value: NodeJS.Timeout | null);
19
+ private generateId;
20
+ }
21
+ //# sourceMappingURL=task.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task.d.ts","sourceRoot":"","sources":["../../src/task/task.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,QAAQ;IACjB,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,KAAK,CAAO;IACpB,OAAO,CAAC,SAAS,CAAa;IAC9B,OAAO,CAAC,SAAS,CAAgB;IACjC,OAAO,CAAC,UAAU,CAAwB;gBAE9B,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,IAAI,EAAE,QAAQ,GAAE,MAAM,GAAG,IAAW,EAAE,EAAE,SAAK;IAarF,IAAI,EAAE,IAAI,MAAM,CAEf;IAED,IAAI,IAAI,IAAI,IAAI,CAEf;IAED,IAAI,IAAI,CAAC,KAAK,EAAE,IAAI,EAEnB;IAEM,QAAQ,IAAI,MAAM,IAAI;IAI7B,IAAI,QAAQ,IAAI,MAAM,GAAG,IAAI,CAE5B;IAED,IAAI,SAAS,IAAI,MAAM,CAAC,OAAO,GAAG,IAAI,CAErC;IAED,IAAI,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,GAAG,IAAI,EAEzC;IAID,OAAO,CAAC,UAAU;CAKrB"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SafeTask = void 0;
4
+ const generic_1 = require("@degenerate/generic");
5
+ /**
6
+ * This is a task scheduler that allows you to schedule tasks to run at a
7
+ * specific time or at a specific interval.
8
+ */
9
+ class SafeTask {
10
+ constructor(time, callback, interval = null, id = '') {
11
+ this._time = time;
12
+ this._callback = callback;
13
+ this._interval = interval;
14
+ this._timeoutId = null;
15
+ if (id) {
16
+ this._id = id; // use the provided id if available
17
+ }
18
+ else {
19
+ this._id = this.generateId(); // generate a unique id for the task based on the hash.
20
+ }
21
+ }
22
+ //#region Getters and Setters
23
+ get id() {
24
+ return this._id;
25
+ }
26
+ get time() {
27
+ return this._time;
28
+ }
29
+ set time(value) {
30
+ this._time = value;
31
+ }
32
+ callback() {
33
+ return this._callback;
34
+ }
35
+ get interval() {
36
+ return this._interval;
37
+ }
38
+ get timeoutId() {
39
+ return this._timeoutId;
40
+ }
41
+ set timeoutId(value) {
42
+ this._timeoutId = value;
43
+ }
44
+ // Setter methods to modify private properties if needed
45
+ generateId() {
46
+ const stringForId = this._time.toISOString() + JSON.stringify(this._callback);
47
+ const hashId = (0, generic_1.calculateHash)(stringForId, 'hex');
48
+ return hashId;
49
+ }
50
+ }
51
+ exports.SafeTask = SafeTask;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@degenerate/events",
3
+ "version": "0.0.1",
4
+ "description": "This contains the ShareData Adaptive Framework Ecosystem (SAFE) network event code",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "rm -rf dist/* && tsc -p tsconfig.pkg.json",
9
+ "compile": "rm -rf dist/* && tsc -p tsconfig.pkg.json",
10
+ "test": "jest --forceExit --detectOpenHandles --verbose --silent=false --env node --config tsconfig.pkg.json",
11
+ "test:coverage": "jest --forceExit --coverage --detectOpenHandles --verbose --silent=false --env node --config tsconfig.pkg.json",
12
+ "test:clearCache": "jest --clearCache"
13
+ },
14
+ "keywords": [
15
+ "ShareData Adaptive Framework Ecosystem",
16
+ "SAFE EVENTS",
17
+ "SAFE",
18
+ "SAFE Generic",
19
+ "ShareData",
20
+ "ShareBlock"
21
+ ],
22
+ "author": "Degenerate Inc.",
23
+ "license": "ISC",
24
+ "dependencies": {
25
+ "@degenerate/generic": "^0.0.21"
26
+ },
27
+ "devDependencies": {
28
+ "@types/jest": "^29.5.14",
29
+ "@types/node": "^22.8.4",
30
+ "jest": "^29.7.0",
31
+ "ts-jest": "^29.2.5",
32
+ "typescript": "^5.6.3"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/syncrude/safe/issues"
36
+ },
37
+ "homepage": "https://github.com/syncrude/safe#readme"
38
+ }