Skip to content

Commit 155419e

Browse files
committed
Added
1 parent 45b40dc commit 155419e

File tree

7 files changed

+135
-8
lines changed

7 files changed

+135
-8
lines changed

Runtime/Loading/Process.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace Juce.CoreUnity.Loading.Process
6+
{
7+
public interface ILoadingProcess
8+
{
9+
event Action OnCompleted;
10+
11+
ILoadingProcess NewChild();
12+
13+
Task StartLoading(CancellationToken cancellationToken);
14+
Task CompletedLoading(CancellationToken cancellationToken);
15+
}
16+
}

Runtime/Loading/Process/ILoadingProcess.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace Juce.CoreUnity.Loading.Process
7+
{
8+
public sealed class LoadingProcess : ILoadingProcess
9+
{
10+
private readonly IReadOnlyList<Func<CancellationToken, Task>> beforeLoad = Array.Empty<Func<CancellationToken, Task>>();
11+
private readonly IReadOnlyList<Func<CancellationToken, Task>> afterLoad = Array.Empty<Func<CancellationToken, Task>>();
12+
13+
public event Action OnCompleted;
14+
15+
private LoadingProcess()
16+
{
17+
18+
}
19+
20+
private LoadingProcess(
21+
IReadOnlyList<Func<CancellationToken, Task>> beforeLoad,
22+
IReadOnlyList<Func<CancellationToken, Task>> afterLoad
23+
)
24+
{
25+
this.beforeLoad = beforeLoad;
26+
this.afterLoad = afterLoad;
27+
}
28+
29+
public static ILoadingProcess New(
30+
IReadOnlyList<Func<CancellationToken, Task>> beforeLoad,
31+
IReadOnlyList<Func<CancellationToken, Task>> afterLoad
32+
)
33+
{
34+
return new LoadingProcess(beforeLoad, afterLoad);
35+
}
36+
37+
public ILoadingProcess NewChild()
38+
{
39+
return new LoadingProcess();
40+
}
41+
42+
public async Task StartLoading(CancellationToken cancellationToken)
43+
{
44+
foreach(Func<CancellationToken, Task> before in beforeLoad)
45+
{
46+
await before?.Invoke(cancellationToken);
47+
}
48+
}
49+
50+
public async Task CompletedLoading(CancellationToken cancellationToken)
51+
{
52+
foreach (Func<CancellationToken, Task> after in afterLoad)
53+
{
54+
await after?.Invoke(cancellationToken);
55+
}
56+
57+
OnCompleted?.Invoke();
58+
}
59+
}
60+
}

Runtime/Loading/Process/LoadingProcess.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using Juce.Core.Loading.Process;
2+
using System;
3+
using System.Threading;
4+
using System.Threading.Tasks;
25

36
namespace Juce.CoreUnity.Loading.Services
47
{
58
public interface ILoadingService
69
{
7-
bool IsLoading { get; }
10+
void AddBeforeLoading(Func<CancellationToken, Task> func);
11+
void AddAfterLoading(Func<CancellationToken, Task> func);
812

9-
bool TryStartLoading(out ILoadingProcess loadingProcess);
13+
bool TryGetNewProcess(out ILoadingProcess loadingProcess);
1014
}
1115
}
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
using Juce.Core.Loading.Process;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading;
5+
using System.Threading.Tasks;
26

37
namespace Juce.CoreUnity.Loading.Services
48
{
59
public class LoadingService : ILoadingService
610
{
7-
public bool IsLoading { get; private set; }
11+
private readonly List<Func<CancellationToken, Task>> beforeLoad = new List<Func<CancellationToken, Task>>();
12+
private readonly List<Func<CancellationToken, Task>> afterLoad = new List<Func<CancellationToken, Task>>();
813

9-
public bool TryStartLoading(out ILoadingProcess loadingProcess)
14+
private bool isLoading;
15+
16+
public void AddAfterLoading(Func<CancellationToken, Task> func)
17+
{
18+
beforeLoad.Add(func);
19+
}
20+
21+
public void AddBeforeLoading(Func<CancellationToken, Task> func)
22+
{
23+
afterLoad.Add(func);
24+
}
25+
26+
public bool TryGetNewProcess(out ILoadingProcess loadingProcess)
1027
{
11-
if(IsLoading)
28+
if(isLoading)
1229
{
1330
loadingProcess = default;
1431
return false;
1532
}
1633

17-
IsLoading = true;
34+
isLoading = true;
1835

19-
loadingProcess = LoadingProcess.New();
36+
loadingProcess = LoadingProcess.New(beforeLoad, afterLoad);
2037

2138
loadingProcess.OnCompleted += OnLoadingProcessCompleted;
2239

@@ -25,7 +42,7 @@ public bool TryStartLoading(out ILoadingProcess loadingProcess)
2542

2643
private void OnLoadingProcessCompleted()
2744
{
28-
IsLoading = false;
45+
isLoading = false;
2946
}
3047
}
3148
}

0 commit comments

Comments
 (0)