[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/ExtrieveTechnologies/ClearScript/master/ClearScript/HostEvent.cs [Back]  [Original]

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Reflection;
using Microsoft.ClearScript.Util;

namespace Microsoft.ClearScript
{
    /// 
    /// Represents a host event source.
    /// 
    /// The event handler delegate type.
    public class EventSource
    {
        private readonly ScriptEngine engine;
        private readonly object source;
        private readonly EventInfo eventInfo;

        internal EventSource(ScriptEngine engine, object source, EventInfo eventInfo)
        {
            MiscHelpers.VerifyNonNullArgument(engine, "engine");
            MiscHelpers.VerifyNonNullArgument(eventInfo, "eventInfo");
            if (eventInfo.EventHandlerType != typeof(T))
            {
                throw new ArgumentException("Invalid event type", "eventInfo");
            }

            this.engine = engine;
            this.source = source;
            this.eventInfo = eventInfo;
        }

        internal object Source
        {
            get { return source; }
        }

        internal EventInfo EventInfo
        {
            get { return eventInfo; }
        }

        #region script-callable interface

        // ReSharper disable InconsistentNaming

        /// 
        /// Connects the host event source to the specified script handler function.
        /// 
        /// The script function that will handle the event.
        /// An  that represents the connection.
        public EventConnection connect(object scriptFunc)
        {
            MiscHelpers.VerifyNonNullArgument(scriptFunc, "scriptFunc");
            var handler = DelegateFactory.CreateDelegate(engine, scriptFunc, eventInfo.EventHandlerType);
            eventInfo.AddEventHandler(source, handler);
            return new EventConnection(source, eventInfo, handler);
        }

        // ReSharper restore InconsistentNaming

        #endregion
    }

    /// 
    /// Represents a connection between a host event source and a script handler function.
    /// 
    /// The event handler delegate type.
    public class EventConnection
    {
        private object source;
        private EventInfo eventInfo;
        private Delegate handler;

        internal EventConnection(object source, EventInfo eventInfo, Delegate handler)
        {
            MiscHelpers.VerifyNonNullArgument(handler, "handler");
            MiscHelpers.VerifyNonNullArgument(eventInfo, "eventInfo");
            if (eventInfo.EventHandlerType != typeof(T))
            {
                throw new ArgumentException("Invalid event type", "eventInfo");
            }

            this.source = source;
            this.eventInfo = eventInfo;
            this.handler = handler;
        }

        #region script-callable interface

        // ReSharper disable InconsistentNaming

        /// 
        /// Disconnects the host event source from the script handler function.
        /// 
        public void disconnect()
        {
            if (handler != null)
            {
                eventInfo.RemoveEventHandler(source, handler);
                source = null;
                eventInfo = null;
                handler = null;
            }
        }

        // ReSharper restore InconsistentNaming

        #endregion
    }
}

Web Proxy Viewer  |  New URL  |  Original Page