Skip to content

Commit

Permalink
feat: SetValueWithoutNotify to ValueReference
Browse files Browse the repository at this point in the history
  • Loading branch information
Hertzole committed Nov 16, 2023
1 parent a0daafd commit 9ce631c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ValueReference<T>
public T Value
{
get { return GetValue(); }
set { SetValue(value); }
set { SetValue(value, true); }
}

internal event ScriptableValue<T>.OldNewValue<T> OnValueChangingInternal;
Expand Down Expand Up @@ -181,7 +181,7 @@ private T GetValue()
}
}

private void SetValue(T value)
private void SetValue(T value, bool notify)

Check warning on line 184 in Packages/se.hertzole.scriptable-values/Runtime/Values/ValueReference.cs

View workflow job for this annotation

GitHub Actions / SonarScan

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

Check warning on line 184 in Packages/se.hertzole.scriptable-values/Runtime/Values/ValueReference.cs

View workflow job for this annotation

GitHub Actions / SonarScan

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.
{
T previousValue = Value;
if (EqualityHelper.Equals(previousValue, value))
Expand All @@ -192,24 +192,50 @@ private void SetValue(T value)
switch (valueType)
{
case ValueReferenceType.Constant:
OnValueChangingInternal?.Invoke(previousValue, value);
if (notify)
{
OnValueChangingInternal?.Invoke(previousValue, value);
}
constantValue = value;
OnValueChangedInternal?.Invoke(previousValue, value);

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

break;
#endif
}
}

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

#if UNITY_EDITOR
internal void SetPreviousValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,79 @@ public void InvalidType_Set()
Assert.IsTrue(gotError);
}

[Test]
public void SetValueWithoutNotify_Constant()
{
bool eventInvoked = false;

ValueReference<TValue> instance = new ValueReference<TValue>(default(TValue));
instance.OnValueChanged += ValueChanged;

instance.SetValueWithoutNotify(MakeDifferentValue(default));

Assert.AreEqual(MakeDifferentValue(default), instance.constantValue);
Assert.AreEqual(MakeDifferentValue(default), instance.Value);
Assert.IsFalse(eventInvoked);

eventInvoked = false;

instance.OnValueChanged -= ValueChanged;

TValue differentValue = MakeDifferentValue(instance.Value);

instance.SetValueWithoutNotify(differentValue);

Assert.AreEqual(differentValue, instance.constantValue);
Assert.AreEqual(differentValue, instance.Value);
Assert.IsFalse(eventInvoked);

void ValueChanged(TValue previousValue, TValue newValue)
{
Assert.AreEqual(MakeDifferentValue(default), previousValue);
Assert.AreEqual(MakeDifferentValue(default), newValue);
eventInvoked = true;
}
}

[Test]
public void SetValueWithoutNotify_Reference()
{
bool eventInvoked = false;

TType scriptableValue = CreateInstance<TType>();
scriptableValue.Value = default;

ValueReference<TValue> instance = new ValueReference<TValue>(scriptableValue);
instance.OnValueChanged += ValueChanged;

instance.SetValueWithoutNotify(MakeDifferentValue(default));

Assert.AreEqual(default, instance.constantValue);
Assert.AreEqual(MakeDifferentValue(default), instance.Value);
Assert.AreEqual(MakeDifferentValue(default), scriptableValue.Value);
Assert.IsFalse(eventInvoked);

eventInvoked = false;

instance.OnValueChanged -= ValueChanged;

TValue differentValue = MakeDifferentValue(instance.Value);

instance.SetValueWithoutNotify(differentValue);

Assert.AreEqual(default, instance.constantValue);
Assert.AreEqual(differentValue, instance.Value);
Assert.AreEqual(differentValue, scriptableValue.Value);
Assert.IsFalse(eventInvoked);

void ValueChanged(TValue previousValue, TValue newValue)
{
Assert.AreEqual(MakeDifferentValue(default), previousValue);
Assert.AreEqual(MakeDifferentValue(default), newValue);
eventInvoked = true;
}
}

protected abstract TValue MakeDifferentValue(TValue value);
}
}

0 comments on commit 9ce631c

Please sign in to comment.