add IPathSelected to JsonRPC plugins

This commit is contained in:
Jeremy 2022-02-16 09:00:21 +11:00
parent 1896b67989
commit ffd6109b58
3 changed files with 40 additions and 11 deletions

View file

@ -21,12 +21,15 @@ namespace Flow.Launcher.Core.Plugin
RedirectStandardOutput = true,
RedirectStandardError = true
};
// required initialisation for below request calls
// required initialisation for below request calls
_startInfo.ArgumentList.Add(string.Empty);
}
protected override Task<Stream> RequestAsync(JsonRPCRequestModel request, CancellationToken token = default)
protected override Task<Stream> RequestAsync(
JsonRPCRequestModel request,
CancellationToken token = default,
bool ignoreEmptyResponse = false)
{
// since this is not static, request strings will build up in ArgumentList if index is not specified
_startInfo.ArgumentList[0] = request.ToString();

View file

@ -35,7 +35,7 @@ namespace Flow.Launcher.Core.Plugin
/// Represent the plugin that using JsonPRC
/// every JsonRPC plugin should has its own plugin instance
/// </summary>
internal abstract class JsonRPCPlugin : IAsyncPlugin, IContextMenu, ISettingProvider, ISavable
internal abstract class JsonRPCPlugin : IAsyncPlugin, IContextMenu, ISettingProvider, ISavable, IPathSelected
{
protected PluginInitContext context;
public const string JsonRPC = "JsonRPC";
@ -44,7 +44,11 @@ namespace Flow.Launcher.Core.Plugin
/// The language this JsonRPCPlugin support
/// </summary>
public abstract string SupportedLanguage { get; set; }
protected abstract Task<Stream> RequestAsync(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
protected abstract Task<Stream> RequestAsync(
JsonRPCRequestModel rpcRequest,
CancellationToken token = default,
bool ignoreEmptyResponse = false);
protected abstract string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
private static readonly RecyclableMemoryStreamManager BufferManager = new();
@ -238,7 +242,10 @@ namespace Flow.Launcher.Core.Plugin
}
}
protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, CancellationToken token = default)
protected async Task<Stream> ExecuteAsync(
ProcessStartInfo startInfo,
CancellationToken token = default,
bool ignoreEmptyResponse = false)
{
Process process = null;
bool disposed = false;
@ -265,7 +272,7 @@ namespace Flow.Launcher.Core.Plugin
try
{
// token expire won't instantly trigger the exception,
// token expire won't instantly trigger the exception,
// manually kill process at before
await source.CopyToAsync(buffer, token);
}
@ -281,10 +288,13 @@ namespace Flow.Launcher.Core.Plugin
if (buffer.Length == 0)
{
var errorMessage = process.StandardError.EndOfStream ?
var endOfStream = process.StandardError.EndOfStream;
var errorMessage = endOfStream ?
"Empty JSONRPC Response" :
await process.StandardError.ReadToEndAsync();
throw new InvalidDataException($"{context.CurrentPluginMetadata.Name}|{errorMessage}");
if (!endOfStream || (endOfStream && !ignoreEmptyResponse))
throw new InvalidDataException($"{context.CurrentPluginMetadata.Name}|{errorMessage}");
}
if (!process.StandardError.EndOfStream)
@ -534,5 +544,18 @@ namespace Flow.Launcher.Core.Plugin
}
}
}
public async Task PathSelected(string filePath, string hotkey)
{
var request = new JsonRPCRequestModel
{
Method = "PathSelected",
Parameters = new object[]
{
filePath, hotkey
}
};
await RequestAsync(request, ignoreEmptyResponse: true);
}
}
}

View file

@ -37,11 +37,14 @@ namespace Flow.Launcher.Core.Plugin
_startInfo.ArgumentList.Add("-B");
}
protected override Task<Stream> RequestAsync(JsonRPCRequestModel request, CancellationToken token = default)
protected override Task<Stream> RequestAsync(
JsonRPCRequestModel request,
CancellationToken token = default,
bool ignoreEmptyResponse = false)
{
_startInfo.ArgumentList[2] = request.ToString();
return ExecuteAsync(_startInfo, token);
return ExecuteAsync(_startInfo, token, ignoreEmptyResponse: ignoreEmptyResponse);
}
protected override string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default)