I am looking for a way to efficiently save time by reusing common code across classes that extend PIXI classes within a 2D WebGL renderer library.
Object Interfaces:
module Game.Core {
export interface IObject {}
export interface IManagedObject extends IObject{
getKeyInManager(key: string): string;
setKeyInManager(key: string): IObject;
}
}
The challenge I face is that the code inside getKeyInManager
and setKeyInManager
is static and I wish to avoid duplication. Here is the current implementation:
export class ObjectThatShouldAlsoBeExtended{
private _keyInManager: string;
public getKeyInManager(key: string): string{
return this._keyInManager;
}
public setKeyInManager(key: string): DisplayObject{
this._keyInManager = key;
return this;
}
}
My goal is to automatically add, through Manager.add()
, the key used in the manager as a reference within the object itself in its property _keyInManager
.
For example, let's consider a Texture. Here is the TextureManager
:
module Game.Managers {
export class TextureManager extends Game.Managers.Manager {
public createFromLocalImage(name: string, relativePath: string): Game.Core.Texture{
return this.add(name, Game.Core.Texture.fromImage("/" + relativePath)).get(name);
}
}
}
When using this.add()
, I want the Game.Managers.Manager
add()
method to invoke a method on the object returned by
Game.Core.Texture.fromImage("/" + relativePath)
. This object, in this instance, would be a Texture
:
module Game.Core {
// Must extend PIXI.Texture, but need to inject methods from IManagedObject.
export class Texture extends PIXI.Texture {
}
}
Despite knowing that IManagedObject
is an interface and cannot contain implementation, I am unsure how to incorporate the class ObjectThatShouldAlsoBeExtended
into my Texture
class. The same process would apply to Sprite
, TilingSprite
, Layer
, and other classes.
I seek advice from experienced TypeScript developers on accomplishing this task. While multiple extends are not viable due to limitations, there must be a solution available that I have yet to discover.