Skip to content

Commit 8330dd3

Browse files
committed
Add a MemoryStream buffer to ReadStream first
1 parent 92685d9 commit 8330dd3

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

Flow.Launcher.Core/Flow.Launcher.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<ItemGroup>
5656
<PackageReference Include="Droplex" Version="1.3.1" />
5757
<PackageReference Include="FSharp.Core" Version="4.7.1" />
58+
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.1.3" />
5859
<PackageReference Include="squirrel.windows" Version="1.5.2" />
5960
</ItemGroup>
6061

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Flow.Launcher.Infrastructure.Logger;
1313
using Flow.Launcher.Plugin;
1414
using JetBrains.Annotations;
15+
using Microsoft.IO;
1516

1617
namespace Flow.Launcher.Core.Plugin
1718
{
@@ -33,9 +34,11 @@ internal abstract class JsonRPCPlugin : IAsyncPlugin, IContextMenu
3334
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
3435
protected abstract string ExecuteContextMenu(Result selectedResult);
3536

37+
private static readonly RecyclableMemoryStreamManager BufferManager = new();
38+
3639
public List<Result> LoadContextMenus(Result selectedResult)
3740
{
38-
string output = ExecuteContextMenu(selectedResult);
41+
var output = ExecuteContextMenu(selectedResult);
3942
try
4043
{
4144
return DeserializedResult(output);
@@ -61,12 +64,23 @@ private async Task<List<Result>> DeserializedResultAsync(Stream output)
6164
{
6265
if (output == Stream.Null) return null;
6366

64-
var queryResponseModel = await
65-
JsonSerializer.DeserializeAsync<JsonRPCQueryResponseModel>(output, options);
67+
try
68+
{
69+
var queryResponseModel =
70+
await JsonSerializer.DeserializeAsync<JsonRPCQueryResponseModel>(output, options);
6671

67-
await output.DisposeAsync();
68-
69-
return ParseResults(queryResponseModel);
72+
return ParseResults(queryResponseModel);
73+
}
74+
catch (JsonException e)
75+
{
76+
Log.Exception(GetType().FullName, "Unexpected Json Input", e);
77+
}
78+
finally
79+
{
80+
await output.DisposeAsync();
81+
}
82+
83+
return null;
7084
}
7185

7286
private List<Result> DeserializedResult(string output)
@@ -81,15 +95,14 @@ private List<Result> DeserializedResult(string output)
8195

8296
private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
8397
{
84-
var results = new List<Result>();
8598
if (queryResponseModel.Result == null) return null;
8699

87100
if (!string.IsNullOrEmpty(queryResponseModel.DebugMessage))
88101
{
89102
context.API.ShowMsg(queryResponseModel.DebugMessage);
90103
}
91104

92-
foreach (JsonRPCResult result in queryResponseModel.Result)
105+
foreach (var result in queryResponseModel.Result)
93106
{
94107
result.Action = c =>
95108
{
@@ -114,7 +127,8 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
114127
return !result.JsonRPCAction.DontHideAfterAction;
115128
}
116129

117-
var jsonRpcRequestModel = JsonSerializer.Deserialize<JsonRPCRequestModel>(actionResponse, options);
130+
var jsonRpcRequestModel =
131+
JsonSerializer.Deserialize<JsonRPCRequestModel>(actionResponse, options);
118132

119133
if (jsonRpcRequestModel?.Method?.StartsWith("Flow.Launcher.") ?? false)
120134
{
@@ -125,9 +139,12 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
125139

126140
return !result.JsonRPCAction.DontHideAfterAction;
127141
};
128-
results.Add(result);
129142
}
130143

144+
var results = new List<Result>();
145+
146+
results.AddRange(queryResponseModel.Result);
147+
131148
return results;
132149
}
133150

@@ -226,7 +243,21 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
226243
return Stream.Null;
227244
}
228245

229-
var result = process.StandardOutput.BaseStream;
246+
var source = process.StandardOutput.BaseStream;
247+
248+
var buffer = BufferManager.GetStream();
249+
250+
try
251+
{
252+
await source.CopyToAsync(buffer, token);
253+
}
254+
catch (OperationCanceledException)
255+
{
256+
await buffer.DisposeAsync();
257+
throw;
258+
}
259+
260+
buffer.Seek(0, SeekOrigin.Begin);
230261

231262
token.ThrowIfCancellationRequested();
232263

@@ -245,7 +276,7 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
245276
return Stream.Null;
246277
}
247278

248-
return result;
279+
return buffer;
249280
}
250281
catch (Exception e)
251282
{

0 commit comments

Comments
 (0)