<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;

namespace Totem.Runtime.Timeline
{
	/// <summary>
	/// A queue of timeline points with asynchronous dequeues for the whole timeline
	/// </summary>
	internal sealed class TimelineQueue : Connection
	{
    readonly Subject<TimelineMessage> _messages = new Subject<TimelineMessage>();
    readonly Subject<TimelinePoint> _schedulePoints = new Subject<TimelinePoint>();
    readonly ITimelineDb _db;
    readonly TimelineSchedule _schedule;
    readonly TimelineFlowSet _flows;
		readonly TimelineRequestSet _requests;
    readonly EnqueueTransactionSet _transactions;
    Task _resumeTask;

    internal TimelineQueue(
      ITimelineDb db,
      TimelineSchedule schedule,
      TimelineFlowSet flows,
      TimelineRequestSet requests)
		{
      _db = db;
			_schedule = schedule;
			_flows = flows;
			_requests = requests;

      _transactions = new EnqueueTransactionSet(this);
		}

    protected override void Open()
    {
      ObserveMessages();

      _resumeTask = Resume();
    }

    void ObserveMessages()
    {
      Track(_messages
        .ObserveOn(TimelineScheduler.Instance)
        .Subscribe(message =>
      {
        _flows.Push(message);
        _requests.Push(message);
      }));

      Track(_schedulePoints
        .ObserveOn(TimelineScheduler.Instance)
        .Subscribe(_schedule.Push));
    }

    async Task Resume()
    {
      await Task.Delay(500);

      try
      {
        var info = await _db.ReadResumeInfo();

        if(info.EventCount == 0)
        {
          Log.Info("[timeline] Resumed activity");
        }
        else
        {
          _flows.ResumeWith(info);

          ResumeWith(info);
        }
      }
      catch(Exception error)
      {
        Log.Error(error, "[timeline] HALTED; failed to resume activity");
      }
      finally<commit_msg>Stop runtime if the timeline fails to resume<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;

namespace Totem.Runtime.Timeline
{
	/// <summary>
	/// A queue of timeline points with asynchronous dequeues for the whole timeline
	/// </summary>
	internal sealed class TimelineQueue : Connection
	{
    readonly Subject<TimelineMessage> _messages = new Subject<TimelineMessage>();
    readonly Subject<TimelinePoint> _schedulePoints = new Subject<TimelinePoint>();
    readonly ITimelineDb _db;
    readonly TimelineSchedule _schedule;
    readonly TimelineFlowSet _flows;
		readonly TimelineRequestSet _requests;
    readonly EnqueueTransactionSet _transactions;
    Task _resumeTask;

    internal TimelineQueue(
      ITimelineDb db,
      TimelineSchedule schedule,
      TimelineFlowSet flows,
      TimelineRequestSet requests)
		{
      _db = db;
			_schedule = schedule;
			_flows = flows;
			_requests = requests;

      _transactions = new EnqueueTransactionSet(this);
		}

    protected override void Open()
    {
      ObserveMessages();

      _resumeTask = Resume();
    }

    void ObserveMessages()
    {
      Track(_messages
        .ObserveOn(TimelineScheduler.Instance)
        .Subscribe(message =>
      {
        _flows.Push(message);
        _requests.Push(message);
      }));

      Track(_schedulePoints
        .ObserveOn(TimelineScheduler.Instance)
        .Subscribe(_schedule.Push));
    }

    async Task Resume()
    {
      await Task.Delay(500);

      try
      {
        var info = await _db.ReadResumeInfo();

        if(info.EventCount == 0)
        {
          Log.Info("[timeline] Resumed activity");
        }
        else
        {
          _flows.ResumeWith(info);

          ResumeWith(info);
        }
      }
      finally
{'fraction_non_alphanumeric': 0.08876629889669008, 'fraction_numerical': 0.0020060180541624875, 'mean_word_length': 2.666360294117647, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '1552946', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1121, 'n_words': 257}