Context bound events

SSharp S# API

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

All event subscriptions in S# are controlled by a specific component called event manager. It stores in-memory cache of all S# function subscriptions to .NET events. Each time script execute += operator new record will be added to event manager cache. Each event will be handled in the same context as a script containing this event function.

Here is an example which demonstrate this case (C#):

 

      Script s = Script.Compile(
         @"
            invoked = false;
            function handler(s,e) global(invoked) {
             invoked = true;
            }
            test = new EventSource();
            test.NameChanged += handler;
            return test;
          "
         );
      EventSource resultVal =(EventSource)s.Execute();
      Assert.IsFalse((bool)s.Context.GetItem("invoked", false));
      //At this point event will be executed in the script's context, i.e. s.Context
      resultVal.Name = "TestName";
      //Now, check that the value in context was changed
      Assert.IsTrue((bool)s.Context.GetItem("invoked", false));

 

Event Source class

 
  public class EventSource
  {
    string name;
    public string Name
    {
      get
      {
        return name;
      }
      set
      {
        name = value;
        if (NameChanged != null)
          NameChanged.Invoke(this, EventArgs.Empty);
      }
    }
    public event EventHandler<EventArgs> NameChanged;
  }