public object AddSubscription(HttpRequestParams request) { var scriptId = request.GetRequiredGuid("scriptId"); var eventAlias = request.GetRequiredString("eventAlias"); var database = Context.Require <DatabasePlugin>(); if (!database.IsInitialized) { return(null); } using (var session = database.OpenSession()) { var guid = Guid.NewGuid(); var subscription = new ScriptEventHandler { Id = guid, EventAlias = eventAlias, UserScriptId = scriptId }; session.Set <ScriptEventHandler>().Add(subscription); session.SaveChanges(); return(guid); } }
public override void Start(StepRoutineInfo stepRoutine) { /* To achieve the effect we want a delegate that checks whether toCheck equals toWaitFor * and if it does proceed the StepRoutine using ProceedStepRoutine. * Then we want to subscribe that delegate to the eventToCheckOn so it will be called when * the event is raised. */ // Create the checking and proceeding delegate. checker = delegate { if (toCheck is null && toWaitFor is null) // Both are null? Proceed. { StepRoutine.ProceedStepRoutine(stepRoutine); return; } if (toCheck is null || toWaitFor is null) // Only one is null? Not there yet. { return; } if (toCheck.Equals(eventToCheckOn)) { StepRoutine.ProceedStepRoutine(stepRoutine); } }; // Subscribe the checking and proceeding delegate to the event so that it is called when // it is raised. eventToCheckOn.Subscribe(checker); }
/// <summary> /// Loads the script /// </summary> /// <param name="scriptPath">The script path.</param> public void LoadScript(string scriptPath) { if (!loadedScripts.ContainsKey(scriptPath)) { try { ScriptSource src = _engine.CreateScriptSourceFromFile(scriptPath); CompiledCode code = src.Compile(); ApplicationScript cpy = new ApplicationScript(scriptPath, code, _scope); loadedScripts.Add(scriptPath, cpy); ScriptEventHandler handler = this.ScriptLoaded; if (handler != null) { handler(cpy); } } catch (SyntaxErrorException ex) { ExceptionOperations eo = _engine.GetService <ExceptionOperations>(); string error = eo.FormatException(ex); string msg = "Syntax error in \"{0}\"{1}Details:{1}{2}"; msg = string.Format(msg, Path.GetFileName(scriptPath), Environment.NewLine, error); MessageService.ShowError(msg); } } }
async void MainWindow_Loaded(object sender, RoutedEventArgs eventArg) { // navigate the browser System.Windows.Navigation.LoadCompletedEventHandler loadedHandler = null; var loadedTcs = new TaskCompletionSource <bool>(); loadedHandler = (s, e) => { this.webBrowser.LoadCompleted -= loadedHandler; loadedTcs.SetResult(true); }; this.webBrowser.LoadCompleted += loadedHandler; this.webBrowser.Navigate("http://localhost:81/callback.html"); await loadedTcs.Task; // call the script method "scriptFuncAsync" var asyncScriptTcs = new TaskCompletionSource <object>(); var oncompleted = new ScriptEventHandler((ref object returnResult, object[] args) => { // we are here when the script has called us back asyncScriptTcs.SetResult(args[0]); }); this.webBrowser.InvokeScript("scriptFuncAsync", oncompleted); await asyncScriptTcs.Task; // show the result of the asyc call dynamic result = asyncScriptTcs.Task.Result; MessageBox.Show(result.outcome.ToString()); }
public object AddSubscription(dynamic args) { string pluginAlias = args.pluginAlias; string eventAlias = args.eventAlias; Guid scriptId = args.scriptId; using (var session = Context.OpenSession()) { var guid = Guid.NewGuid(); var script = session.Load <UserScript>(scriptId); var subscription = new ScriptEventHandler { Id = guid, PluginAlias = pluginAlias, EventAlias = eventAlias, UserScript = script }; session.Save(subscription); session.Flush(); return(guid); } }
protected virtual void onScriptUpdate(TaskEventArgs e) { ScriptEventHandler handler = scriptEventHandler; if (handler != null) { handler(this, e); } }
public override void Start(StepRoutineInfo stepRoutine) { /* To achieve the effect we want, we should create a delegate that calls ProceedStepRoutine. * We subscribe that delegate to the event. * When the event is raised ProceedStepRoutine will be called, then Cleanup will be called * from there and the StepRoutine will proceed. */ // Create the proceeder delegate. proceeder = delegate { StepRoutine.ProceedStepRoutine(stepRoutine); }; // Subscribe the delegate to the event so it will be called when the event is raised. eventToWaitFor.Subscribe(proceeder); }
public static IUnitOfWork AddEventHandlers(IUnitOfWork work, IServiceProvider provider) { var scriptEventHandler = new ScriptEventHandler(work.Broker); work.ScriptRepository.OnInsert += scriptEventHandler.HandleInsert; work.ScriptRepository.OnUpdate += scriptEventHandler.HandleUpdate; work.ScriptRepository.OnDelete += scriptEventHandler.HandleDelete; var propertiesEventHandler = new ConnectionPropertiesEventHandler(work.Broker); work.ConnectionPropertiesRepository.OnInsert += propertiesEventHandler.HandleInsert; work.ConnectionPropertiesRepository.OnUpdate += propertiesEventHandler.HandleUpdate; work.ConnectionPropertiesRepository.OnDelete += propertiesEventHandler.HandleDelete; return(work); }
////////////////////////////////////////////////////////////////////////// private ScriptScope ParseEventHandler(ref List <ScriptToken> .Enumerator TokenEnum) { ScriptToken Token = TokenEnum.Current; if (Token == null) { return(null); } // setup handler ScriptEventHandler Handler = new ScriptEventHandler(); Handler.Signature = Token.Value + " "; Handler.Line = Token.OrigLine; Handler.Filename = Token.Filename; // setup scope ScriptScope Scope = new ScriptScope(); Scope.Owner = Handler; Scope.StartLine = Token.StartLine; Scope.StartCol = Token.StartCol; while (TokenEnum.MoveNext()) { Token = TokenEnum.Current; if (Token.IsComment) { continue; } if (Token.IsString) { Handler.Signature += Token.Value; Handler.Name = Token.Value.Trim(new char[] { '\"' }); break; } else { return(null); } } EventHandlers.Add(Handler); return(Scope); }
public HttpHandlerResult AddSubscription(HttpRequestParams request) { var scriptId = request.GetRequiredGuid("scriptId"); var eventAlias = request.GetRequiredString("eventAlias"); using (var session = database.OpenSession()) { var subscriptionId = Guid.NewGuid(); var subscription = new ScriptEventHandler { Id = subscriptionId, EventAlias = eventAlias, UserScriptId = scriptId }; session.Set<ScriptEventHandler>().Add(subscription); session.SaveChanges(); return HttpHandlerResult.Json(new { subscriptionId }) ; } }
public object AddSubscription(HttpRequestParams request) { string eventAlias = request.GetRequiredString("eventAlias"); Guid scriptId = request.GetRequiredGuid("scriptId"); using (var session = Context.OpenSession()) { var guid = Guid.NewGuid(); var script = session.Load <UserScript>(scriptId); var subscription = new ScriptEventHandler { Id = guid, EventAlias = eventAlias, UserScript = script }; session.Save(subscription); session.Flush(); return(guid); } }
public override LSL_List xmrEventDequeue(double timeout, int returnMask1, int returnMask2, int backgroundMask1, int backgroundMask2) { DateTime sleepUntil = DateTime.UtcNow + TimeSpan.FromMilliseconds(timeout * 1000.0); EventParams evt = null; int callNo, evc2; int evc1 = 0; int mask1 = returnMask1 | backgroundMask1; // codes 00..31 int mask2 = returnMask2 | backgroundMask2; // codes 32..63 LinkedListNode <EventParams> lln = null; object[] sv; ScriptEventCode evc = ScriptEventCode.None; callNo = -1; try { if (callMode == CallMode_NORMAL) { goto findevent; } // Stack frame is being restored as saved via CheckRun...(). // Restore necessary values then jump to __call<n> label to resume processing. sv = RestoreStackFrame("xmrEventDequeue", out callNo); sleepUntil = DateTime.Parse((string)sv[0]); returnMask1 = (int)sv[1]; returnMask2 = (int)sv[2]; mask1 = (int)sv[3]; mask2 = (int)sv[4]; switch (callNo) { case 0: goto __call0; case 1: { evc1 = (int)sv[5]; evc = (ScriptEventCode)(int)sv[6]; DetectParams[] detprms = ObjArrToDetPrms((object[])sv[7]); object[] ehargs = (object[])sv[8]; evt = new EventParams(evc.ToString(), ehargs, detprms); goto __call1; } } throw new ScriptBadCallNoException(callNo); // Find first event that matches either the return or background masks. findevent: Monitor.Enter(m_QueueLock); for (lln = m_EventQueue.First; lln != null; lln = lln.Next) { evt = lln.Value; evc = (ScriptEventCode)Enum.Parse(typeof(ScriptEventCode), evt.EventName); evc1 = (int)evc; evc2 = evc1 - 32; if ((((uint)evc1 < (uint)32) && (((mask1 >> evc1) & 1) != 0)) || (((uint)evc2 < (uint)32) && (((mask2 >> evc2) & 1) != 0))) { goto remfromq; } } // Nothing found, sleep while one comes in. m_SleepUntil = sleepUntil; m_SleepEventMask1 = mask1; m_SleepEventMask2 = mask2; Monitor.Exit(m_QueueLock); suspendOnCheckRunTemp = true; callNo = 0; __call0: CheckRunQuick(); goto checktmo; // Found one, remove it from queue. remfromq: m_EventQueue.Remove(lln); if ((uint)evc1 < (uint)m_EventCounts.Length) { m_EventCounts[evc1]--; } Monitor.Exit(m_QueueLock); m_InstEHEvent++; // See if returnable or background event. if ((((uint)evc1 < (uint)32) && (((returnMask1 >> evc1) & 1) != 0)) || (((uint)evc2 < (uint)32) && (((returnMask2 >> evc2) & 1) != 0))) { // Returnable event, return its parameters in a list. // Also set the detect parameters to what the event has. int plen = evt.Params.Length; object[] plist = new object[plen + 1]; plist[0] = (LSL_Integer)evc1; for (int i = 0; i < plen;) { object ob = evt.Params[i]; if (ob is int) { ob = (LSL_Integer)(int)ob; } else if (ob is double) { ob = (LSL_Float)(double)ob; } else if (ob is string) { ob = (LSL_String)(string)ob; } plist[++i] = ob; } m_DetectParams = evt.DetectParams; return(new LSL_List(plist)); } // It is a background event, simply call its event handler, // then check event queue again. callNo = 1; __call1: ScriptEventHandler seh = m_ObjCode.scriptEventHandlerTable[stateCode, evc1]; if (seh == null) { goto checktmo; } DetectParams[] saveDetParams = this.m_DetectParams; object[] saveEHArgs = this.ehArgs; ScriptEventCode saveEventCode = this.eventCode; this.m_DetectParams = evt.DetectParams; this.ehArgs = evt.Params; this.eventCode = evc; try { seh(this); } finally { this.m_DetectParams = saveDetParams; this.ehArgs = saveEHArgs; this.eventCode = saveEventCode; } // Keep waiting until we find a returnable event or timeout. checktmo: if (DateTime.UtcNow < sleepUntil) { goto findevent; } // We timed out, return an empty list. return(emptyList); } finally { if (callMode != CallMode_NORMAL) { // Stack frame is being saved by CheckRun...(). // Save everything we need at the __call<n> labels so we can restore it // when we need to. sv = CaptureStackFrame("xmrEventDequeue", callNo, 9); sv[0] = sleepUntil.ToString(); // needed at __call0,__call1 sv[1] = returnMask1; // needed at __call0,__call1 sv[2] = returnMask2; // needed at __call0,__call1 sv[3] = mask1; // needed at __call0,__call1 sv[4] = mask2; // needed at __call0,__call1 if (callNo == 1) { sv[5] = evc1; // needed at __call1 sv[6] = (int)evc; // needed at __call1 sv[7] = DetPrmsToObjArr(evt.DetectParams); // needed at __call1 sv[8] = evt.Params; // needed at __call1 } } } }
////////////////////////////////////////////////////////////////////////// private ScriptScope ParseEventHandler(ref List<ScriptToken>.Enumerator TokenEnum) { ScriptToken Token = TokenEnum.Current; if (Token == null) return null; // setup handler ScriptEventHandler Handler = new ScriptEventHandler(); Handler.Signature = Token.Value + " "; Handler.Line = Token.OrigLine; Handler.Filename = Token.Filename; // setup scope ScriptScope Scope = new ScriptScope(); Scope.Owner = Handler; Scope.StartLine = Token.StartLine; Scope.StartCol = Token.StartCol; while (TokenEnum.MoveNext()) { Token = TokenEnum.Current; if (Token.IsComment) continue; if (Token.IsString) { Handler.Signature += Token.Value; Handler.Name = Token.Value.Trim(new char[] { '\"' }); break; } else return null; } EventHandlers.Add(Handler); return Scope; }