/// <summary> /// Append attribute to base item when it is loaded /// </summary> /// <param name="item">Base item about to be persisted to datastore</param> public void OnItemUpdate(Item item) { if (item.HasData <JavascriptItemData>()) { var data = item.GetData <JavascriptItemData>(); if (data.Id == Guid.Empty) { OnItemCreate(item); } else { data.ScriptFunctions = GetFunctionDeclarations(data.Script); JavascriptItemDataRepo.Update(data); } } else if (JavascriptItemDataRepo.Read(item) is JavascriptItemData data) { JavascriptItemDataRepo.Delete(data); } }
/// <summary> /// Execute a javascript function of an item /// </summary> /// <param name="item"></param> /// <param name="name"></param> /// <param name="parameters"></param> /// <returns></returns> public object ExecuteFunction(Item item, string name, params object[] parameters) { object report = null; if (item.GetData <JavascriptItemData>() is JavascriptItemData data) { if (data.ScriptFunctions.Contains($":{name}:", StringComparison.InvariantCultureIgnoreCase) && data.Disabled == false) { try { lock (_lock) { JSEngine.EnterExecutionContext(JSEngine.GlobalEnvironment, JSEngine.GlobalEnvironment, null); SetupJintItem(JSEngine, item); JSEngine.Execute(data.Script); report = JSEngine.Invoke(name, parameters)?.ToObject(); JSEngine.LeaveExecutionContext(); } } catch (Exception e) { data.ErrCount += 1; data.ErrDescriptions += $"Executing {name}: {e.Message}\n"; Logger.LogException($"JS Engine executing {name} in {item.Id}", e); if (data.ErrCount >= 10) { data.Disabled = true; Logger.LogError($"Disabled javascript for item {item.Id}"); Logger.LogException($"JS Engine executing {name} in {item.Id}", e); } JavascriptItemDataRepo.Update(data); } } } return(report); }