-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from mhutch/background-parse-service
Background parse service
- Loading branch information
Showing
28 changed files
with
170 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace MonoDevelop.Xml.Editor.Parsing; | ||
|
||
/// <summary> | ||
/// Allows observing the status of background parsing operations. | ||
/// May be extended in future to provide custom scheduling. | ||
/// </summary> | ||
public interface IBackgroundParseService | ||
{ | ||
void RegisterBackgroundOperation (Task task); | ||
event EventHandler RunningStateChanged; | ||
public bool IsRunning { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.ComponentModel.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.VisualStudio.Text; | ||
|
||
using MonoDevelop.Xml.Editor.Logging; | ||
using MonoDevelop.Xml.Logging; | ||
|
||
namespace MonoDevelop.Xml.Editor.Parsing; | ||
|
||
/// <summary> | ||
/// Import to obtain <see cref="IBackgroundParseService"/> instances. | ||
/// </summary> | ||
[Export (typeof (BackgroundParseServiceProvider))] | ||
public sealed class BackgroundParseServiceProvider | ||
{ | ||
readonly object lockerObject = new (); | ||
readonly IEditorLoggerFactory loggerFactory; | ||
|
||
ImmutableDictionary<string, BackgroundParserService> parseServices = ImmutableDictionary<string, BackgroundParserService>.Empty; | ||
|
||
[ImportingConstructor] | ||
public BackgroundParseServiceProvider (IEditorLoggerFactory loggerFactory) | ||
{ | ||
this.loggerFactory = loggerFactory; | ||
} | ||
|
||
public IBackgroundParseService GetParseServiceForBuffer (ITextBuffer buffer) => GetParseServiceForContentType (buffer.ContentType.TypeName); | ||
public IBackgroundParseService GetParseServiceForContentType (string contentTypeName) | ||
{ | ||
if (parseServices.TryGetValue (contentTypeName, out var service)) { | ||
return service; | ||
} | ||
lock (lockerObject) { | ||
if (parseServices.TryGetValue (contentTypeName, out service)) { | ||
return service; | ||
} | ||
var logger = loggerFactory.CreateLogger<BackgroundParserService> (contentTypeName); | ||
service = new BackgroundParserService (logger); | ||
parseServices = parseServices.Add (contentTypeName, service); | ||
} | ||
return service; | ||
} | ||
|
||
class BackgroundParserService : IBackgroundParseService | ||
{ | ||
readonly ILogger logger; | ||
|
||
[ImportingConstructor] | ||
public BackgroundParserService (ILogger logger) | ||
{ | ||
this.logger = logger; | ||
} | ||
|
||
int runningTasks = 0; | ||
|
||
public bool IsRunning => runningTasks > 0; | ||
|
||
public void RegisterBackgroundOperation (Task task) | ||
{ | ||
int taskCount = Interlocked.Increment (ref runningTasks); | ||
task.ContinueWith (OperationCompleted, TaskScheduler.Default).CatchAndLogWarning (logger); | ||
|
||
if (taskCount == 1 || taskCount == 0) { | ||
RunningStateChanged?.Invoke (this, EventArgs.Empty); | ||
} | ||
} | ||
|
||
void OperationCompleted (Task t) | ||
{ | ||
int taskCount = Interlocked.Decrement (ref runningTasks); | ||
|
||
if (taskCount == 1 || taskCount == 0) { | ||
RunningStateChanged?.Invoke (this, EventArgs.Empty); | ||
} | ||
} | ||
|
||
public event EventHandler? RunningStateChanged; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.