Skip to content

Commit

Permalink
feat: removed "events" dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
yohgen committed Jul 21, 2023
1 parent 76bc2b8 commit a76c133
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 74 deletions.
64 changes: 64 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export declare const PerfEventType = "perf";
/** Event triggered when performance ratio (`this.frameDuration / averageDeltaTime`) is updated. Understand a ratio of the fps, for instance for a fps value of 24, `ratio < 0.5` means that the averaged `fps < 12` and you should probably do something about it. */
declare class PerfEvent extends Event {
/** The performance ratio of frame duration against average delta time. */
ratio: number;
constructor();
}
export declare const TickEventType = "tick";
/** Event triggered on tick, throttled by `options.fps`. */
declare class TickEvent extends Event {
/** The delta since previous frame. */
timeDelta: number;
constructor();
}
type RafPerfEvent = PerfEvent | TickEvent;

export type OptionsPerformances = {
/** Evaluate performances. */
enabled: boolean;
/** Number of samples to evaluate performances. */
samplesCount: number;
/** Duration of sample to evaluate performances. */
sampleDuration: number;
};
export type Options = {
/** Throttle fps. */
fps: number;
/** Performances metrics. */
performances: OptionsPerformances;
};

/** Creates an instance of RafPerf. */
export default class RafPerf extends EventTarget {
static now: () => number;
static fpsToMs(value: number): number;
static PerfEvent: PerfEvent;
static TickEvent: TickEvent;
options: Options;
isVisible?: boolean;
running?: boolean;
prevTime?: number | null;
startTime?: number | null;
frameDuration?: number;
performance?: number;
perfSamples?: number[];
requestID?: number;
perfStartTime?: number;
constructor(options?: Partial<Options>);
reset(): void;
/** Run the `requestAnimationFrame` loop and start checking performances if `options.performances.enabled` is `true`. */
start(): void;
/** The frame loop callback. Fires {@link PerfEvent} and {@link TickEvent}. */
tick(): void;
/** Run `cancelAnimationFrame` if necessary and reset the engine. */
stop(): void;
onVisibilityChange(): void;
dispatchEvent(e: RafPerfEvent): boolean;
addEventListener<T extends RafPerfEvent['type'], TEvent extends RafPerfEvent & {
type: T;
}>(type: T, listener: ((e: TEvent) => void) | EventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
removeEventListener<T extends RafPerfEvent['type'], TEvent extends RafPerfEvent & {
type: T;
}>(type: T, listener: ((e: TEvent) => void) | EventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
}
86 changes: 32 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import EventEmitter from "events";

/**
* @typedef {Object} Options
* @property {number} [fps=60] Throttle fps.
* @property {OptionsPerformances} [performances={ enabled: true, samplesCount: 200, sampleDuration: 4000 }] Performances metrics.
*/

/**
* @typedef {Object} OptionsPerformances
* @property {boolean} [enabled=false] Evaluate performances.
* @property {number} [samplesCount=200] Number of samples to evaluate performances.
* @property {number} [sampleDuration=200] Duration of sample to evaluate performances.
*/

class RafPerf extends EventEmitter {
/**
* Creates an instance of RafPerf.
*
* @param {Options} [options={}]
* `samplesCount` and `sampleDuration` are used concurrently. Set `sampleDuration` to a _falsy_ value if you only want to sample performances from a number of frames.
*/
export const PerfEventType = 'perf';
/** @type {import('./index').PerfEvent} */
class PerfEvent extends Event {
constructor() {
super(PerfEventType);
this.ratio = 0;
}
}

export const TickEventType = 'tick';
/** @type {import('./index').TickEvent} */
class TickEvent extends Event {
constructor() {
super(TickEventType);
this.timeDelta = 0;
}
}

/** @type {import('./index').default} */
export default class RafPerf extends EventTarget {
constructor(options = {}) {
super();

Expand Down Expand Up @@ -57,9 +55,6 @@ class RafPerf extends EventEmitter {
if (this.requestID) cancelAnimationFrame(this.requestID);
}

/**
* Run the `requestAnimationFrame` loop and start checking performances if `options.performances.enabled` is `true`.
*/
start() {
// Check if loop is already running
if (this.running) return;
Expand All @@ -81,12 +76,6 @@ class RafPerf extends EventEmitter {
this.requestID = requestAnimationFrame(this.tick);
}

/**
* The frame loop callback.
*
* @fires RafPerf#perf
* @fires RafPerf#tick
*/
tick() {
// Ensure loop is running
if (!this.running || !this.isVisible) return;
Expand Down Expand Up @@ -119,13 +108,8 @@ class RafPerf extends EventEmitter {
this.perfSamples.length;
this.performance = this.frameDuration / averageDeltaTime;

/**
* Event triggered when performance ratio (`this.frameDuration / averageDeltaTime`) is updated. Understand a ratio of the fps, for instance for a fps value of 24, `ratio < 0.5` means that the averaged `fps < 12` and you should probably do something about it.
*
* @event RafPerf#perf
* @type {number} The performance ratio of frame duration against average delta time.
*/
this.emit("perf", this.performance);
RafPerf.PerfEvent.ratio = this.performance;
this.dispatchEvent(RafPerf.PerfEvent);

// Reset performances variables
this.perfSamples = [];
Expand All @@ -138,21 +122,13 @@ class RafPerf extends EventEmitter {
this.prevTime = time - (deltaTime % this.frameDuration);
this.startTime = time;

/**
* Event triggered on tick, throttled by `options.fps`.
*
* @event RafPerf#tick
* @type {number} The delta since previous frame.
*/
this.emit("tick", frameDeltaTime);
RafPerf.TickEvent.timeDelta = frameDeltaTime;
this.emit(RafPerf.TickEvent);
}

this.requestID = requestAnimationFrame(this.tick);
}

/**
* Run `cancelAnimationFrame` if necessary and reset the engine.
*/
stop() {
document.removeEventListener(
"visibilitychange",
Expand All @@ -174,12 +150,14 @@ class RafPerf extends EventEmitter {
}

// Static
RafPerf.now = () => {
RafPerf.now = function now() {
return (performance || Date).now();
};
}

RafPerf.fpsToMs = function fpsToMs(value) {
return 1000 / value;
}

RafPerf.fpsToMs = (value) => {
return (1 / value) * 1000;
};
RafPerf.PerfEvent = new PerfEvent();

export default RafPerf;
RafPerf.TickEvent = new TickEvent();
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
"type": "module",
"exports": "./index.js",
"main": "index.js",
"types": "types/index.d.ts",
"dependencies": {
"events": "^3.3.0"
},
"types": "index.d.ts",
"devDependencies": {
"canvas-context": "^3.0.1",
"es-module-shims": "^0.10.3"
Expand Down

0 comments on commit a76c133

Please sign in to comment.