rbxts-flamework / core

Flamework is an extensible game framework. It requires typescript and offers many useful features.
MIT License
112 stars 8 forks source link

Add support for yielding in component constructors #57

Open osyrisrblx opened 2 years ago

osyrisrblx commented 2 years ago

Constructors that yield are useful for setting up class members using functions like .WaitForChild(). The component will not setup any lifecycle events until the constructor is complete. This allows for patterns like:

@Component({ tag: "CharacterComponent" })
export class CharacterComponent extends BaseComponent<{}, Model> implements OnPhysics {
    private humanoid: Humanoid;

    constructor() {
        super();

        const humanoid = this.instance.WaitForChild("Humanoid", 5);
        assert(humanoid && humanoid.IsA("Humanoid"));
        this.humanoid = humanoid;
    }

    onPhysics() {
        // use this.humanoid
    }
}

removeComponent would need to wait for the constructor to finish before calling destroy()