Skip to content

Commit

Permalink
refactor(timestep): update INotify impl & event types
Browse files Browse the repository at this point in the history
- rename EVENT_INTEGRATE => EVENT_SUBFRAME
  • Loading branch information
postspectacular committed Jul 25, 2023
1 parent d36aca6 commit 02d83e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
18 changes: 12 additions & 6 deletions packages/timestep/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { EVENT_ALL } from "@thi.ng/api";
import type { TimeStep } from "./timestep";

/**
Expand Down Expand Up @@ -104,14 +105,19 @@ export type StateInterpolation<T> = (
ctx: ReadonlyTimeStep
) => T;

/**
* Event ID for {@link TimeStep.addListener} to be notified of individual
* {@link ITimeStep.integrate} iterations.
*/
export const EVENT_INTEGRATE = "integrate";

/**
* Event ID for {@link TimeStep.addListener} to be notified of calls to
* {@link TimeStep.update} (triggered at the very end of a frame update).
*/
export const EVENT_FRAME = "frame";

/**
* Event ID for {@link TimeStep.addListener} to be notified of individual
* {@link ITimeStep.integrate} iterations.
*/
export const EVENT_SUBFRAME = "subframe";

export type TimeStepEventType =
| typeof EVENT_FRAME
| typeof EVENT_SUBFRAME
| typeof EVENT_ALL;
23 changes: 12 additions & 11 deletions packages/timestep/src/timestep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {
} from "@thi.ng/api";
import {
EVENT_FRAME,
EVENT_INTEGRATE,
EVENT_SUBFRAME,
type ITimeStep,
type ReadonlyTimeStep,
type TimeStepEventType,
type TimeStepOpts,
} from "./api.js";

@INotifyMixin
export class TimeStep implements INotify {
export class TimeStep implements INotify<TimeStepEventType> {
start: number;
dt: number;
maxFrameTime: number;
Expand All @@ -25,8 +26,8 @@ export class TimeStep implements INotify {
frame = 0;
updates = 0;

protected __eventIntegrate: Event;
protected __eventFrame: Event;
protected __eventFrame: Event<TimeStepEventType>;
protected __eventSubFrame: Event<TimeStepEventType>;

constructor(opts?: Partial<TimeStepOpts>) {
const $opts = {
Expand All @@ -40,19 +41,19 @@ export class TimeStep implements INotify {
this.maxFrameTime = $opts.maxFrameTime;
this.scale = $opts.scale;
this.start = $opts.startTime * this.scale;
this.__eventIntegrate = Object.freeze({
id: EVENT_INTEGRATE,
this.__eventFrame = Object.freeze({ id: EVENT_FRAME, target: this });
this.__eventSubFrame = Object.freeze({
id: EVENT_SUBFRAME,
target: this,
});
this.__eventFrame = Object.freeze({ id: EVENT_FRAME, target: this });
}

// @ts-ignore mixin
addListener(id: string, fn: Listener, scope?: any): boolean {}
addListener(id: TimeStepEventType, fn: Listener, scope?: any): boolean {}
// @ts-ignore mixin
removeListener(id: string, fn: Listener, scope?: any): boolean {}
removeListener(id: TimeStepEventType, fn: Listener, scope?: any): boolean {}
// @ts-ignore mixin
notify(event: Event): boolean {}
notify(event: Event<TimeStepEventType>): boolean {}

/**
* Updates internal time to given new time `now` (given value will be scaled
Expand Down Expand Up @@ -83,7 +84,7 @@ export class TimeStep implements INotify {
this.t += dt;
this.accumulator -= dt;
this.updates++;
this.notify(this.__eventIntegrate);
this.notify(this.__eventSubFrame);
}
if (interpolate) {
const alpha = this.accumulator / dt;
Expand Down

0 comments on commit 02d83e8

Please sign in to comment.