Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically pair links and unlinks #84

Closed
samreid opened this issue Dec 29, 2015 · 2 comments
Closed

Automatically pair links and unlinks #84

samreid opened this issue Dec 29, 2015 · 2 comments

Comments

@samreid
Copy link
Member

samreid commented Dec 29, 2015

During the meeting on Dec 29, we discussed an API that automatically pairs link/unlink and addListener/removeListener, etc. This is not exclusive to axon, but would also appear in scenery, etc.

We discussed an API like this:

    var Disposable = function() {
      this.disposeActions = [];
    };
    Disposable.prototype.dispose = function() {
      for ( var i = 0; i < this.disposeActions.length; i++ ) {
        var disposeAction = this.disposeActions[ i ];
        disposeAction();
      }
    };
    Disposable.prototype.autolink = function() {
      property.link( listener );
      disposeActions.push( function() {
        property.unlink( listener );
      } );
    };

    this.autolink( myProperty, myListener );

Discussed in the context of phetsims/chipper#418

@jonathanolson
Copy link
Contributor

Most Scenery objects are reused, so they have initialize() and dispose(). Thus dispose would be called multiple times.

So if Scenery internal code were to use this:

  1. dispose() would need to be callable multiple times (actually clear the array)
  2. autolink shouldn't create a new function closure every time it's called (avoiding creation of objects, particularly when our disposable thing is pulled out of a pool).
  3. Would be ideal if it can be mixed in.

@samreid
Copy link
Member Author

samreid commented Dec 29, 2016

We agreed the current convention (dispose functions) is very clear and we do not want to investigate auto-unlink.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants