Skip to content

Commit

Permalink
feat(shared): Add alt.Timers.isValid static method
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Sep 21, 2024
1 parent df3923f commit ec85d9b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions shared/js/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ class Timer {
return timers.get(id) || null;
}

/**
*
* @param {number | Timer} idOrHandle
*/
static isValid(idOrHandle) {
if (!idOrHandle) return false;

const id = idOrHandle instanceof Timer ? idOrHandle.id : idOrHandle;
return timers.has(id);
}

constructor(type, callback, interval, once, args) {
assertIsType(type, "number", "Expected a number as first argument");
assertIsType(callback, "function", "Expected a function as second argument");
Expand Down Expand Up @@ -171,6 +182,7 @@ alt.Timers.EveryTick = EveryTick;
alt.Timers.NextTick = NextTick;

alt.Timers.getByID = Timer.getByID;
alt.Timers.isValid = Timer.isValid;

alt.Timers.setInterval = (callback, interval, ...args) => new Interval(callback, interval, ...args);
alt.Timers.setTimeout = (callback, timeout, ...args) => new Timeout(callback, timeout, ...args);
Expand Down
1 change: 1 addition & 0 deletions types/shared/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ declare module "@altv/shared" {
export function timeEnd(name?: string): void;

export function getByID(id: number): Timer | null;
export function isValid(idOrHandle?: number | Timer): boolean;
}

// DO NOT TOUCH THIS - This is only here so client / server can extend Utils namespace using merging
Expand Down
2 changes: 1 addition & 1 deletion types/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@altv/shared",
"version": "0.0.22",
"version": "0.0.23",
"description": "This package contains the type definitions for the alt:V JS module v2 shared types",
"types": "index.d.ts",
"files": [
Expand Down

0 comments on commit ec85d9b

Please sign in to comment.