Flow.Launcher/Flow.Launcher.Core/Plugin/JsonRPCPluginV2.cs

126 lines
3.5 KiB
C#
Raw Normal View History

2022-09-01 02:34:47 +00:00
using System;
using System.Collections.Generic;
using System.IO;
2023-07-03 08:19:27 +00:00
using System.IO.Pipelines;
2022-09-01 02:34:47 +00:00
using System.Threading;
using System.Threading.Tasks;
2023-06-02 15:05:09 +00:00
using Flow.Launcher.Core.Plugin.JsonRPCV2Models;
2022-09-01 02:34:47 +00:00
using Flow.Launcher.Plugin;
using Microsoft.VisualStudio.Threading;
2023-06-02 15:05:09 +00:00
using StreamJsonRpc;
using IAsyncDisposable = System.IAsyncDisposable;
2023-06-02 15:05:09 +00:00
2022-09-01 02:34:47 +00:00
namespace Flow.Launcher.Core.Plugin
{
2023-07-03 08:19:27 +00:00
internal abstract class JsonRPCPluginV2 : JsonRPCPluginBase, IAsyncDisposable, IAsyncReloadable, IResultUpdated
2022-09-01 02:34:47 +00:00
{
public abstract string SupportedLanguage { get; set; }
2023-06-02 15:05:09 +00:00
public const string JsonRpc = "JsonRPC";
2022-09-01 02:34:47 +00:00
2023-07-03 08:19:27 +00:00
protected abstract IDuplexPipe ClientPipe { get; set; }
2022-09-01 02:34:47 +00:00
2023-06-02 15:05:09 +00:00
protected StreamReader ErrorStream { get; set; }
2022-09-01 02:34:47 +00:00
2023-07-03 08:19:27 +00:00
private JsonRpc RPC { get; set; }
2022-09-01 02:34:47 +00:00
2023-06-02 15:05:09 +00:00
protected override async Task<bool> ExecuteResultAsync(JsonRPCResult result)
2022-09-01 02:34:47 +00:00
{
2023-06-02 15:05:09 +00:00
try
{
2023-06-24 16:43:21 +00:00
var res = await RPC.InvokeAsync<JsonRPCExecuteResponse>(result.JsonRPCAction.Method,
argument: result.JsonRPCAction.Parameters);
2022-09-01 02:34:47 +00:00
2023-06-02 15:05:09 +00:00
return res.Hide;
}
catch
2022-09-01 02:34:47 +00:00
{
2023-06-02 15:05:09 +00:00
return false;
2022-09-01 02:34:47 +00:00
}
}
public override List<Result> LoadContextMenus(Result selectedResult)
{
throw new NotImplementedException();
}
2023-06-02 15:05:09 +00:00
public override async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
2022-09-01 02:34:47 +00:00
{
2023-06-02 15:05:09 +00:00
try
{
2023-06-25 04:43:47 +00:00
var res = await RPC.InvokeWithCancellationAsync<JsonRPCQueryResponseModel>("query",
2023-06-24 16:43:21 +00:00
new[] { query },
token);
2022-09-01 02:34:47 +00:00
2023-06-02 15:05:09 +00:00
var results = ParseResults(res);
return results;
}
catch
{
2023-06-25 04:43:47 +00:00
return new List<Result>();
2023-06-02 15:05:09 +00:00
}
2022-09-01 02:34:47 +00:00
}
2023-06-02 15:05:09 +00:00
2023-03-26 19:04:06 +00:00
public override async Task InitAsync(PluginInitContext context)
2022-09-01 02:34:47 +00:00
{
2023-03-26 19:04:06 +00:00
await base.InitAsync(context);
2023-06-02 15:05:09 +00:00
2023-07-03 08:19:27 +00:00
SetupJsonRPC();
2023-06-02 15:05:09 +00:00
_ = ReadErrorAsync();
2023-06-25 04:43:47 +00:00
await RPC.InvokeAsync("initialize", context);
2023-06-02 15:05:09 +00:00
async Task ReadErrorAsync()
{
var error = await ErrorStream.ReadToEndAsync();
if (!string.IsNullOrEmpty(error))
{
throw new Exception(error);
}
}
2022-09-01 02:34:47 +00:00
}
2023-06-24 16:43:21 +00:00
2023-07-03 08:19:27 +00:00
public event ResultUpdatedEventHandler ResultsUpdated;
private void SetupJsonRPC()
{
var formatter = new SystemTextJsonFormatter();
2023-07-03 08:19:27 +00:00
var handler = new NewLineDelimitedMessageHandler(ClientPipe,
formatter);
RPC = new JsonRpc(handler, new JsonRPCPublicAPI(Context.API));
RPC.AddLocalRpcMethod("UpdateResults", new Action<string, JsonRPCQueryResponseModel>((rawQuery, response) =>
{
var results = ParseResults(response);
ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs { Query = new Query()
{
RawQuery = rawQuery
}, Results = results });
}));
RPC.SynchronizationContext = null;
RPC.StartListening();
}
public virtual Task ReloadDataAsync()
{
SetupJsonRPC();
return Task.CompletedTask;
}
public virtual ValueTask DisposeAsync()
2023-06-24 16:43:21 +00:00
{
RPC?.Dispose();
ErrorStream?.Dispose();
2023-07-03 08:19:27 +00:00
return ValueTask.CompletedTask;
2023-06-24 16:43:21 +00:00
}
2022-09-01 02:34:47 +00:00
}
}