svghmi/widgets_common.ysl2
changeset 3504 9d895a103019
parent 3473 1202b47e7e63
child 3520 b27e50143083
equal deleted inserted replaced
3503:49fcd5d62139 3504:9d895a103019
   179             this.minmaxes = minmaxes;
   179             this.minmaxes = minmaxes;
   180             Object.keys(members).forEach(prop => this[prop]=members[prop]);
   180             Object.keys(members).forEach(prop => this[prop]=members[prop]);
   181             this.lastapply = indexes.map(() => undefined);
   181             this.lastapply = indexes.map(() => undefined);
   182             this.inhibit = indexes.map(() => undefined);
   182             this.inhibit = indexes.map(() => undefined);
   183             this.pending = indexes.map(() => undefined);
   183             this.pending = indexes.map(() => undefined);
   184             this.bound_unhinibit = this.unhinibit.bind(this);
   184             this.bound_uninhibit = this.uninhibit.bind(this);
       
   185 
       
   186             this.lastdispatch = indexes.map(() => undefined);
       
   187             this.deafen = indexes.map(() => undefined);
       
   188             this.incoming = indexes.map(() => undefined);
       
   189             this.bound_undeafen = this.undeafen.bind(this);
       
   190 
   185             this.forced_frequency = freq;
   191             this.forced_frequency = freq;
   186             this.clip = true;
   192             this.clip = true;
   187         }
   193         }
   188 
   194 
   189         do_init(){
   195         do_init(){
   223                     /* flush updates pending because of inhibition */
   229                     /* flush updates pending because of inhibition */
   224                     let inhibition = this.inhibit[i];
   230                     let inhibition = this.inhibit[i];
   225                     if(inhibition != undefined){
   231                     if(inhibition != undefined){
   226                         clearTimeout(inhibition);
   232                         clearTimeout(inhibition);
   227                         this.lastapply[i] = undefined;
   233                         this.lastapply[i] = undefined;
   228                         this.unhinibit(i);
   234                         this.uninhibit(i);
       
   235                     }
       
   236                     let deafened = this.deafen[i];
       
   237                     if(deafened != undefined){
       
   238                         clearTimeout(deafened);
       
   239                         this.lastdispatch[i] = undefined;
       
   240                         this.undeafen(i);
   229                     }
   241                     }
   230                     let index = this.indexes[i];
   242                     let index = this.indexes[i];
   231                     if(this.relativeness[i])
   243                     if(this.relativeness[i])
   232                         index += this.offset;
   244                         index += this.offset;
   233                     subscribers(index).delete(this);
   245                     subscribers(index).delete(this);
   311             if(this.clip)
   323             if(this.clip)
   312                 new_val = this.clip_min_max(index, new_val);
   324                 new_val = this.clip_min_max(index, new_val);
   313             return apply_hmi_value(realindex, new_val);
   325             return apply_hmi_value(realindex, new_val);
   314         }
   326         }
   315 
   327 
   316         unhinibit(index){
   328         uninhibit(index){
   317             this.inhibit[index] = undefined;
   329             this.inhibit[index] = undefined;
   318             let new_val = this.pending[index];
   330             let new_val = this.pending[index];
   319             this.pending[index] = undefined;
   331             this.pending[index] = undefined;
   320             return this.apply_hmi_value(index, new_val);
   332             return this.apply_hmi_value(index, new_val);
   321         }
   333         }
   330                     return this._apply_hmi_value(index, new_val);
   342                     return this._apply_hmi_value(index, new_val);
   331                 }
   343                 }
   332                 else {
   344                 else {
   333                     let elapsed = now - lastapply;
   345                     let elapsed = now - lastapply;
   334                     this.pending[index] = new_val;
   346                     this.pending[index] = new_val;
   335                     this.inhibit[index] = setTimeout(this.bound_unhinibit, min_interval - elapsed, index);
   347                     this.inhibit[index] = setTimeout(this.bound_uninhibit, min_interval - elapsed, index);
   336                 }
   348                 }
   337             }
   349             }
   338             else {
   350             else {
   339                 this.pending[index] = new_val;
   351                 this.pending[index] = new_val;
   340                 return new_val;
   352                 return new_val;
   352                     break;
   364                     break;
   353                 }
   365                 }
   354             }
   366             }
   355         }
   367         }
   356         
   368         
       
   369         undeafen(index){
       
   370             this.deafen[index] = undefined;
       
   371             let [new_val, old_val] = this.incoming[index];
       
   372             this.incoming[index] = undefined;
       
   373             this.dispatch(new_val, old_val, index);
       
   374         }
       
   375 
   357         _dispatch(value, oldval, varnum) {
   376         _dispatch(value, oldval, varnum) {
   358             let dispatch = this.dispatch;
   377             let dispatch = this.dispatch;
   359             if(dispatch != undefined){
   378             if(dispatch != undefined){
   360                 try {
   379                 if(this.deafen[varnum] == undefined){
   361                     dispatch.call(this, value, oldval, varnum);
   380                     let now = Date.now();
   362                 } catch(err) {
   381                     let min_interval = 1000/this.frequency;
   363                     console.log(err);
   382                     let lastdispatch = this.lastdispatch[varnum];
       
   383                     if(lastdispatch == undefined || now > lastdispatch + min_interval){
       
   384                         this.lastdispatch[varnum] = now;
       
   385                         try {
       
   386                             dispatch.call(this, value, oldval, varnum);
       
   387                         } catch(err) {
       
   388                             console.log(err);
       
   389                         }
       
   390                     }
       
   391                     else {
       
   392                         let elapsed = now - lastdispatch;
       
   393                         this.incoming[varnum] = [value, oldval];
       
   394                         this.deafen[varnum] = setTimeout(this.bound_undeafen, min_interval - elapsed, varnum);
       
   395                     }
       
   396                 }
       
   397                 else {
       
   398                     this.incoming[varnum] = [value, oldval];
   364                 }
   399                 }
   365             }
   400             }
   366         }
   401         }
   367 
   402 
   368         _animate(){
   403         _animate(){