Skip to content

Commit

Permalink
Fix indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nadako committed Oct 19, 2023
1 parent 71c0e3b commit 32eb7d8
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 235 deletions.
10 changes: 5 additions & 5 deletions playground-unity/Assets/PoolAndBinding/PoolAndBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ void Awake()
items = new List<PoolAndBinding_Item>();
}

void Start()
{
var globalInt = Observable.State(1);
var nextId = 1;
void Start()
{
var globalInt = Observable.State(1);
var nextId = 1;

addButton.onClick.AddListener(() =>
{
Expand All @@ -55,5 +55,5 @@ void Start()
});

increaseButton.onClick.AddListener(() => globalInt.Value++);
}
}
}
40 changes: 20 additions & 20 deletions playground-unity/Assets/PoolAndBinding/PoolAndBinding_Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ public class PoolAndBinding_Item : MonoBehaviour
{
[SerializeField] TMP_Text label;

public void Init(Observable<string> model)
{
gameObject.RunOnActive(() =>
{
return model.Bind(text =>
{
Debug.Log($"Setting label for {gameObject.name} to: {text}", gameObject);
label.text = text;
});
});
}
public void Init(Observable<string> model)
{
gameObject.RunOnActive(() =>
{
return model.Bind(text =>
{
Debug.Log($"Setting label for {gameObject.name} to: {text}", gameObject);
label.text = text;
});
});
}

public void OnPoolGet()
{
gameObject.SetActive(true);
}
public void OnPoolGet()
{
gameObject.SetActive(true);
}

public void OnPoolRelease()
{
gameObject.ClearOnActiveRuns();
gameObject.SetActive(false);
}
public void OnPoolRelease()
{
gameObject.ClearOnActiveRuns();
gameObject.SetActive(false);
}
}
2 changes: 1 addition & 1 deletion playground-unity/Assets/Scripts/PaginationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PaginationView : MonoBehaviour
[SerializeField] Button nextButton;

public void Init(PaginationModel model)
{
{
var pageIndicatorText = Observable.Auto(() => (model.CurrentPageIndex.Value + 1) + " / " + model.NumPages.Value);
gameObject.DisposeOnDestroy(pageIndicatorText.Bind(text => label.text = text));

Expand Down
4 changes: 2 additions & 2 deletions playground-unity/Assets/Scripts/SampleItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class SampleItem : MonoBehaviour
[SerializeField] TMP_Text text;

public void Init(string data)
{
{
text.text = data;
}
}
}
44 changes: 22 additions & 22 deletions playground/Playground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@

class Playground
{
static async Task Main()
{
var stateA = Observable.State("hello");
var stateB = Observable.State("world");
static async Task Main()
{
var stateA = Observable.State("hello");
var stateB = Observable.State("world");

var o = Observable.Auto(async () =>
{
Console.WriteLine("computing");
var a = stateA.Value;
await Task.Delay(1000);
var b = stateB.Value;
return a + " " + b;
});
var o = Observable.Auto(async () =>
{
Console.WriteLine("computing");
var a = stateA.Value;
await Task.Delay(1000);
var b = stateB.Value;
return a + " " + b;
});

o.Bind(result => Console.WriteLine(result.Status switch
{
AsyncComputeStatus.Loading => "Loading...",
AsyncComputeStatus.Done => "Done: " + result.Result,
AsyncComputeStatus.Failed => "Failed: " + result.Exception,
}));
o.Bind(result => Console.WriteLine(result.Status switch
{
AsyncComputeStatus.Loading => "Loading...",
AsyncComputeStatus.Done => "Done: " + result.Result,
AsyncComputeStatus.Failed => "Failed: " + result.Exception,
}));

await Task.Delay(1500);
await Task.Delay(1500);

stateB.Value = "Dan";
stateB.Value = "Dan";

Process.GetCurrentProcess().WaitForExit();
}
Process.GetCurrentProcess().WaitForExit();
}
}
170 changes: 85 additions & 85 deletions src/TinkState-Unity/Runtime/UnityBatchScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,100 +9,100 @@

namespace TinkState
{
public class UnityBatchScheduler : Scheduler
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Init()
{
Observable.Scheduler = Instance;
PlayerLoopHelper.ModifyPlayerLoop(typeof(PostLateUpdate), typeof(UnityBatchScheduler), OnUpdate);
}
public class UnityBatchScheduler : Scheduler
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Init()
{
Observable.Scheduler = Instance;
PlayerLoopHelper.ModifyPlayerLoop(typeof(PostLateUpdate), typeof(UnityBatchScheduler), OnUpdate);
}

static void OnUpdate()
{
var scheduler = Instance;
if (scheduler.scheduled)
{
scheduler.Progress(0.1f);
}
}
static void OnUpdate()
{
var scheduler = Instance;
if (scheduler.scheduled)
{
scheduler.Progress(0.1f);
}
}

static readonly UnityBatchScheduler Instance = new UnityBatchScheduler();
static readonly UnityBatchScheduler Instance = new UnityBatchScheduler();

List<Schedulable> queue = new List<Schedulable>();
List<Schedulable> nextQueue = new List<Schedulable>();
bool scheduled;
List<Schedulable> queue = new List<Schedulable>();
List<Schedulable> nextQueue = new List<Schedulable>();
bool scheduled;

UnityBatchScheduler() { }
UnityBatchScheduler() { }

public void Schedule(Schedulable schedulable)
{
queue.Add(schedulable);
scheduled = true;
}
public void Schedule(Schedulable schedulable)
{
queue.Add(schedulable);
scheduled = true;
}

void Progress(float maxSeconds)
{
var end = GetTimeStamp() + maxSeconds;
do
{
// to handle the unfortunate case where a binding invocation schedules another one
// we have two queues and swap between them to avoid allocating a new list every time
var currentQueue = queue;
queue = nextQueue;
foreach (var o in currentQueue) o.Run();
currentQueue.Clear();
nextQueue = currentQueue;
}
while (queue.Count > 0 && GetTimeStamp() < end);
void Progress(float maxSeconds)
{
var end = GetTimeStamp() + maxSeconds;
do
{
// to handle the unfortunate case where a binding invocation schedules another one
// we have two queues and swap between them to avoid allocating a new list every time
var currentQueue = queue;
queue = nextQueue;
foreach (var o in currentQueue) o.Run();
currentQueue.Clear();
nextQueue = currentQueue;
}
while (queue.Count > 0 && GetTimeStamp() < end);

if (queue.Count == 0)
{
scheduled = false;
}
}
if (queue.Count == 0)
{
scheduled = false;
}
}

float GetTimeStamp()
{
return Time.realtimeSinceStartup;
}
}
float GetTimeStamp()
{
return Time.realtimeSinceStartup;
}
}

