forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostEvent.cs
More file actions
112 lines (93 loc) · 3.46 KB
/
HostEvent.cs
File metadata and controls
112 lines (93 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Reflection;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript
{
/// <summary>
/// Represents a host event source.
/// </summary>
/// <typeparam name="T">The event handler delegate type.</typeparam>
public class EventSource<T>
{
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
/// <summary>
/// Connects the host event source to the specified script handler function.
/// </summary>
/// <param name="scriptFunc">The script function that will handle the event.</param>
/// <returns>An <see cref="EventConnection{T}"/> that represents the connection.</returns>
public EventConnection<T> connect(object scriptFunc)
{
MiscHelpers.VerifyNonNullArgument(scriptFunc, "scriptFunc");
var handler = DelegateFactory.CreateDelegate(engine, scriptFunc, eventInfo.EventHandlerType);
eventInfo.AddEventHandler(source, handler);
return new EventConnection<T>(source, eventInfo, handler);
}
// ReSharper restore InconsistentNaming
#endregion
}
/// <summary>
/// Represents a connection between a host event source and a script handler function.
/// </summary>
/// <typeparam name="T">The event handler delegate type.</typeparam>
public class EventConnection<T>
{
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
/// <summary>
/// Disconnects the host event source from the script handler function.
/// </summary>
public void disconnect()
{
if (handler != null)
{
eventInfo.RemoveEventHandler(source, handler);
source = null;
eventInfo = null;
handler = null;
}
}
// ReSharper restore InconsistentNaming
#endregion
}
}