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

16
LINES

< > BotCompany Repo | #1034149 // addWeakChangeListener - listener with weak reference to target that auto-removes itself when the target has been garbage collected

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

Libraryless. Click here for Pure Java version (5665L/31K).

1  
svoid addWeakChangeListener(IHasChangeListeners src, ChangeTriggerable target) {
2  
  addWeakChangeListener(src, target, o -> o.change());
3  
}
4  
5  
svoid <A> addWeakChangeListener(IHasChangeListeners src, A target, IVF1<A> action) {
6  
  if (src == null || action == null || target == null) ret;
7  
  
8  
  var ref = weakRef(target);
9  
  src.onChange(r {
10  
    var target = ref!;
11  
    if (target == null)
12  
      src.removeChangeListener(this);
13  
    else
14  
      action.get(target);
15  
  });
16  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034149
Snippet name: addWeakChangeListener - listener with weak reference to target that auto-removes itself when the target has been garbage collected
Eternal ID of this version: #1034149/4
Text MD5: ee190d0f660fa4a1897b7b0f5a3d043d
Transpilation MD5: b369311b638d3f84cebf79f2099bd919
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-08 05:43:51
Source code size: 477 bytes / 16 lines
Pitched / IR pitched: No / No
Views / Downloads: 170 / 275
Version history: 3 change(s)
Referenced in: [show references]