<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Miruken.Callback;
using Miruken.Graph;

namespace Miruken.Context
{
    public enum ContextState
    {
        Active = 0,
        Ending,
        Ended
    }

    public class Context : CompositeHandler,
        IHandlerAxis, ITraversing, IDisposable
	{
	    private EventHandlerList _events;
	    private readonly List<Context> _children;

	    public static readonly object AlreadyEnded = new object();
	    public static readonly object Unwinded     = new object();
	    public static readonly object Disposed     = new object();

        public Context()
		{
            State     = ContextState.Active;
            _children = new List<Context>();
            _events   = new EventHandlerList();
		}

		public Context(Context parent) : this()
		{
			Parent = parent;
		}

	    public ContextState State { get; private set; }

	    ITraversing ITraversing.Parent => Parent;

	    public Context Parent { get; }

	    public bool HasChildren => _children.Count > 0;

	    public Context Root
        {
            get
            {
                var root = this;
                while (root?.Parent != null)
                    root = root.Parent;
                return root;                  
            }
        }

	    public Context CreateChild()
	    {
            AssertActive();
	        var child = InternalCreateChild();
	        child.ContextEnding += (ctx, reason) => 
	            Raise(ContextEvents.ChildContextEnding, ctx, reason);
            child.ContextEnded += (ctx, reason) => {
                _children.Remove(ctx);
                Raise(ContextEvents.ChildContextEnded, ctx, reason);
            };
            _children.Add(child);
	        return child;
	    }

        ITraversing[] ITraversing.Children
        {
            get
            {
                return _children.Select(c => c as ITraversing).ToArray();
            }
        }

	    public Context[] Children => _children.ToArray();

	    protected virtual Context InternalCreateChild()
	    {
            return new Context(this);
	    }

	    public void Store(object data)
	    {
            if (data != null)
	            AddHandlers(data);
	    }

	    protected override bool HandleCallback(
            object callback, ref bool greedy, IHandler composer)
	    {
	        var handled = base.HandleCallback(callback, ref greedy, composer);
	        if (handled && !greedy)
	            return true;
	        var parent = Parent;
	        if (parent != null)
	            handled = handled | parent.Handle(callback, ref greedy, composer);<commit_msg>Return self in Context.Store<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Miruken.Callback;
using Miruken.Graph;

namespace Miruken.Context
{
    public enum ContextState
    {
        Active = 0,
        Ending,
        Ended
    }

    public class Context : CompositeHandler,
        IHandlerAxis, ITraversing, IDisposable
	{
	    private EventHandlerList _events;
	    private readonly List<Context> _children;

	    public static readonly object AlreadyEnded = new object();
	    public static readonly object Unwinded     = new object();
	    public static readonly object Disposed     = new object();

        public Context()
		{
            State     = ContextState.Active;
            _children = new List<Context>();
            _events   = new EventHandlerList();
		}

		public Context(Context parent) : this()
		{
			Parent = parent;
		}

	    public ContextState State { get; private set; }

	    ITraversing ITraversing.Parent => Parent;

	    public Context Parent { get; }

	    public bool HasChildren => _children.Count > 0;

	    public Context Root
        {
            get
            {
                var root = this;
                while (root?.Parent != null)
                    root = root.Parent;
                return root;                  
            }
        }

	    public Context CreateChild()
	    {
            AssertActive();
	        var child = InternalCreateChild();
	        child.ContextEnding += (ctx, reason) => 
	            Raise(ContextEvents.ChildContextEnding, ctx, reason);
            child.ContextEnded += (ctx, reason) => {
                _children.Remove(ctx);
                Raise(ContextEvents.ChildContextEnded, ctx, reason);
            };
            _children.Add(child);
	        return child;
	    }

        ITraversing[] ITraversing.Children
        {
            get
            {
                return _children.Select(c => c as ITraversing).ToArray();
            }
        }

	    public Context[] Children => _children.ToArray();

	    protected virtual Context InternalCreateChild()
	    {
            return new Context(this);
	    }

	    public Context Store(object data)
	    {
            if (data != null)
	            AddHandlers(data);
	        return this;
	    }

	    protected override bool HandleCallback(
            object callback, ref bool greedy, IHandler composer)
	    {
	        var handled = base.HandleCallback(callback, ref greedy, composer);
	        if (handled && !greedy)
	            return true;
	        var parent = Parent;
	        if (parent != null)
	            handled = handled | parent.Handle(callback, ref greedy, composer);
{'fraction_non_alphanumeric': 0.08463251670378619, 'fraction_numerical': 0.0007423904974016332, 'mean_word_length': 1.7778350515463917, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '5916052', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1334, 'n_words': 380}