Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

38
LINES

< > BotCompany Repo | #1022885 // Dyn_FieldWatcher - watch a field in a DynModule and call Runnable in module queue. might work for non-persistent fields now too

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (16460L/99K).

sclass Dyn_FieldWatcher is AutoCloseable {
  DynModule module;
  S field;
  O value;
  Runnable action;
  
  Runnable changeListener = r check;
  VF1<S> fieldChangeListener = voidfunc(S f) {
    if (eq(f, field))
      check();
  };
  
  swappable O cloneValue(O o) { ret o; }
  
  *(DynModule module, S field, Runnable action) {
    this(module, field, action, null);
  }
  
  *(DynModule *module, S *field, Runnable *action, IF1 *cloneValue) {
    value = cloneValue(get(module, field));
    
    // Let's do both for safety
    module.onChange(changeListener);
    module.onFieldChange(fieldChangeListener);
  }
  
  void check {
    O newValue = cloneValue(get(module, field));
    if (eq(value, newValue)) ret;
    value = newValue;
    dm_q(module, action);
  }
  
  close {
    module.removeChangeListener(changeListener);
    module.removeFieldChangeListener(fieldChangeListener);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1022885
Snippet name: Dyn_FieldWatcher - watch a field in a DynModule and call Runnable in module queue. might work for non-persistent fields now too
Eternal ID of this version: #1022885/12
Text MD5: 45192cec7a0bebe1bf64b4a652878042
Transpilation MD5: 6b1d3f7eb1cc5648b4273db5bbae92c9
Author: stefan
Category: javax / stefan's os
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-21 18:37:01
Source code size: 931 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 401 / 973
Version history: 11 change(s)
Referenced in: [show references]