Renaming callbacks to be more sane

This commit is contained in:
Benjamin Russell
2016-07-25 12:37:48 -07:00
parent 46032d3e2e
commit 6664de2252
4 changed files with 54 additions and 36 deletions

View File

@@ -4,9 +4,9 @@
//
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SqlTools.EditorServices.Utility;
using Microsoft.SqlTools.ServiceLayer.Hosting.Contracts;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
@@ -27,12 +27,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting
private static readonly Lazy<ServiceHost> instance = new Lazy<ServiceHost>(() => new ServiceHost());
/// <summary>
/// Creates or retrieves the current instance of the ServiceHost
/// Current instance of the ServiceHost
/// </summary>
/// <returns>Instance of the service host</returns>
public static ServiceHost Create()
public static ServiceHost Instance
{
return instance.Value;
get { return instance.Value; }
}
/// <summary>
@@ -42,8 +41,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting
private ServiceHost() : base(new StdioServerChannel())
{
// Initialize the shutdown activities
shutdownActivities = new List<ShutdownHandler>();
initializeActivities = new List<InitializeHandler>();
shutdownCallbacks = new List<ShutdownCallback>();
initializeCallbacks = new List<InitializeCallback>();
// Register the requests that this service host will handle
this.SetRequestHandler(InitializeRequest.Type, this.HandleInitializeRequest);
@@ -54,34 +53,34 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting
#region Member Variables
public delegate Task ShutdownHandler(object shutdownParams, RequestContext<object> shutdownRequestContext);
public delegate Task ShutdownCallback(object shutdownParams, RequestContext<object> shutdownRequestContext);
public delegate Task InitializeHandler(InitializeRequest startupParams, RequestContext<InitializeResult> requestContext);
public delegate Task InitializeCallback(InitializeRequest startupParams, RequestContext<InitializeResult> requestContext);
private readonly List<ShutdownHandler> shutdownActivities;
private readonly List<ShutdownCallback> shutdownCallbacks;
private readonly List<InitializeHandler> initializeActivities;
private readonly List<InitializeCallback> initializeCallbacks;
#endregion
#region Public Methods
/// <summary>
/// Adds a new method to be called when the shutdown request is submitted
/// Adds a new callback to be called when the shutdown request is submitted
/// </summary>
/// <param name="activity"></param>
public void RegisterShutdownTask(ShutdownHandler activity)
/// <param name="callback">Callback to perform when a shutdown request is submitted</param>
public void RegisterShutdownTask(ShutdownCallback callback)
{
shutdownActivities.Add(activity);
shutdownCallbacks.Add(callback);
}
/// <summary>
/// Add a new method to be called when the initialize request is submitted
/// </summary>
/// <param name="activity"></param>
public void RegisterInitializeTask(InitializeHandler activity)
/// <param name="callback">Callback to perform when an initialize request is submitted</param>
public void RegisterInitializeTask(InitializeCallback callback)
{
initializeActivities.Add(activity);
initializeCallbacks.Add(callback);
}
#endregion
@@ -96,7 +95,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting
Logger.Write(LogLevel.Normal, "Service host is shutting down...");
// Call all the shutdown methods provided by the service components
Task[] shutdownTasks = shutdownActivities.Select(t => t(shutdownParams, requestContext)).ToArray();
Task[] shutdownTasks = shutdownCallbacks.Select(t => t(shutdownParams, requestContext)).ToArray();
await Task.WhenAll(shutdownTasks);
}
@@ -111,7 +110,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting
Logger.Write(LogLevel.Verbose, "HandleInitializationRequest");
// Call all tasks that registered on the initialize request
var initializeTasks = initializeActivities.Select(t => t(initializeParams, requestContext));
var initializeTasks = initializeCallbacks.Select(t => t(initializeParams, requestContext));
await Task.WhenAll(initializeTasks);
// TODO: Figure out where this needs to go to be agnostic of the language

View File

@@ -84,7 +84,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
});
// Register the configuration update handler
WorkspaceService<SqlToolsSettings>.Instance.RegisterDidChangeConfigurationNotificationTask(HandleDidChangeConfigurationNotification);
WorkspaceService<SqlToolsSettings>.Instance.RegisterConfigChangeCallback(HandleDidChangeConfigurationNotification);
// Store the SqlToolsContext for future use
Context = context;

View File

