Skip to content

Commit 32bbf1e

Browse files
committed
Finally make JSONRPC Bidirection work
1 parent f691259 commit 32bbf1e

File tree

7 files changed

+396
-42
lines changed

7 files changed

+396
-42
lines changed

Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Flow.Launcher.Core.ExternalPlugins
1212
{
1313
public static class PluginsManifest
1414
{
15-
private const string manifestFileUrl = "https://cdn.jsdelivr.net/gh/Flow-Launcher/Flow.Launcher.PluginsManifest@plugin_api_v2/plugins.json";
15+
private const string manifestFileUrl = "https://jsdelivr.bobocdn.tk/gh/Flow-Launcher/Flow.Launcher.PluginsManifest@plugin_api_v2/plugins.json";
1616

1717
private static readonly SemaphoreSlim manifestUpdateLock = new(1);
1818

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,5 @@
6464
<ProjectReference Include="..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
6565
<ProjectReference Include="..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
6666
</ItemGroup>
67-
68-
<ItemGroup>
69-
<Folder Include="Helper" />
70-
</ItemGroup>
7167

7268
</Project>

Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ internal abstract class JsonRPCPluginBase : IAsyncPlugin, IContextMenu, ISetting
4141

4242
private int RequestId { get; set; }
4343

44-
private string SettingConfigurationPath => Path.Combine(Context.CurrentPluginMetadata.PluginDirectory, "SettingsTemplate.yaml");
44+
private string SettingConfigurationPath =>
45+
Path.Combine(Context.CurrentPluginMetadata.PluginDirectory, "SettingsTemplate.yaml");
4546

46-
private string SettingPath => Path.Combine(DataLocation.PluginSettingsDirectory, Context.CurrentPluginMetadata.Name, "Settings.json");
47+
private string SettingPath => Path.Combine(DataLocation.PluginSettingsDirectory,
48+
Context.CurrentPluginMetadata.Name, "Settings.json");
4749

4850
public abstract List<Result> LoadContextMenus(Result selectedResult);
4951

@@ -57,10 +59,7 @@ internal abstract class JsonRPCPluginBase : IAsyncPlugin, IContextMenu, ISetting
5759
// see: https://github.com/dotnet/runtime/issues/39152
5860
IgnoreNullValues = true,
5961
#pragma warning restore SYSLIB0020 // Type or member is obsolete
60-
Converters =
61-
{
62-
new JsonObjectConverter()
63-
}
62+
Converters = { new JsonObjectConverter() }
6463
};
6564

6665
protected static readonly JsonSerializerOptions RequestSerializeOption = new()
@@ -83,9 +82,9 @@ protected List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel
8382

8483
foreach (var result in queryResponseModel.Result)
8584
{
86-
result.AsyncAction = async c =>
85+
result.AsyncAction = async _ =>
8786
{
88-
Settings.UpdateSettings(result.SettingsChange);
87+
Settings?.UpdateSettings(result.SettingsChange);
8988

9089
return await ExecuteResultAsync(result);
9190
};
@@ -95,7 +94,7 @@ protected List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel
9594

9695
results.AddRange(queryResponseModel.Result);
9796

98-
Settings.UpdateSettings(queryResponseModel.SettingsChanges);
97+
Settings?.UpdateSettings(queryResponseModel.SettingsChanges);
9998

10099
return results;
101100
}
@@ -130,18 +129,18 @@ private async Task InitSettingAsync()
130129
if (!File.Exists(SettingConfigurationPath))
131130
return;
132131

133-
var deserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
134-
var configuration = deserializer.Deserialize<JsonRpcConfigurationModel>(await File.ReadAllTextAsync(SettingConfigurationPath));
132+
var deserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance)
133+
.Build();
134+
var configuration =
135+
deserializer.Deserialize<JsonRpcConfigurationModel>(
136+
await File.ReadAllTextAsync(SettingConfigurationPath));
135137

136138
Settings ??= new PortableSettings
137139
{
138-
Configuration = configuration,
139-
SettingPath = SettingPath,
140-
API = Context.API
140+
Configuration = configuration, SettingPath = SettingPath, API = Context.API
141141
};
142142

143143
await Settings.InitializeAsync();
144-
145144
}
146145

147146
public virtual async Task InitAsync(PluginInitContext context)
@@ -154,10 +153,10 @@ public void Save()
154153
{
155154
Settings?.Save();
156155
}
156+
157157
public Control CreateSettingPanel()
158158
{
159159
return Settings.CreateSettingPanel();
160160
}
161161
}
162-
163162
}

Flow.Launcher.Core/Plugin/JsonRPCPluginV2.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
using System;
2-
using System.Collections.Concurrent;
32
using System.Collections.Generic;
43
using System.IO;
5-
using System.Text.Json;
64
using System.Threading;
7-
using System.Threading.Channels;
85
using System.Threading.Tasks;
9-
using System.Windows.Controls;
106
using Flow.Launcher.Core.Plugin.JsonRPCV2Models;
117
using Flow.Launcher.Plugin;
128
using StreamJsonRpc;
139

1410

1511
namespace Flow.Launcher.Core.Plugin
1612
{
17-
internal abstract class JsonRpcPluginV2 : JsonRPCPluginBase
13+
internal abstract class JsonRPCPluginV2 : JsonRPCPluginBase, IDisposable
1814
{
1915
public abstract string SupportedLanguage { get; set; }
2016

2117
public const string JsonRpc = "JsonRPC";
2218

23-
protected abstract JsonRpc Rpc { get; set; }
19+
protected abstract JsonRpc RPC { get; set; }
2420

2521
protected StreamReader ErrorStream { get; set; }
2622

@@ -29,7 +25,8 @@ protected override async Task<bool> ExecuteResultAsync(JsonRPCResult result)
2925
{
3026
try
3127
{
32-
var res = await Rpc.InvokeAsync<JsonRPCExecuteResponse>(result.JsonRPCAction.Method, argument: result.JsonRPCAction.Parameters);
28+
var res = await RPC.InvokeAsync<JsonRPCExecuteResponse>(result.JsonRPCAction.Method,
29+
argument: result.JsonRPCAction.Parameters);
3330

3431
return res.Hide;
3532
}
@@ -43,15 +40,17 @@ public override async Task<List<Result>> QueryAsync(Query query, CancellationTok
4340
{
4441
try
4542
{
46-
var res = await Rpc.InvokeAsync<JsonRPCQueryResponseModel>("query", query);
43+
var res = await RPC.InvokeWithCancellationAsync<JsonRPCQueryResponseModel>("query",
44+
new[] { query },
45+
token);
4746

4847
var results = ParseResults(res);
4948

5049
return results;
5150
}
5251
catch
5352
{
54-
return new List<Result>();
53+
return new List<Result>();
5554
}
5655
}
5756

@@ -72,5 +71,11 @@ async Task ReadErrorAsync()
7271
}
7372
}
7473
}
74+
75+
public void Dispose()
76+
{
77+
RPC?.Dispose();
78+
ErrorStream?.Dispose();
79+
}
7580
}
7681
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace Flow.Launcher.Core.Plugin.JsonRPCV2Models
22
{
3-
public record JsonRPCExecuteResponse(bool Hide = true);
3+
public abstract record JsonRPCExecuteResponse(bool Hide = true);
44
}

0 commit comments

Comments
 (0)