static class PlayerLoopHelper
{
public static void ModifyPlayerLoop(Type where, Type what, PlayerLoopSystem.UpdateFunction updateFunction)
{
var newSystem = new PlayerLoopSystem
{
type = what,
updateDelegate = updateFunction
};
var playerLoop = PlayerLoop.GetCurrentPlayerLoop();
var index = FindLoopSystemIndex(playerLoop.subSystemList, where);
var postLateUpdateSystem = playerLoop.subSystemList[index];
postLateUpdateSystem.subSystemList = AppendLoopSystem(postLateUpdateSystem.subSystemList, newSystem);
playerLoop.subSystemList[index] = postLateUpdateSystem;
PlayerLoop.SetPlayerLoop(playerLoop);
}
static class PlayerLoopHelper
{
public static void ModifyPlayerLoop(Type where, Type what, PlayerLoopSystem.UpdateFunction updateFunction)
{
var newSystem = new PlayerLoopSystem
{
type = what,
updateDelegate = updateFunction
};
var playerLoop = PlayerLoop.GetCurrentPlayerLoop();
var index = FindLoopSystemIndex(playerLoop.subSystemList, where);
var postLateUpdateSystem = playerLoop.subSystemList[index];
postLateUpdateSystem.subSystemList = AppendLoopSystem(postLateUpdateSystem.subSystemList, newSystem);
playerLoop.subSystemList[index] = postLateUpdateSystem;
PlayerLoop.SetPlayerLoop(playerLoop);
}

static PlayerLoopSystem[] AppendLoopSystem(PlayerLoopSystem[] subSystemList, PlayerLoopSystem system)
{
var newSubSystemList = new PlayerLoopSystem[subSystemList.Length + 1];
Array.Copy(subSystemList, 0, newSubSystemList, 0, subSystemList.Length);
newSubSystemList[subSystemList.Length] = system;
return newSubSystemList;
}
static PlayerLoopSystem[] AppendLoopSystem(PlayerLoopSystem[] subSystemList, PlayerLoopSystem system)
{
var newSubSystemList = new PlayerLoopSystem[subSystemList.Length + 1];
Array.Copy(subSystemList, 0, newSubSystemList, 0, subSystemList.Length);
newSubSystemList[subSystemList.Length] = system;
return newSubSystemList;
}

static int FindLoopSystemIndex(PlayerLoopSystem[] subSystemList, Type systemType)
{
for (int i = 0; i < subSystemList.Length; i++)
{
if (subSystemList[i].type == systemType)
{
return i;
}
}
throw new Exception("Target PlayerLoopSystem does not found. Type:" + systemType.FullName);
}
}
static int FindLoopSystemIndex(PlayerLoopSystem[] subSystemList, Type systemType)
{
for (int i = 0; i < subSystemList.Length; i++)
{
if (subSystemList[i].type == systemType)
{
return i;
}
}
throw new Exception("Target PlayerLoopSystem does not found. Type:" + systemType.FullName);
}
}
}
Loading

0 comments on commit 32eb7d8

Please sign in to comment.