@@ -34,8 +34,8 @@ namespace Microsoft.SqlTools.ServiceLayer
var profilePaths = new ProfilePaths(hostProfileId, "baseAllUsersPath", "baseCurrentUserPath");
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails, profilePaths);
// Create the service host
ServiceHost serviceHost = ServiceHost.Create();
// Grab the instance of the service host
ServiceHost serviceHost = ServiceHost.Instance;
// Initialize the services that will be hosted here
WorkspaceService<SqlToolsSettings>.Instance.InitializeService(serviceHost);

View File

@@ -5,13 +5,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlTools.EditorServices.Utility;
using Microsoft.SqlTools.ServiceLayer.Hosting;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.WorkspaceServices.Contracts;
using System.Linq;
namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices
{
@@ -29,8 +29,8 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices
private WorkspaceService()
{
ConfigurationNotificationHandlers = new List<DidChangeConfigurationNotificationHandler>();
TextDocumentChangeHandlers = new List<DidChangeTextDocumentNotificationTask>();
ConfigChangeCallbacks = new List<ConfigChangeCallback>();
TextDocChangeCallbacks = new List<TextDocChangeCallback>();
}
#endregion
@@ -41,12 +41,31 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices
public TConfig CurrentSettings { get; private set; }
public delegate Task DidChangeConfigurationNotificationHandler(TConfig newSettings, TConfig oldSettings, EventContext eventContext);
/// <summary>
/// Delegate for callbacks that occur when the configuration for the workspace changes
/// </summary>
/// <param name="newSettings">The settings that were just set</param>
/// <param name="oldSettings">The settings before they were changed</param>
/// <param name="eventContext">Context of the event that triggered the callback</param>
/// <returns></returns>
public delegate Task ConfigChangeCallback(TConfig newSettings, TConfig oldSettings, EventContext eventContext);
public delegate Task DidChangeTextDocumentNotificationTask(ScriptFile[] changedFiles, EventContext eventContext);
/// <summary>
/// Delegate for callbacks that occur when the current text document changes
/// </summary>
/// <param name="changedFiles">Array of files that changed</param>
/// <param name="eventContext">Context of the event raised for the changed files</param>
public delegate Task TextDocChangeCallback(ScriptFile[] changedFiles, EventContext eventContext);
private List<DidChangeConfigurationNotificationHandler> ConfigurationNotificationHandlers { get; set; }
private List<DidChangeTextDocumentNotificationTask> TextDocumentChangeHandlers { get; set; }
/// <summary>
/// List of callbacks to call when the configuration of the workspace changes
/// </summary>
private List<ConfigChangeCallback> ConfigChangeCallbacks { get; set; }
/// <summary>
/// List of callbacks to call when the current text document changes
/// </summary>
private List<TextDocChangeCallback> TextDocChangeCallbacks { get; set; }
#endregion
@@ -95,18 +114,18 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices
/// handle changing configuration and changing the current configuration.
/// </summary>
/// <param name="task">Task to handle the request</param>
public void RegisterDidChangeConfigurationNotificationTask(DidChangeConfigurationNotificationHandler task)
public void RegisterConfigChangeCallback(ConfigChangeCallback task)
{
ConfigurationNotificationHandlers.Add(task);
ConfigChangeCallbacks.Add(task);
}
/// <summary>
/// Adds a new task to be called when the text of a document changes.
/// </summary>
/// <param name="task">Delegate to call when the document changes</param>
public void RegisterDidChangeTextDocumentNotificationTask(DidChangeTextDocumentNotificationTask task)
public void RegisterTextDocChangeCallback(TextDocChangeCallback task)
{
TextDocumentChangeHandlers.Add(task);
TextDocChangeCallbacks.Add(task);
}
#endregion
@@ -145,7 +164,7 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices
Logger.Write(LogLevel.Verbose, msg.ToString());
var handlers = TextDocumentChangeHandlers.Select(t => t(changedFiles.ToArray(), eventContext));
var handlers = TextDocChangeCallbacks.Select(t => t(changedFiles.ToArray(), eventContext));
return Task.WhenAll(handlers);
}
@@ -177,7 +196,7 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices
Logger.Write(LogLevel.Verbose, "HandleDidChangeConfigurationNotification");
// Propagate the changes to the event handlers
var configUpdateTasks = ConfigurationNotificationHandlers.Select(
var configUpdateTasks = ConfigChangeCallbacks.Select(
t => t(configChangeParams.Settings, CurrentSettings, eventContext));
await Task.WhenAll(configUpdateTasks);
}