Skip to content

Commit 7ab86ca

Browse files
committed
Refactor JsonRPC structure
1 parent f1ed346 commit 7ab86ca

File tree

4 files changed

+34
-64
lines changed

4 files changed

+34
-64
lines changed

Flow.Launcher.Core/Plugin/ExecutablePlugin.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,16 @@ public ExecutablePlugin(string filename)
2424
};
2525
}
2626

27-
protected override Task<Stream> ExecuteQueryAsync(Query query, CancellationToken token)
27+
protected override Task<Stream> RequestAsync(JsonRPCRequestModel request, CancellationToken token = default)
2828
{
29-
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
30-
{
31-
Method = "query",
32-
Parameters = new object[] {query.Search},
33-
};
34-
3529
_startInfo.Arguments = $"\"{request}\"";
36-
3730
return ExecuteAsync(_startInfo, token);
3831
}
3932

40-
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
33+
protected override string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default)
4134
{
4235
_startInfo.Arguments = $"\"{rpcRequest}\"";
4336
return Execute(_startInfo);
4437
}
45-
46-
protected override string ExecuteContextMenu(Result selectedResult)
47-
{
48-
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
49-
{
50-
Method = "contextmenu",
51-
Parameters = new object[] {selectedResult.ContextData},
52-
};
53-
54-
_startInfo.Arguments = $"\"{request}\"";
55-
56-
return Execute(_startInfo);
57-
}
5838
}
5939
}

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,22 @@ internal abstract class JsonRPCPlugin : IAsyncPlugin, IContextMenu
3030
/// The language this JsonRPCPlugin support
3131
/// </summary>
3232
public abstract string SupportedLanguage { get; set; }
33-
34-
protected abstract Task<Stream> ExecuteQueryAsync(Query query, CancellationToken token);
35-
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
36-
protected abstract string ExecuteContextMenu(Result selectedResult);
33+
protected abstract Task<Stream> RequestAsync(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
34+
protected abstract string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
3735

3836
private static readonly RecyclableMemoryStreamManager BufferManager = new();
3937

4038
public List<Result> LoadContextMenus(Result selectedResult)
4139
{
42-
var output = ExecuteContextMenu(selectedResult);
40+
var request = new JsonRPCRequestModel
41+
{
42+
Method = "context_menu",
43+
Parameters = new[]
44+
{
45+
selectedResult.ContextData
46+
}
47+
};
48+
var output = Request(request);
4349
return DeserializedResult(output);
4450
}
4551

@@ -100,7 +106,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
100106
}
101107
else
102108
{
103-
var actionResponse = ExecuteCallback(result.JsonRPCAction);
109+
var actionResponse = Request(result.JsonRPCAction);
104110

105111
if (string.IsNullOrEmpty(actionResponse))
106112
{
@@ -255,8 +261,8 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
255261

256262
if (buffer.Length == 0)
257263
{
258-
var errorMessage = process.StandardError.EndOfStream ?
259-
"Empty JSONRPC Response" :
264+
var errorMessage = process.StandardError.EndOfStream ?
265+
"Empty JSONRPC Response" :
260266
await process.StandardError.ReadToEndAsync();
261267
throw new InvalidDataException($"{context.CurrentPluginMetadata.Name}|{errorMessage}");
262268
}
@@ -283,7 +289,15 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
283289

284290
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
285291
{
286-
var output = await ExecuteQueryAsync(query, token);
292+
var request = new JsonRPCRequestModel
293+
{
294+
Method = "query",
295+
Parameters = new[]
296+
{
297+
query.Search
298+
}
299+
};
300+
var output = await RequestAsync(request, token);
287301
return await DeserializedResultAsync(output);
288302
}
289303

@@ -293,4 +307,4 @@ public virtual Task InitAsync(PluginInitContext context)
293307
return Task.CompletedTask;
294308
}
295309
}
296-
}
310+
}

Flow.Launcher.Core/Plugin/PythonPlugin.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@ public PythonPlugin(string filename)
3232
_startInfo.ArgumentList.Add("-B");
3333
}
3434

35-
protected override Task<Stream> ExecuteQueryAsync(Query query, CancellationToken token)
35+
protected override Task<Stream> RequestAsync(JsonRPCRequestModel request, CancellationToken token = default)
3636
{
37-
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
38-
{
39-
Method = "query", Parameters = new object[] {query.Search},
40-
};
41-
4237
_startInfo.ArgumentList[2] = request.ToString();
4338

4439
// todo happlebao why context can't be used in constructor
@@ -47,27 +42,13 @@ protected override Task<Stream> ExecuteQueryAsync(Query query, CancellationToken
4742
return ExecuteAsync(_startInfo, token);
4843
}
4944

50-
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
45+
protected override string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default)
5146
{
5247
_startInfo.ArgumentList[2] = rpcRequest.ToString();
5348
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
5449
// TODO: Async Action
5550
return Execute(_startInfo);
5651
}
57-
58-
protected override string ExecuteContextMenu(Result selectedResult)
59-
{
60-
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
61-
{
62-
Method = "context_menu", Parameters = new object[] {selectedResult.ContextData},
63-
};
64-
_startInfo.ArgumentList[2] = request.ToString();
65-
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
66-
67-
// TODO: Async Action
68-
return Execute(_startInfo);
69-
}
70-
7152
public override Task InitAsync(PluginInitContext context)
7253
{
7354
this.context = context;

Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@ internal class JsonRPCPluginTest : JsonRPCPlugin
1818
{
1919
public override string SupportedLanguage { get; set; } = AllowedLanguage.Executable;
2020

21-
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
21+
protected override string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default)
2222
{
2323
throw new System.NotImplementedException();
2424
}
2525

26-
protected override string ExecuteContextMenu(Result selectedResult)
26+
protected override Task<Stream> RequestAsync(JsonRPCRequestModel request, CancellationToken token = default)
2727
{
28-
throw new System.NotImplementedException();
29-
}
30-
31-
protected override Task<Stream> ExecuteQueryAsync(Query query, CancellationToken token)
32-
{
33-
var byteInfo = Encoding.UTF8.GetBytes(query.RawQuery);
28+
var byteInfo = Encoding.UTF8.GetBytes(request.Parameters[0] as string ?? string.Empty);
3429

3530
var resultStream = new MemoryStream(byteInfo);
3631
return Task.FromResult((Stream)resultStream);
@@ -45,7 +40,7 @@ public async Task GivenVariousJsonText_WhenVariousNamingCase_ThenExpectNotNullRe
4540
{
4641
var results = await QueryAsync(new Query
4742
{
48-
RawQuery = resultText
43+
Search = resultText
4944
}, default);
5045

5146
Assert.IsNotNull(results);
@@ -85,8 +80,8 @@ public async Task GivenModel_WhenSerializeWithDifferentNamingPolicy_ThenExpectSa
8580

8681
var pascalText = JsonSerializer.Serialize(reference);
8782

88-
var results1 = await QueryAsync(new Query { RawQuery = camelText }, default);
89-
var results2 = await QueryAsync(new Query { RawQuery = pascalText }, default);
83+
var results1 = await QueryAsync(new Query { Search = camelText }, default);
84+
var results2 = await QueryAsync(new Query { Search = pascalText }, default);
9085

9186
Assert.IsNotNull(results1);
9287
Assert.IsNotNull(results2);

0 commit comments

Comments
 (0)