StateType
Defines the shadowing behavior for a property. f.lux uses StateType instances to configure Shader instances that are then used to proxy the store's state using shadow properties.
Property subclasses usually expose a type
'class variable to specify their default shadowing behavior.
The
typeclass variable should be a getter property and is typically attached to the class using the
StateType.defineType()`` function.
Example of defining a type
class variable:
StateType.defineType(TodoRootProperty, type => { type.initialState({}) // empty object initial state .autoshadowOff // do not shadow state values without explicit definitions .properties({ // define sub-properties (just one in this case) todos: TodoCollection.type, // 'todos' is a collection property }) .readonly // prevent reassigning the 'todos' collection (paranoia) .typeName("TodoRootProperty"); // useful for certain diagnostic situations });
Shadowing behavior may be customized by setting the following StateType values:
- autoshadow - shadow implicit state properties using default f.lux property types (default=true)
- defaults - specifies default values for an object's initial state. A default value is applied when the initial state for this property is 'undefined'. (default=undefined)
- element type - StateType used for shadowing each sub-property, such as elements in an array or properties of an object. (default=null)
- jit properties - dynamically extends the 'properties' based on the property's state through a callback function (experimental)
- implementation class - ShadowImpl subclass used to back shadow state property (default=null)
- initial state - the initial property value. The store's state will take precedence and this value is used when the store's state is undefined for the specific property. (default=undefined)
- managed type - StateType instance used by CollectionProperty to specify the property type for each model contained in the collection. Other collection types could utilize the same mechanism. (default=null)
- properties - literal object with name/value pairs where name is the sub-property name and the value is a StateType instance describing the property shadowing behavior (default={})
- shadow class - Shadow subclass used to represent a shadow state property. Built-in implementation classes set a default so only need to set when want to customize parent type default. (default=null)
Methods/properties used when setting up a StateType
description:
addProperty(name, type)
- add a single property type where 'name' is the sub-property name and value is a StateType. Consider using StateType.properties() instead.autoshadow
- turn on auto-shadowing (default)autoshadowOff
- turn off auto-shadowingdefault(state)
- used with ObjectProperty and MapProperty sub-properties to set defaults for values. The default value is only applied if the initial state for this property is undefined. Example usage:stateType.properties({ name: PrimitiveProperty.type.default(null), })
elementType(type)
- StateType for shadowing each sub-property. Regularly used with IndexedProperty and ArrayProperty. Used with ObjectProperty and MapProperty when each value is the same type such as in a dictionary.jitProperties(callback)
- Sets a callback used for dynamically extending properties definition. Callback signature is 'callback(state, parentProperty)' and returns object with name/value pairs where name is the sub-property name and the value is a StateType instance describing the property shadowing behavior. (advanced feature)implementationClass(cls)
- sets the ShadowImpl subclass to be used for backing the shadow state (should not be needed)initialState(state)
- value suitable for the property type: boolean, array, object,...managedType(type)
- StateType for shading managed properties (used with CollectionProperty) Example usage:stateType.managedType(TodoProperty.type)
properties(propTypes)
- add multiple properties using a literal object with property-name/StateType key/value pairs Example usage:stateType.properties({ completed: PrimitiveProperty.type.initialState(false), created: PrimitiveProperty.type.readonly, desc: PrimitiveProperty.type, id: PrimitiveProperty.type.readonly, })
readonly
- set property to readonlyreadonlyOff
- set property to read/write (default unless parent property has set readonly to true since readonly property cascades through the state tree)typeName(name)
- set a label for the type. Useful for debugging situations where shadowing is not acting as expected. Example usage:const root = store.shadow; const rootProperty = root.$$(); console.log(rootProperty.typeName()); console.log(rootProperty.stateType().getTypeName());
Instances of this class are rarely directly created and instead are created using the defineType
static functions of each built-in property class.
Static Method Summary
Static Public Methods | ||
public static |
Convenience class method to avoid using 'new StateType(...)' making chaining on the newly created StateType easier to read. |
|
public static |
defineType(PropertyClass: Property, typeCallback: function): * Sets a |
|
public static |
defineTypeEx(PropClass: Property, ShadowType: Shadow, typeCallback: function, initialState: *) Sets a |
|
public static |
Walks the prototype chain and returns the first StateType returned by a |
|
public static |
implementationClassForProperty(property: Property, defaultClass: ShadowImpl): * Determines the ShadowImpl subclass to use for the property. |
Constructor Summary
Public Constructor | ||
public |
|
Member Summary
Public Members | ||
public get |
autoshadow: StateType: * Shadowing configuration getter to enable auto-shadowing. |
|
public get |
Shadowing configuration getter to disable auto-shadowing. |
|
public get |
Shadowing configuration getter to set the readonly attribute. |
|
public get |
readonlyOff: StateType: * Shadowing configuration getter to disable the readonly attribute. |
Method Summary
Public Methods | ||
public |
accessClass(Cls: *): * |
|
public |
addProperty(name: string, type: StateType): StateType Shadowing configuration method to add a child property. |
|
public |
Creates a shallow copy of this |
|
public |
computeInitialState(): * Generates the initial state for this property and its immediate child properties. |
|
public |
Sets the default property values for child properties. |
|
public |
elementType(type: StateType): StateType Sets the |
|
public |
Gets the descriptive type name from the StateType#_PropertyClass. |
|
public |
implementationClass(cls: *): StateType The ShadowImpl class to use for backing the properties Shadow. |
|
public |
initialState(state: *): StateType Sets the initial state for the property if the f.lux store's state does not have a value for this property. |
|
public |
initialStateWithDefaults(state: *): * Generates the initial state for this property and its immediate child properties. |
|
public |
jitProperties(callback: function): * Sets a callback used for dynamically extending the child property definitions. |
|
public |
managedType(type: StateType): StateType Sets the |
|
public |
properties(propTypes: *): StateType Sets |
|
public |
propertyClass(PropertyClass: Property): StateType Sets the Property class used for shadowing the state property. |
|
public |
shadowClass(cls: Shadow): StateType Sets the Shadow class to be used for shadowing the property. |
|
public |
Sets a descriptive type name on a property class. |
Static Public Methods
public static create(PropertyClass: Property): * source
Convenience class method to avoid using 'new StateType(...)' making chaining on the newly created StateType easier to read.
Return:
* |
public static defineType(PropertyClass: Property, typeCallback: function): * source
Sets a type
getter on a Property class. The type
getter is used to define properties
in the StateType#properties method. The type
getter may be set once per class and all
further requests are ignored.
Example usage:
StateType.defineType(CounterProperty); // and the use it ObjectProperty.createClass({}, type => { type.properties({ counter: CounterProperty.type.readonly, }) });
Return:
* |
public static defineTypeEx(PropClass: Property, ShadowType: Shadow, typeCallback: function, initialState: *) source
Sets a type
getter on a Property class with the ability to set the initial state. The
type
getter is used to define properties in the StateType#properties method. The type
getter may be set once per class and all further requests are ignored.
Params:
Name | Type | Attribute | Description |
PropClass | Property | Property subclass (required) |
|
ShadowType | Shadow |
|
Shadow subclass or will pick up default set by parent class |
typeCallback | function | callback of form |
|
initialState | * |
|
the initial state for the new property. |
public static from(prop: Property): * source
Walks the prototype chain and returns the first StateType returned by a type
class
variable.
public static implementationClassForProperty(property: Property, defaultClass: ShadowImpl): * source
Determines the ShadowImpl subclass to use for the property.
Params:
Name | Type | Attribute | Description |
property | Property | the |
|
defaultClass | ShadowImpl |
|
=ShadowImpl - the |
Return:
* |
Public Constructors
public constructor() source
Public Members
public get autoshadow: StateType: * source
Shadowing configuration getter to enable auto-shadowing. Auto-shadowing is enabled by default and the shadowing attribute does not cascade through the state hieararchy.
public get autoshadowOff: StateType: * source
Shadowing configuration getter to disable auto-shadowing. Auto-shadowing is enabled by default and the shadowing attribute does not cascade through the state hieararchy.
public get readonly: StateType: * source
Shadowing configuration getter to set the readonly attribute. Readonly is disabled by
default and the attribute cascades through the state hieararchy. This means all child
properties will be readonly once an upper level property's readonly
attribute is enabled
until explicitly disabled using StateType#readonlyOff.
public get readonlyOff: StateType: * source
Shadowing configuration getter to disable the readonly attribute. Readonly is disabled by
default and the attribute cascades through the state hieararchy. This means all child
properties will be readonly once an upper level property's readonly
attribute is enabled
until explicitly disabled using StateType#readonlyOff.
Public Methods
public addProperty(name: string, type: StateType): StateType source
Shadowing configuration method to add a child property. The current implementation support 'keyed' properties only, such as ObjectProperty and MapProperty.
Consider using StateType#properties for adding multiple properties.
public computeInitialState(): * source
Generates the initial state for this property and its immediate child properties. This method does not account for any explicit default values set using StateType#default.
Return:
* | the initial state |
public default(state: *): StateType source
Sets the default property values for child properties. A default value is used when the
Store state and the property type
descriptor's initial state are not defined.
Params:
Name | Type | Attribute | Description |
state | * | the default state value |
public elementType(type: StateType): StateType source
Sets the StateType
to use for shadowing each immediate child property.
Params:
Name | Type | Attribute | Description |
type | StateType |
|
public getTypeName(): string source
Gets the descriptive type name from the StateType#_PropertyClass. This value is set using StateType#typeName.
public implementationClass(cls: *): StateType source
The ShadowImpl class to use for backing the properties Shadow. This rarely required.
Params:
Name | Type | Attribute | Description |
cls | * |
public initialState(state: *): StateType source
Sets the initial state for the property if the f.lux store's state does not have a value for this property.
Params:
Name | Type | Attribute | Description |
state | * | the initial value |
public initialStateWithDefaults(state: *): * source
Generates the initial state for this property and its immediate child properties. The intial state is merged with any explicit default values set using StateType#default.
Params:
Name | Type | Attribute | Description |
state | * |
Return:
* | the initial state |
public jitProperties(callback: function): * source
Sets a callback used for dynamically extending the child property definitions. Callback signature is:
callback(state, parentProperty)
where:
state
- the parent property stateparentProperty
- the parent property
Params:
Name | Type | Attribute | Description |
callback | function | the function to call during the shadowing process to obtain any additional property definitions beyond those statically configured. |
Return:
* |
|
public managedType(type: StateType): StateType source
Sets the StateType
for the 'managed type'. Complex Property subclasses use the
managed type for determining how to shadow contained data. More common is the use of
StateType#elementType to specify a StateType
for each immediate child property.
See CollectionProperty for an example of a property employing managed types.
Params:
Name | Type | Attribute | Description |
type | StateType | the |
public properties(propTypes: *): StateType source
Sets StateType
values describing the shadowing behavior for multiple child properties.
Params:
Name | Type | Attribute | Description |
propTypes | * |
public propertyClass(PropertyClass: Property): StateType source
Sets the Property class used for shadowing the state property.
Params:
Name | Type | Attribute | Description |
PropertyClass | Property | a |
public shadowClass(cls: Shadow): StateType source
Sets the Shadow class to be used for shadowing the property.
Params:
Name | Type | Attribute | Description |
cls | Shadow | The |