Skip to main content

Derivables

Table of Contents

  1. Overview
  2. Imports and Dependencies
  3. Classes and Interfaces
  4. Methods and Functionality
  5. Usage (Additional)

Derivables - Derived Properties

Derivables extension will listen to source properties changes and update the derived property's value. Usually, derived properties are also non-serializable (since it can be derived from data), therefore you can apply Ephemerals on it.

class DerivedTestClass {
strVal = 'string';
numVal = 5;
derivedProp: string; /* DERIVED */
derivedProp2: string; /* DERIVED EPHEM */
constructor(init?: Partial<DerivedTestClass>) {
defineOn(this, DerivedTestClass, lib => {
lib.derivables.of(this, { /* options */ }, {
derivedProp: (strVal, numVal) => strVal + numVal, // short format
derivedProp2: { // long format, functionally the same as above (a little fatster in terms of perf)
from: { strVal:1, numVal:1 },
derive() { return this.strVal + this.numVal; }
}
...
lib.ephemerals.of(this, { derivedProp2: true, ...
}
}

const d = new DerivedTestClass({ strVal: 'joined', numVal: 5 }));
console.log( d.derivedProp ); // 'joined5'
d.strVal = 'joined2_'; // updated source
d.numVal = 10;
console.log( d.derivedProp ); // 'joinedUpdated_10' automatically updated
console.log( JSON.stringify(d) ); // derivedProp2 not saved: {"strVal":"joined2_","numVal":10,"derivedProp":"joined2_10"}

Classes and Interfaces

DerivablesSettings

DerivablesSettings is a class that extends PropertiesControllerSettings. It provides settings specific to derivable properties.

  • Static Properties:

    • extensionDerivables: A string identifier for this extension, defaulting to 'Derivables'.
  • Constructor:

    • Accepts an optional initial configuration and assigns any provided settings.

DerivablesMetadata

An interface for metadata associated with derivable properties.

  • from: Specifies the properties from which the derivation occurs.
  • derive: A function that performs the derivation.

DerivablesOptions

An interface to specify options for derivable properties.

  • derive: A boolean indicating if derivation should take place.

DerivablesExtensionData

A class implementing TypeToolsExtensionData, it contains:

  • rubric: Object mapping property names to their derivation logic.
  • triggers: Maps property names to lists of dependent properties and guards against repeated derivations.

Derivables

The core class implementing the derivable properties mechanism, extends functionality via TypeToolsExtension.

  • Static Methods:

    • getExtensionData: Retrieves extension data for a target.
    • typeCheck: Checks if a target has required derivable properties.
    • implementOn: Implements derivable properties on a target.
    • of: Sets up derivable properties according to the rubric provided.
  • Constructor: Initializes with optional settings.


Methods and Functionality

getExtensionData

Retrieves extension data related to derivable properties from a given target.

typeCheck

Validates if a given target can support derivable properties based on its settings.

implementOn

Adds derivable functionality to a target class, ensuring it can handle property changes and derivations appropriately.

of

Configures derivable properties on a target using a provided rubric that defines how each property should be derived. It:

  • Checks for circular dependencies with a maximum iteration limit.
  • Initializes derived properties.