<commit_before>﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.ErrorReporting;

namespace Microsoft.CodeAnalysis.Editor.Implementation.ForegroundNotification
{
    [Export(typeof(IForegroundNotificationService))]
    internal class ForegroundNotificationService : ForegroundThreadAffinitizedObject, IForegroundNotificationService
    {
        // how much time we will give notifications to run on the UI thread
        private const int DefaultTimeSliceInMS = 15;

        // Don't call NotifyOnForeground more than once per 50ms
        private const int MinimumDelayBetweenProcessing = 50;

        private static readonly Func<int, string> s_notifyOnForegroundLogger = c => string.Format("Processed : {0}", c);
        private readonly PriorityQueue _workQueue;

        private int _lastProcessedTimeInMS;

        [ImportingConstructor]
        public ForegroundNotificationService()
        {
            _workQueue = new PriorityQueue();
            _lastProcessedTimeInMS = Environment.TickCount;

            Debug.Assert(IsValid());
            Debug.Assert(IsForeground());
            Task.Factory.SafeStartNewFromAsync(ProcessAsync, CancellationToken.None, TaskScheduler.Default);
        }

        public void RegisterNotification(Action action, IAsyncToken asyncToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            RegisterNotification(action, DefaultTimeSliceInMS, asyncToken, cancellationToken);
        }

        public void RegisterNotification(Func<bool> action, IAsyncToken asyncToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            RegisterNotification(action, DefaultTimeSliceInMS, asyncToken, cancellationToken);
        }

        public void RegisterNotification(Action action, int delay, IAsyncToken asyncToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            Contract.Requires(delay >= 0);

            var current = Environment.TickCount;<commit_msg>Switch Debug.Assert to Contract.ThrowIfFalse<commit_after>﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.ErrorReporting;

namespace Microsoft.CodeAnalysis.Editor.Implementation.ForegroundNotification
{
    [Export(typeof(IForegroundNotificationService))]
    internal class ForegroundNotificationService : ForegroundThreadAffinitizedObject, IForegroundNotificationService
    {
        // how much time we will give notifications to run on the UI thread
        private const int DefaultTimeSliceInMS = 15;

        // Don't call NotifyOnForeground more than once per 50ms
        private const int MinimumDelayBetweenProcessing = 50;

        private static readonly Func<int, string> s_notifyOnForegroundLogger = c => string.Format("Processed : {0}", c);
        private readonly PriorityQueue _workQueue;

        private int _lastProcessedTimeInMS;

        [ImportingConstructor]
        public ForegroundNotificationService()
        {
            _workQueue = new PriorityQueue();
            _lastProcessedTimeInMS = Environment.TickCount;

            Contract.ThrowIfFalse(IsValid());
            Debug.Assert(IsForeground());
            Task.Factory.SafeStartNewFromAsync(ProcessAsync, CancellationToken.None, TaskScheduler.Default);
        }

        public void RegisterNotification(Action action, IAsyncToken asyncToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            RegisterNotification(action, DefaultTimeSliceInMS, asyncToken, cancellationToken);
        }

        public void RegisterNotification(Func<bool> action, IAsyncToken asyncToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            RegisterNotification(action, DefaultTimeSliceInMS, asyncToken, cancellationToken);
        }

        public void RegisterNotification(Action action, int delay, IAsyncToken asyncToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            Contract.Requires(delay >= 0);

            var current = Environment.TickCount;
{'fraction_non_alphanumeric': 0.0690343176376696, 'fraction_numerical': 0.0039904229848363925, 'mean_word_length': 3.866990291262136, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6101906', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1178, 'n_words': 314}