Class BindableVariableBase<T>
Abstract class which contains a member variable of type T
and provides a binding API to data changes.
Implements
Inherited Members
Namespace: Unity.XR.CoreUtils .Bindings.Variables
Assembly: Unity.XR.CoreUtils.dll
Syntax
[Serializable]
public abstract class BindableVariableBase<T> : IReadOnlyBindableVariable<T>
Type Parameters
Name | Description |
---|---|
T | The type of the variable value. |
Constructors
BindableVariableBase(T, bool, Func<T, T, bool>, bool)
Constructor for bindable variable, which is a variable that notifies listeners when the internal value changes.
Declaration
protected BindableVariableBase(T initialValue = default, bool checkEquality = true, Func<T, T, bool> equalityMethod = null, bool startInitialized = false)
Parameters
Type | Name | Description |
---|---|---|
T | initialValue | Value to initialize variable with. Defaults to type default. |
bool | checkEquality | Setting true checks whether to compare new value to old before triggering callback. Defaults to true. |
Func<T, T, bool> | equalityMethod | Func used to provide custom equality checking behavior. Defaults to |
bool | startInitialized | Setting false results in initial value setting will trigger registered callbacks, regardless of whether the value is the same as the initial one. Defaults to false. |
See Also
Properties
BindingCount
Get number of subscribed binding callbacks.
Note that if you manually call Unsubscribe(Action<T>) with the same callback several times this value may be inaccurate.
For best results leverage the IEvent
Declaration
public int BindingCount { get; }
Property Value
Type | Description |
---|---|
int |
See Also
Value
The internal variable value. When setting the value, subscribers may be notified. The subscribers will not be notified if this variable is initialized, is configured to check for equality, and the new value is equivalent.
Declaration
public T Value { get; set; }
Property Value
Type | Description |
---|---|
T |
See Also
Methods
BroadcastValue()
Triggers a callback for all subscribed listeners with the current internal variable value.
Declaration
public void BroadcastValue()
See Also
SetValueWithoutNotify(T)
Sets the internal variable value and, even if different, doesn't notify of the change to subscribed listeners. This is intended to be used by developers to allow for setting multiple bindable variables before broadcasting any of them individually.
Declaration
public bool SetValueWithoutNotify(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The new value. |
Returns
Type | Description |
---|---|
bool | Returns true if a broadcast would have normally occurred if the property setter was used instead. Always returns false if no listeners are subscribed. |
See Also
Subscribe(Action<T>)
Register callback to the event that is invoked when the value is updated.
Declaration
public IEventBinding Subscribe(Action<T> callback)
Parameters
Type | Name | Description |
---|---|---|
Action<T> | callback | Callback to register. |
Returns
Type | Description |
---|---|
IEvent |
IEvent |
See Also
SubscribeAndUpdate(Action<T>)
Triggers the callback inline, followed by the Subscribe(Action<T>) function.
Declaration
public IEventBinding SubscribeAndUpdate(Action<T> callback)
Parameters
Type | Name | Description |
---|---|---|
Action<T> | callback | Callback to register. |
Returns
Type | Description |
---|---|
IEvent |
IEvent |
See Also
Task(Func<T, bool>, CancellationToken)
Wait until predicate is met, or until token is called. A null predicate can be passed to have it await any change.
Declaration
public Task<T> Task(Func<T, bool> awaitPredicate, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
Func<T, bool> | awaitPredicate | Callback to be executed on completion of task. |
Cancellation |
token | Token used to trigger a cancellation of the task. |
Returns
Type | Description |
---|---|
Task<T> | Task to schedule. |
See Also
Task(T, CancellationToken)
Wait until BindableVariable is set to awaitState
.
Declaration
public Task<T> Task(T awaitState, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
T | awaitState | Variable state to wait for. |
Cancellation |
token | Token used to trigger a cancellation of the task. |
Returns
Type | Description |
---|---|
Task<T> | Task to schedule. |
See Also
Unsubscribe(Action<T>)
Manually unsubscribe callback from Value update event, but no protections from multiple unsubscribe calls. If unsubscribing multiple times, reference count may not be accurate.
Declaration
public void Unsubscribe(Action<T> callback)
Parameters
Type | Name | Description |
---|---|---|
Action<T> | callback | Callback to unregister. |
See Also
ValueEquals(T)
Evaluates equality with the internal value held by the bindable variable.
Declaration
public virtual bool ValueEquals(T other)
Parameters
Type | Name | Description |
---|---|---|
T | other | Other value to compare equality against. |
Returns
Type | Description |
---|---|
bool | True if both values are equal. |