Ejemplo n.º 1
0
        /// <summary>
        /// Append attribute to base item when it is loaded
        /// </summary>
        /// <param name="item">Base item just read from datastore</param>
        public void OnItemRead(Item item)
        {
            var data = JavascriptItemDataRepo.Read(item);

            if (data != null)
            {
                item.Data.Add(data);
                item.AddProperty(typeof(JavascriptItemProperty), new JavascriptItemProperty()
                {
                });
                ExecuteFunction(item, "onItemRead", item);
            }
        }
Ejemplo n.º 2
0
 /// <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);
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Execute onTick javascript function on all items that have it
 /// </summary>
 public void OnTick()
 {
     if (TickCount++ >= TickInterval)
     {
         Logger.LogInformation("Running item scripts");
         var javascriptDatas = JavascriptItemDataRepo.Read();
         javascriptDatas.ForEach(data =>
         {
             if (data.ScriptFunctions.Contains(":onTick:", StringComparison.InvariantCultureIgnoreCase))
             {
                 var item = ItemManager.Read(data.DataItemId);
                 if (item == null)
                 {
                     throw new BeforeOurTimeException($"No item ({data.DataItemId}) found for javascript data ({data.Id})");
                 }
                 JavascriptItemDataManager.ExecuteFunction(item, "onTick");
             }
         });
         TickCount = 0;
     }
 }