Skip to content

Commit d4f8cc0

Browse files
Merge pull request #9 from tanitaka-tech/release/v2.0.0
Release/v2.0.0
2 parents 6dbde72 + e9b0733 commit d4f8cc0

14 files changed

Lines changed: 215 additions & 55 deletions

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,62 @@ await ConcurrentProcess.Create(
8484
.LoopProcessAsync(cancellationToken: ct);
8585
```
8686

87-
# Logging
87+
## LiteRequestBroker
88+
89+
LiteRequestBroker is a class that can be used to send requests without using the RequestHandler class.
90+
91+
It is good to use in the following cases:
92+
- The request does not have any parameters
93+
- If the request is not being waited simultaneously
94+
95+
### ① Bind LiteRequestBroker
96+
```cs
97+
// ----- In some installer
98+
99+
var liteRequestBroker = new LiteRequestBroker();
100+
Container.BindInstance<ILiteRequestPusher>(liteRequestBroker);
101+
Container.BindInstance<ILiteRequestConsumer>(liteRequestBroker);
102+
```
103+
104+
### ② Push Request (R3 Example)
105+
```cs
106+
// ----- In some object
107+
[SerializeField] private Button _closeButton;
108+
[Inject] private ILiteRequestPusher _liteRequestPusher;
109+
private void Start()
110+
{
111+
_closeButton.OnClickAsObservable()
112+
// I reccommend defining a const string for each request
113+
.Subscribe(_ => _liteRequestPusher.PushRequest("CloseRequest"))
114+
.AddTo(this);
115+
}
116+
```
117+
118+
### ③ Wait Request
119+
```cs
120+
await ConcurrentProcess.Create(
121+
Process.Create(
122+
waitTask: async ct => await LiteRequestConcumer.WaitRequestAndConsumeAsync("CloseRequest", ct),
123+
onPassedTask: async ct =>
124+
{
125+
await CloseAsync(ct);
126+
return ProcessContinueType.Break;
127+
}),
128+
129+
// ...
130+
)
131+
.LoopProcessAsync(cancellationToken: ct);
132+
```
133+
134+
135+
## Logging
88136

89137
You can enable logging by defining the following:
90138

91139
`UNITY_PROCESS_MANAGER_LOGGER`
92140

141+
### ConcurrentProcess Logging
142+
93143
```csharp
94144
await ConcurrentProcess.CreateWithLog(
95145
Logger, // Some Microsoft.Extensions.Logging implements class.(I recommend using ZLogger)
@@ -98,6 +148,11 @@ await ConcurrentProcess.CreateWithLog(
98148
.LoopProcessAsync(cancellationToken: cancellationToken);
99149
```
100150

151+
### LiteRequestBroker Logging
152+
153+
```csharp
154+
var liteRequestBroker = new LiteRequestBroker(Logger);
155+
```
101156

102157

103158
## Installation ☘️

README_JA.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
2. `RequestConsumer`を使用して、RequestのPushをawait
1818
3. `ConcurrentProcess`を使用して、複数の`RequestConsumer`の並列待機し、各Requestが来た時の処理を個別に記述
1919

20-
## コード例
21-
2220
### ① Bind RequestHandler (Zenject Example)
2321
```cs
2422
// ----- In some installer
@@ -92,12 +90,61 @@ await ConcurrentProcess.Create(
9290
.LoopProcessAsync(cancellationToken: ct);
9391
```
9492

95-
# Logging
93+
## LiteRequestBroker
94+
95+
LiteRequestBrokerは、RequestHandlerクラスを使用せずにリクエストを送信するために使用できるクラスです。
96+
97+
下記のような場合に使用することをおすすめします。
98+
- リクエストがパラメータを持たない時
99+
- 同じリクエストが同時に待機されない時
100+
101+
### ① Bind LiteRequestBroker
102+
```cs
103+
// ----- In some installer
104+
105+
var liteRequestBroker = new LiteRequestBroker();
106+
Container.BindInstance<ILiteRequestPusher>(liteRequestBroker);
107+
Container.BindInstance<ILiteRequestConsumer>(liteRequestBroker);
108+
```
109+
110+
### ② Push Request (R3 Example)
111+
```cs
112+
// ----- In some object
113+
[SerializeField] private Button _closeButton;
114+
[Inject] private ILiteRequestPusher _liteRequestPusher;
115+
private void Start()
116+
{
117+
_closeButton.OnClickAsObservable()
118+
// I reccommend defining a const string for each request
119+
.Subscribe(_ => _liteRequestPusher.PushRequest("CloseRequest"))
120+
.AddTo(this);
121+
}
122+
```
123+
124+
### ③ Wait Request
125+
```cs
126+
await ConcurrentProcess.Create(
127+
Process.Create(
128+
waitTask: async ct => await LiteRequestConcumer.WaitRequestAndConsumeAsync("CloseRequest", ct),
129+
onPassedTask: async ct =>
130+
{
131+
await CloseAsync(ct);
132+
return ProcessContinueType.Break;
133+
}),
134+
135+
// ...
136+
)
137+
.LoopProcessAsync(cancellationToken: ct);
138+
```
139+
140+
## Logging
96141

97142
下記のdefine symbolを定義することでログ関連の実装を有効にできます。
98143

99144
`UNITY_PROCESS_MANAGER_LOGGER`
100145

146+
### ConcurrentProcessのログ対応
147+
101148
```csharp
102149
await ConcurrentProcess.CreateWithLog(
103150
Logger, // Microsoft.Extensions.Loggingを継承したクラス(ログ出しにはZLoggerがおすすめです)
@@ -106,6 +153,11 @@ await ConcurrentProcess.CreateWithLog(
106153
.LoopProcessAsync(cancellationToken: cancellationToken);
107154
```
108155

156+
### LiteRequestBrokerのログ対応
157+
```csharp
158+
var liteRequestBroker = new LiteRequestBroker(Logger);
159+
```
160+
109161
## Installation ☘️
110162

111163
### Install via git URL

Runtime/ConcurrentProcess.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace TanitakaTech.UnityProcessManager
1111
{
1212
public readonly struct ConcurrentProcess
1313
{
14-
private Memory<Process> Processes { get; }
14+
private readonly Memory<Process> _processes;
1515

1616
private ConcurrentProcess(params Process[] processTasks)
1717
{
18-
Processes = new Memory<Process>(processTasks);
18+
_processes = new Memory<Process>(processTasks);
1919
}
2020

2121
public static ConcurrentProcess Create(params Process[] processTasks)
@@ -57,16 +57,16 @@ public static ConcurrentProcess CreateWithLog(ILogger logger, params IProcessPro
5757
public static ConcurrentProcess Create(params ConcurrentProcess[] concurrentProcesses)
5858
{
5959
return new ConcurrentProcess(
60-
concurrentProcesses.SelectMany(concurrentProcess => concurrentProcess.Processes.ToArray()).ToArray()
60+
concurrentProcesses.SelectMany(concurrentProcess => concurrentProcess._processes.ToArray()).ToArray()
6161
);
6262
}
6363

6464
public ConcurrentProcess With(params ConcurrentProcess[] concurrentProcesses)
6565
{
6666
return new ConcurrentProcess(
6767
concurrentProcesses
68-
.SelectMany(concurrentProcess => concurrentProcess.Processes.ToArray())
69-
.Concat(Processes.ToArray())
68+
.SelectMany(concurrentProcess => concurrentProcess._processes.ToArray())
69+
.Concat(_processes.ToArray())
7070
.ToArray()
7171
);
7272
}
@@ -84,15 +84,16 @@ private async UniTask<ProcessContinueType> InternalProcessAsync(CancellationToke
8484
{
8585
CancellationTokenSource cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
8686

87-
UniTask[] tasks = new UniTask[Processes.Length];
88-
for (int i = 0; i < Processes.Length; i++)
87+
var length = _processes.Length;
88+
UniTask[] tasks = new UniTask[length];
89+
for (int i = 0; i < length; i++)
8990
{
90-
tasks[i] = Processes.Span[i].WaitTask(cancellationTokenSource.Token);
91+
tasks[i] = _processes.Span[i].WaitTask(cancellationTokenSource.Token);
9192
}
9293

9394
var passedTaskIndex = await UniTask.WhenAny(tasks);
9495
cancellationTokenSource.Cancel();
95-
return await Processes.Span[passedTaskIndex].OnPassedTask(cancellationToken);
96+
return await _processes.Span[passedTaskIndex].OnPassedTask(cancellationToken);
9697
}
9798
}
9899
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Threading;
2+
using Cysharp.Threading.Tasks;
3+
4+
namespace TanitakaTech.UnityProcessManager
5+
{
6+
public interface ILiteRequestConsumer
7+
{
8+
UniTask WaitRequestAndConsumeAsync(string waitRequest, CancellationToken cancellationToken);
9+
}
10+
}

Runtime/Request/ILiteRequestConsumer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TanitakaTech.UnityProcessManager
2+
{
3+
public interface ILiteRequestPusher
4+
{
5+
void PushRequest(string request);
6+
}
7+
}

Runtime/Request/ILiteRequestPusher.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Request/IRequestConsumer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace TanitakaTech.UnityProcessManager
55
{
66
public interface IRequestConsumer<TRequest>
77
{
8-
public UniTask<TRequest> WaitRequestAndConsumeAsync(CancellationToken cancellationToken);
8+
UniTask<TRequest> WaitRequestAndConsumeAsync(CancellationToken cancellationToken);
99
}
1010
}

Runtime/Request/IRequestPusher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public interface IRequestPusher<TRequest>
44
{
5-
public void PushRequest(TRequest requestValue);
5+
void PushRequest(TRequest requestValue);
66
}
77
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.Generic;
2+
using System.Threading;
3+
using Cysharp.Threading.Tasks;
4+
5+
#if UNITY_PROCESS_MANAGER_LOGGER
6+
using Microsoft.Extensions.Logging;
7+
#endif
8+
9+
namespace TanitakaTech.UnityProcessManager
10+
{
11+
public class LiteRequestBroker :
12+
ILiteRequestPusher,
13+
ILiteRequestConsumer
14+
{
15+
#if UNITY_PROCESS_MANAGER_LOGGER
16+
private readonly ILogger _logger;
17+
#endif
18+
private readonly HashSet<string> _waitRequests;
19+
20+
public LiteRequestBroker(
21+
#if UNITY_PROCESS_MANAGER_LOGGER
22+
ILogger logger,
23+
#endif
24+
int initialCapacity = 32
25+
)
26+
{
27+
#if UNITY_PROCESS_MANAGER_LOGGER
28+
_logger = logger;
29+
#endif
30+
_waitRequests = new HashSet<string>(initialCapacity);
31+
}
32+
33+
public LiteRequestBroker(int initialCapacity)
34+
{
35+
_waitRequests = new HashSet<string>(initialCapacity);
36+
}
37+
38+
void ILiteRequestPusher.PushRequest(string request)
39+
{
40+
if (_waitRequests.Contains(request))
41+
{
42+
_waitRequests.Remove(request);
43+
}
44+
#if UNITY_PROCESS_MANAGER_LOGGER
45+
else
46+
{
47+
_logger.LogInformation("Any consumer is not waiting for a {0}", request);
48+
}
49+
#endif
50+
}
51+
52+
async UniTask ILiteRequestConsumer.WaitRequestAndConsumeAsync(string waitRequest, CancellationToken cancellationToken)
53+
{
54+
#if UNITY_PROCESS_MANAGER_LOGGER
55+
_logger.LogInformation("Start waiting for a {0}", waitRequest);
56+
#endif
57+
_waitRequests.Add(waitRequest);
58+
cancellationToken.Register(() => _waitRequests.Remove(waitRequest));
59+
await UniTask.WaitUntil(() => !_waitRequests.Contains(waitRequest), cancellationToken: cancellationToken);
60+
#if UNITY_PROCESS_MANAGER_LOGGER
61+
_logger.LogInformation("{0} was consumed", waitRequest);
62+
#endif
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)