Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Updating Descriptor Properties

Firtina Ozbalikci edited this page Oct 20, 2017 · 3 revisions

See Also: Adding a new descriptor#define-property-types-for-typescript

For example, let's add intensity to pointLight

Before, the code for PointLightDescriptor looked like this:

class PointLightDescriptor extends Object3DDescriptorBase<IPointLightProps,
  PointLight> {
 
  constructor() {
    super();
 
    this.hasProp("color", (instance: PointLight,
                           newValue: number | string): void => {
      instance.color.set(newValue as any);
    }, false);
  }
 
  public createInstance(props: IPointLightProps) {
    return new THREE.PointLight(props.color, props.intensity, props.distance, props.decay);
  }
}

In order to add a new property, we can use this.hasProp.

For hasProp, the arguments are:

  • name of property:
    • "intensity"
  • property update function:
    • (instance: PointLight, newValue: number): void => {
        instance.intensity = newValue;
      }
  • whether it should be used right after createInstance (true by default)
    • false, because it's already passed within createInstance above.
  • whether it's a property that should trigger a render (true by default)
    • true
  constructor() {
    super();

    this.hasProp("color", (instance: PointLight,
                           newValue: any): void => {
      instance.color.set(newValue);
    }, false);

+   this.hasProp("intensity", (instance: PointLight,
+                              newValue: number): void => {
+     instance.intensity = newValue;
+   }, false);
  }

We can see that this property is simple (it just does instance[propertyName] = newValue).

This means we can use hasSimpleProp.

For hasSimpleProp, the arguments are:

  • name of property:
    • "intensity"
  • whether it should be used right after createInstance (true by default)
    • false, because it's already passed within createInstance above.
  • whether it's a property that should trigger a render (true by default)
    • true
  constructor() {
    super();

    this.hasProp("color", (instance: PointLight,
                           newValue: any): void => {
      instance.color.set(newValue);
    }, false);

-   this.hasProp("intensity", (instance: PointLight,
-                              newValue: number): void => {
-     instance.intensity = newValue;
-   }, false);
+   this.hasSimpleProp("intensity", false);
  }
Clone this wiki locally