Skip to content

Commit

Permalink
expose GlobalRecord instead of SerializableDictionary in renderer com…
Browse files Browse the repository at this point in the history
…ponents
  • Loading branch information
KurtGokhan committed Dec 4, 2023
1 parent 733ca60 commit 573d6d5
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Editor/Renderer/ReactInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public override VisualElement CreateInspectorGUI()

protected abstract ScriptSource GetScript();

protected virtual SerializableDictionary GetGlobals()
protected virtual GlobalRecord GetGlobals()
{
return new SerializableDictionary()
return new GlobalRecord()
{
{ "Inspector", this },
};
Expand Down
4 changes: 2 additions & 2 deletions Editor/Renderer/ReactProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)

protected abstract ScriptSource GetScript();

protected virtual SerializableDictionary GetGlobals(SerializedProperty property)
protected virtual GlobalRecord GetGlobals(SerializedProperty property)
{
return new SerializableDictionary()
return new GlobalRecord()
{
{ "Property", property },
{ "Drawer", this },
Expand Down
4 changes: 2 additions & 2 deletions Editor/Renderer/ReactWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public virtual void Run(VisualElement root = null)

protected abstract ScriptSource GetScript();

protected virtual SerializableDictionary GetGlobals()
protected virtual GlobalRecord GetGlobals()
{
return new SerializableDictionary()
return new GlobalRecord()
{
{ "Window", this },
};
Expand Down
2 changes: 1 addition & 1 deletion Editor/UIToolkit/ReactUnityEditorElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ReactUnityEditorElement : ReactUnityElement
public ReactInspector Inspector { get; internal set; }
public ReactProperty Property { get; internal set; }

public ReactUnityEditorElement(ScriptSource script, SerializableDictionary globals, ITimer timer, IMediaProvider mediaProvider, JavascriptEngineType engineType = JavascriptEngineType.Auto, bool debug = false, bool awaitDebugger = false, bool autorun = false)
public ReactUnityEditorElement(ScriptSource script, GlobalRecord globals, ITimer timer, IMediaProvider mediaProvider, JavascriptEngineType engineType = JavascriptEngineType.Auto, bool debug = false, bool awaitDebugger = false, bool autorun = false)
: base(script, globals, timer, mediaProvider, engineType, debug, awaitDebugger, autorun)
{
}
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Core/ReactContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum UnknownPropertyHandling

public class Options
{
public SerializableDictionary Globals;
public GlobalRecord Globals;
public ScriptSource Source;
public ITimer Timer;
public IMediaProvider MediaProvider;
Expand Down Expand Up @@ -83,7 +83,7 @@ public ReactContext(Options options)
Source = options.Source;
Timer = options.Timer;
Dispatcher = CreateDispatcher();
Globals = GlobalRecord.BindSerializableDictionary(options.Globals, Dispatcher, false);
Globals = options.Globals;
OnRestart = options.OnRestart ?? (() => { });
CalculatesLayout = options.CalculatesLayout;
Location = new Location(this);
Expand Down
11 changes: 9 additions & 2 deletions Runtime/Core/ReactRendererBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ public class ReactAdvancedOptions
public ReactContext Context { get; private set; }
public ITimer Timer { get; set; }

public SerializableDictionary Globals = new SerializableDictionary();
[SerializeField]
[FormerlySerializedAs("Globals")]
protected SerializableDictionary globals = new SerializableDictionary();

public ReactAdvancedOptions AdvancedOptions = new ReactAdvancedOptions();

public GlobalRecord Globals { get; } = new GlobalRecord();

private Coroutine MediaProviderCoroutine;

void OnEnable()
{
Globals.BindSerializableDictionary(globals, true);
if (AdvancedOptions.AutoRender) Render();
}

Expand All @@ -67,7 +73,8 @@ private void OnDestroy()

private void OnValidate()
{
Context?.Globals.UpdateStringObjectDictionary(Globals, true);
Globals.BindSerializableDictionary(globals, true);
Context?.Dispatcher?.OnceUpdate(() => Globals.Change());
}

protected virtual void Clean()
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Frameworks/UIToolkit/General/ReactUnityElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public class ReactUnityElement : VisualElement
public IMediaProvider MediaProvider { get; private set; }

public ScriptSource Script { get; }
public SerializableDictionary Globals { get; }
public GlobalRecord Globals { get; }
public JavascriptEngineType EngineType { get; }

public bool Debug = false;
public bool AwaitDebugger = false;


public ReactUnityElement(ScriptSource script, SerializableDictionary globals, ITimer timer, IMediaProvider mediaProvider, JavascriptEngineType engineType = JavascriptEngineType.Auto, bool debug = false, bool awaitDebugger = false, bool autorun = true)
public ReactUnityElement(ScriptSource script, GlobalRecord globals, ITimer timer, IMediaProvider mediaProvider, JavascriptEngineType engineType = JavascriptEngineType.Auto, bool debug = false, bool awaitDebugger = false, bool autorun = true)
{
Script = script;
Globals = globals;
Expand Down
21 changes: 6 additions & 15 deletions Runtime/Helpers/GlobalRecord.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
using System;
using ReactUnity.Reactive;
using ReactUnity.Scheduling;

namespace ReactUnity.Helpers
{
public class GlobalRecord : ReactiveObjectRecord
{
private System.Action removeStringDictionaryListener;
private IDispatcher dispatcher;
private Action removeStringDictionaryListener;

public GlobalRecord() { }

public static GlobalRecord BindSerializableDictionary(SerializableDictionary dict, IDispatcher dispatcher, bool isSerializing)
{
var res = new GlobalRecord();
res.dispatcher = dispatcher;
res.BindSerializableDictionary(dict, isSerializing);
return res;
}

public void BindSerializableDictionary(SerializableDictionary dict, bool isSerializing)
{
removeStringDictionaryListener?.Invoke();
removeStringDictionaryListener = null;

if (dict == null) return;

UpdateStringObjectDictionary(dict, isSerializing);

removeStringDictionaryListener = dict.AddListener((key, value, dc) => {
Expand All @@ -38,15 +31,13 @@ public void BindSerializableDictionary(SerializableDictionary dict, bool isSeria

public void UpdateStringObjectDictionary(ReactiveRecord<object> dict, bool isSerializing)
{
ClearWithoutNotify();
foreach (var entry in dict)
{
SetWithoutNotify(entry.Key, entry.Value);
if (entry.Value == null) RemoveWithoutNotify(entry.Key);
else SetWithoutNotify(entry.Key, entry.Value);
}

if (!isSerializing) Change(null, default);
else if (dispatcher != null)
dispatcher.OnceUpdate(() => Change(null, default));
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/Editor/Utils/EditorTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using ReactUnity.Editor;
using ReactUnity.Editor.Renderer;
using ReactUnity.Editor.UIToolkit;
using ReactUnity.Helpers;
using ReactUnity.Reactive;
using ReactUnity.Scripting;
using ReactUnity.Styling;
using ReactUnity.Styling.Rules;
Expand All @@ -43,7 +43,7 @@ public abstract class EditorTestBase
protected EditorContext EditorContext => Context as EditorContext;
protected IMediaProvider MediaProvider => Context?.MediaProvider;
protected HostComponent Host => Context?.Host as HostComponent;
protected GlobalRecord Globals => Context?.Globals;
protected ReactiveObjectRecord Globals => Context?.Globals;
internal ReactUnityBridge Bridge => ReactUnityBridge.Instance;

public readonly JavascriptEngineType EngineType;
Expand Down
4 changes: 2 additions & 2 deletions Tests/Editor/Utils/TestReactWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ReactUnity.Tests.Editor
public class TestReactWindow : ReactWindow
{
public Func<ScriptSource> ScriptCallback;
public SerializableDictionary Globals = new SerializableDictionary();
public GlobalRecord Globals = new GlobalRecord();
public override bool AutoRun => false;

public override JavascriptEngineType EngineType { get; set; }
Expand All @@ -31,7 +31,7 @@ protected override ScriptSource GetScript()
return ScriptCallback?.Invoke();
}

protected override SerializableDictionary GetGlobals()
protected override GlobalRecord GetGlobals()
{
Globals["Window"] = this;
return Globals;
Expand Down
15 changes: 15 additions & 0 deletions Tests/Runtime/Base/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ public IEnumerator GlobalsKeysCanBeAccessedNaturally()
Render();
}

[UGUITest(Script = @"
function App() { };
Globals.Set('a', 5);
Assert.AreEqual(5, Globals.a);
", AutoRender = false)]
public IEnumerator GlobalsGetChangedOnCsharpSide()
{
yield return null;
Render();
Assert.AreEqual(Globals, Context.Globals);
Assert.AreEqual(5, Globals["a"]);
Assert.AreEqual(5, Context.Globals["a"]);
}

[UGUITest(Script = @"
function App() { };
Expand Down
4 changes: 2 additions & 2 deletions Tests/Runtime/Utils/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ReactUnity.Helpers;
using ReactUnity.Reactive;
using ReactUnity.Scripting;
using ReactUnity.Styling;
using ReactUnity.Styling.Rules;
Expand Down Expand Up @@ -51,7 +51,7 @@ public abstract class TestBase : InputTestFixture
protected UGUIContext UGUIContext => Context as UGUIContext;
protected IMediaProvider MediaProvider => Context?.MediaProvider;
protected HostComponent Host => Context?.Host as HostComponent;
protected SerializableDictionary Globals => Component?.Globals;
protected ReactiveObjectRecord Globals => Component?.Globals;
internal ReactUnityBridge Bridge => ReactUnityBridge.Instance;

public readonly JavascriptEngineType EngineType;
Expand Down
4 changes: 2 additions & 2 deletions Tests/Runtime/Utils/UIToolkitTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ReactUnity.Helpers;
using ReactUnity.Reactive;
using ReactUnity.Scripting;
using ReactUnity.Styling.Rules;
using ReactUnity.UIToolkit;
Expand Down Expand Up @@ -53,7 +53,7 @@ public abstract class UIToolkitTestBase : InputTestFixture
protected UIToolkitContext UGUIContext => Context as UIToolkitContext;
protected IMediaProvider MediaProvider => Context?.MediaProvider;
protected HostComponent Host => Context?.Host as HostComponent;
protected SerializableDictionary Globals => Component?.Globals;
protected ReactiveObjectRecord Globals => Component?.Globals;
internal ReactUnityBridge Bridge => ReactUnityBridge.Instance;

public readonly JavascriptEngineType EngineType;
Expand Down

0 comments on commit 573d6d5

Please sign in to comment.