From c8778ffe7014d29acf35dfca970ad94901919db3 Mon Sep 17 00:00:00 2001 From: Ilya Date: Sun, 20 Aug 2023 22:47:52 +0300 Subject: [PATCH] Allow to start multiple push tasks --- package.json | 2 +- src/index.d.ts | 14 +++++++++++++- src/index.js | 22 +++++++++++++++------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 4728532..d3e840e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uptimekuma-api", - "version": "1.0.5", + "version": "1.0.6", "description": "", "main": "src/index.js", "types": "src/index.d.ts", diff --git a/src/index.d.ts b/src/index.d.ts index 5827552..62a87c6 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,4 +1,8 @@ export default class UptimeKumaApi { + /** + * Creates a new instance of the UptimeKumaApi class + * @param baseURL + */ constructor(baseURL: string); /** @@ -8,12 +12,20 @@ export default class UptimeKumaApi { */ startPushing(code: string, interval?: number); - cancelPushing(); + /** + * Stops pushing heartbeats to the server + * @param code The monitor code, if not specified, all monitors will be stopped + */ + cancelPushing(code?: string); on(event: "pushSuccessful", handle: (url: string) => void); on(event: "pushFailed", handle: (url: string, err: Error) => void); on(event: "prePush", handle: (url: string, params: {status:"up"|"down",msg:string,ping?:number}) => void); + /** + * Gets the status of public monitors + * @param name The name of target status page + */ status(name?:string): Promise<[{ id: number, name: string, diff --git a/src/index.js b/src/index.js index 1e0e9a1..e15de42 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ const EventEmitter = require("events"); module.exports = class UptimeKumaApi extends EventEmitter { - _pushTimer; + _pushTimers = {}; _baseURL; constructor(baseURL = "") { @@ -22,17 +22,25 @@ module.exports = class UptimeKumaApi extends EventEmitter { } startPushing(code, interval = 60) { - if (this._pushTimer) - this._pushTimer.cancel(); - this._pushTimer = setInterval(() => { + if (this._pushTimers[code]) + this._pushTimers[code].cancel(); + this._pushTimers[code] = setInterval(() => { this.push(this._baseURL+"api/push/"+code); }, interval * 1000); this.push(this._baseURL+"api/push/"+code); } - cancelPushing() { - if (this._pushTimer) - this._pushTimer.cancel(); + cancelPushing(code = undefined) { + if(code) { + if (this._pushTimers[code]) + this._pushTimers[code].cancel(); + } else { + for (let code in this._pushTimers) { + if (this._pushTimers.hasOwnProperty(code)) { + this._pushTimers[code].cancel(); + } + } + } } async status(name = "default") {