Skip to content

BaseTween#callbacks type is inconsistent with runtime behaviour #7353

Description

@Bertie690

Version

  • Phaser Version: N/A (Occurs on master)
  • Operating system: N/A
  • Browser: N/A

Description

The type for callbacks is incorrect - instead of a partial object that might have each callback, it's an object that ALWAYS has every single callback (though each may be set to null).

this.callbacks = {
onActive: null,
onComplete: null,
onLoop: null,
onPause: null,
onRepeat: null,
onResume: null,
onStart: null,
onStop: null,
onUpdate: null,
onYoyo: null
};

MOREOVER, the data added by setCallback is NOT a simple lambda function! It's an object containing the extra parameters!

if (this.callbacks.hasOwnProperty(type))
{
this.callbacks[type] = { func: callback, params: params };
}

This is BLATANTLY type-unsafe due to effectively misrepresenting the type's very nature - the declaration file says "record of lambda functions", the code says "record of objects with lambda functions".

Current (incorrect) typing

type TweenCallbacks = {
  /**
   * A function to call when the tween becomes active within the Tween Manager.
   */
  onActive?: Phaser.Types.Tweens.TweenOnActiveCallback;
  /**
   * A function to call when the tween starts playback, after any delays have expired.
   */
  onStart?: Phaser.Types.Tweens.TweenOnStartCallback;
  /**
   * A function to call when the tween completes.
   */
  onComplete?: Phaser.Types.Tweens.TweenOnCompleteCallback;
  /**
   * A function to call each time the tween loops.
   */
  onLoop?: Phaser.Types.Tweens.TweenOnLoopCallback;
  /**
   * A function to call each time the tween is paused.
   */
  onPause?: Phaser.Types.Tweens.TweenOnPauseCallback;
  /**
   * A function to call each time the tween is resumed.
   */
  onResume?: Phaser.Types.Tweens.TweenOnResumeCallback;
  /**
   * A function to call each time the tween repeats. Called once per property per target.
   */
  onRepeat?: Phaser.Types.Tweens.TweenOnRepeatCallback;
  /**
   * A function to call when the tween is stopped.
   */
  onStop?: Phaser.Types.Tweens.TweenOnStopCallback;
  /**
   * A function to call each time the tween steps. Called once per property per target.
   */
  onUpdate?: Phaser.Types.Tweens.TweenOnUpdateCallback;
  /**
   * A function to call each time the tween yoyos. Called once per property per target.
   */
  onYoyo?: Phaser.Types.Tweens.TweenOnYoyoCallback;
};

Example Test Code

const tween = scene.tweens.add({targets: sprite, duration: 400, on});
if (tween.callbacks.onComplete === null) {} // TS says impossible, but actually runs
tween.callbacks.onComplete?.() // CRASHES because it's not a function!

Additional Information

The callbacks themselves should also probably be readonly, given the setCallback function's entire purpose is to modify them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions