Skip to content

Commit

Permalink
Заготовка для #860
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilBeaver committed Jul 24, 2021
1 parent 0e259aa commit e89cbad
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/ScriptEngine.HostedScript/DefaultEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public void AddHandler(
}

handlers[eventName].Add(handlerScript, handlerMethod);
if (eventSource is IEventSourceNotify notify)
{
notify.OnSubscribe(eventName, handlerScript);
}
}

public void RemoveHandler(
Expand All @@ -90,6 +94,10 @@ public void RemoveHandler(
if (_registeredHandlers.TryGetValue(eventSource, out var handlers))
{
handlers[eventName].Remove(handlerScript, handlerMethod);
if (eventSource is IEventSourceNotify notify)
{
notify.OnUnsubscribe(eventName, handlerScript);
}
}
}

Expand Down
44 changes: 43 additions & 1 deletion src/ScriptEngine.HostedScript/Library/ConsoleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ namespace ScriptEngine.HostedScript.Library
/// Предназначен для низкоуровнего манипулирования выводом в консоль.
/// </summary>
[ContextClass("Консоль", "Console")]
public class ConsoleContext : AutoContext<ConsoleContext>
public class ConsoleContext : AutoContext<ConsoleContext>, IEventSourceNotify
{
private int _cancelKeyHandlers;
private IEventProcessor _eventProcessor;

[ContextProperty("НажатаКлавиша", "KeyPressed")]
public bool HasKey
{
Expand Down Expand Up @@ -270,6 +273,45 @@ public static ConsoleContext Constructor()

return provider.Console;
}

public void OnSubscribe(string eventName, IRuntimeContextInstance target)
{
if (_cancelKeyHandlers == 0)
{
Console.CancelKeyPress += CancelKeyHandler;
_eventProcessor = MachineInstance.Current.EventProcessor;
}

++_cancelKeyHandlers;
}

public void OnUnsubscribe(string eventName, IRuntimeContextInstance target)
{
if(_cancelKeyHandlers == 0)
return;

if (--_cancelKeyHandlers == 0)
{
Console.CancelKeyPress -= CancelKeyHandler;
_eventProcessor = null;
}
}

private void CancelKeyHandler(object sender, ConsoleCancelEventArgs args)
{
if(_eventProcessor == null)
return;

var cancelVariable = Variable.Create(ValueFactory.Create(false), "Отказ");
var scriptArgs = new IValue[] { cancelVariable };

_eventProcessor.HandleEvent(this, "CancelKeyPress", scriptArgs);

if (cancelVariable.DataType == DataType.Boolean)
{
args.Cancel = cancelVariable.AsBoolean();
}
}
}


Expand Down
16 changes: 16 additions & 0 deletions src/ScriptEngine/Machine/IEventSourceNotify.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/

namespace ScriptEngine.Machine
{
public interface IEventSourceNotify
{
void OnSubscribe(string eventName, IRuntimeContextInstance target);

void OnUnsubscribe(string eventName, IRuntimeContextInstance target);
}
}
14 changes: 14 additions & 0 deletions tests/console.os
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@

Процедура ТестДолжен_ПроверитьПеренаправлениеВывода() Экспорт

ДобавитьОбработчик Консоль.CancelKeyPress, ЭтотОбъект.Обработчик;

Сч = 0;
Пока Сч < 20 Цикл
Приостановить(1000);
Сообщить(Сч);
Сч = Сч + 1;
КонецЦикла;

ВФ = ПолучитьИмяВременногоФайла();
Поток = ФайловыеПотоки.ОткрытьДляЗаписи(ВФ);
Консоль.УстановитьПотокВывода(Поток);
Expand All @@ -92,4 +101,9 @@

юТест.ПроверитьРавенство("Привет мир!", СокрЛП(Текст));

КонецПроцедуры

Процедура Обработчик(Отказ) Экспорт
Сообщить("Обработчик вызван");
Отказ = Истина;
КонецПроцедуры

0 comments on commit e89cbad

Please sign in to comment.