Skip to content

Commit

Permalink
refactor: split ValueReference.SetValue into smaller methods
Browse files Browse the repository at this point in the history
Just to decrease the cognitive complexity.
  • Loading branch information
Hertzole committed Jan 17, 2024
1 parent c651619 commit e0b188e
Showing 1 changed file with 49 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private T GetValue()
}
}

private void SetValue(T value, bool notify)
private void SetValue(in T value, in bool notify)
{
T previousValue = Value;
if (EqualityHelper.Equals(previousValue, value))
Expand All @@ -192,46 +192,63 @@ private void SetValue(T value, bool notify)
switch (valueType)
{
case ValueReferenceType.Constant:
if (notify)
{
OnValueChangingInternal?.Invoke(previousValue, value);
}
constantValue = value;

if (notify)
{
OnValueChangedInternal?.Invoke(previousValue, value);
}
SetValueConstant(value, previousValue, notify);
break;
case ValueReferenceType.Reference:
if (notify)
{
referenceValue.Value = value;
}
else
{
referenceValue.SetValueWithoutNotify(value);
}
SetValueReference(value, notify);
break;
#if SCRIPTABLE_VALUES_ADDRESSABLES
case ValueReferenceType.Addressable:
if (AssetHandle.IsValid() && AssetHandle.IsDone && AssetHandle.Result != null)
{
if (notify)
{
AssetHandle.Result.Value = value;
}
else
{
AssetHandle.Result.SetValueWithoutNotify(value);
}
}

SetValueAddressable(value, notify);
break;
#endif
}
}


private void SetValueConstant(in T value, in T previousValue, in bool notify)
{
if (notify)
{
OnValueChangingInternal?.Invoke(previousValue, value);
}

constantValue = value;

if (notify)
{
OnValueChangedInternal?.Invoke(previousValue, value);
}
}

private void SetValueReference(in T value, in bool notify)
{
if (notify)
{
referenceValue.Value = value;
}
else
{
referenceValue.SetValueWithoutNotify(value);
}
}

#if SCRIPTABLE_VALUES_ADDRESSABLES
private void SetValueAddressable(in T value, in bool notify)
{
if (AssetHandle.IsValid() && AssetHandle.IsDone && AssetHandle.Result != null)
{
if (notify)
{
AssetHandle.Result.Value = value;
}
else
{
AssetHandle.Result.SetValueWithoutNotify(value);
}
}
}
#endif

public void SetValueWithoutNotify(T value)
{
SetValue(value, false);
Expand Down

0 comments on commit e0b188e

Please sign in to comment.