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
121 lines (95 loc) · 3.72 KB
/
HostEvent.cs
File metadata and controls
121 lines (95 loc) · 3.72 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
113
114
115
116
117
118
119
120
121
// 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;
internal EventSource(ScriptEngine engine, object source, EventInfo eventInfo)
{
MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
MiscHelpers.VerifyNonNullArgument(eventInfo, nameof(eventInfo));
if (eventInfo.EventHandlerType != typeof(T))
{
throw new ArgumentException("Invalid event type", nameof(eventInfo));
}
this.engine = engine;
Source = source;
EventInfo = eventInfo;
}
internal object Source { get; }
internal EventInfo EventInfo { get; }
#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, nameof(scriptFunc));
return engine.CreateEventConnection<T>(Source, EventInfo, DelegateFactory.CreateDelegate(engine, scriptFunc, typeof(T)));
}
// ReSharper restore InconsistentNaming
#endregion
}
internal interface IEventConnection
{
void Break();
}
/// <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> : IEventConnection
{
private readonly ScriptEngine engine;
private readonly object source;
private readonly EventInfo eventInfo;
private readonly Delegate handler;
private readonly InterlockedOneWayFlag brokenFlag = new InterlockedOneWayFlag();
internal EventConnection(ScriptEngine engine, object source, EventInfo eventInfo, Delegate handler)
{
MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
MiscHelpers.VerifyNonNullArgument(handler, nameof(handler));
MiscHelpers.VerifyNonNullArgument(eventInfo, nameof(eventInfo));
if (eventInfo.EventHandlerType != typeof(T))
{
throw new ArgumentException("Invalid event type", nameof(eventInfo));
}
this.engine = engine;
this.source = source;
this.eventInfo = eventInfo;
this.handler = handler;
eventInfo.AddEventHandler(source, handler);
}
#region script-callable interface
// ReSharper disable InconsistentNaming
/// <summary>
/// Disconnects the host event source from the script handler function.
/// </summary>
public void disconnect()
{
engine.BreakEventConnection(this);
}
// ReSharper restore InconsistentNaming
#endregion
#region IEventConnection implementation
void IEventConnection.Break()
{
if (brokenFlag.Set())
{
eventInfo.RemoveEventHandler(source, handler);
}
}
#endregion
}
}