-
-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: it runs in the browser!!! (#7586)
- Loading branch information
Showing
31 changed files
with
1,098 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
export class Timer { | ||
readonly #callback: (...args: any[]) => void; | ||
readonly #delay?: number; | ||
readonly #args: any[]; | ||
|
||
readonly #inner: NodeJS.Timeout; | ||
readonly #kind = "timeout"; | ||
|
||
/** @internal */ | ||
constructor( | ||
callback: (...args: any[]) => void, | ||
delay?: number, | ||
...args: any[] | ||
) { | ||
this.#callback = callback; | ||
this.#delay = delay; | ||
this.#args = args; | ||
this.#inner = globalThis.setTimeout(callback, delay, ...args); | ||
} | ||
|
||
/** Clears the timeout. */ | ||
public clear(): void { | ||
globalThis.clearTimeout(this.#inner); | ||
} | ||
|
||
public unref(): this { | ||
// Not supported in browsers | ||
if (typeof this.#inner.unref === "function") { | ||
this.#inner.unref(); | ||
} | ||
return this; | ||
} | ||
|
||
public refresh(): this { | ||
if (typeof this.#inner.refresh === "function") { | ||
this.#inner.refresh(); | ||
} else { | ||
globalThis.clearTimeout(this.#inner); | ||
globalThis.setTimeout(this.#callback, this.#delay, ...this.#args); | ||
} | ||
return this; | ||
} | ||
} | ||
|
||
export class Interval { | ||
readonly #inner: NodeJS.Timeout; | ||
readonly #kind = "interval"; | ||
|
||
/** @internal */ | ||
constructor(inner: NodeJS.Timeout) { | ||
this.#inner = inner; | ||
} | ||
|
||
/** Clears the timeout. */ | ||
public clear(): void { | ||
globalThis.clearInterval(this.#inner); | ||
} | ||
|
||
public unref(): this { | ||
// Not supported in browsers | ||
if (typeof this.#inner.unref === "function") { | ||
this.#inner.unref(); | ||
} | ||
return this; | ||
} | ||
} | ||
|
||
export function setTimer<TArgs extends any[]>( | ||
callback: (...args: TArgs) => void, | ||
delay?: number, | ||
...args: TArgs | ||
): Timer { | ||
return new Timer( | ||
callback, | ||
delay, | ||
...args, | ||
); | ||
} | ||
|
||
export function setInterval<TArgs extends any[]>( | ||
callback: (...args: TArgs) => void, | ||
delay?: number, | ||
...args: TArgs | ||
): Interval { | ||
return new Interval(globalThis.setInterval(callback, delay, ...args)); | ||
} |
Oops, something went wrong.