No way to destroy an action #120

Closed
ds0nt opened this Issue Nov 12, 2014 · 1 comment

Comments

Projects
None yet
2 participants
@ds0nt

ds0nt commented Nov 12, 2014

Is there a way to destroy an action? I peeked at the code and didn't see anything and skimmed the npm and github pages.

Did I miss it?

Use Case:

<PrettyButton> //Generates an action fired onClick
  <PrettyButtonDialog action={prettybutton's action}> //Subscribes to prettybutton's action and toggles it's visibility
</PrettyButton>

It has advantage over function, because I don't need to define an onclick from the container of these elements to proxy elements from PrettyButton into PrettyButton.props.children. They can all just listen to the action. And I can still use the same PrettyButton for many different Children

Problem is I don't want to leak actions everywhere... (when prettybutton dismounts and remounts)

@spoike

This comment has been minimized.

Show comment
Hide comment
@spoike

spoike Nov 12, 2014

Member

That's not really how actions are designed to be used. You only create one action per data-flow that you want to initiate, which is done when the application is bootstrapping, and you're not supposed to "destroy" the actions (at least they weren't designed to be). You may however unsubscribe to them as subscriptions return an unsubscription function.

You can write a mixin that hooks up the action and unsubscribes like this:

var PrettyButtonDialogMixin = {
    componentDidMount: function() {
        this._unsubFunc = this.props.action.listen(this.onAction, this);
    },
    componentDidUnmount: function() {
        if (this._unsubFunc) {
            this._unsubFunc():
        }
    }
};

... and use it in your PrettyButtonDialog:

var PrettyButtonDialog = {
    mixins: [PrettyButtonDialogMixin],
    render: function() { /* snip snip */ }
};

Hope this helps!

Member

spoike commented Nov 12, 2014

That's not really how actions are designed to be used. You only create one action per data-flow that you want to initiate, which is done when the application is bootstrapping, and you're not supposed to "destroy" the actions (at least they weren't designed to be). You may however unsubscribe to them as subscriptions return an unsubscription function.

You can write a mixin that hooks up the action and unsubscribes like this:

var PrettyButtonDialogMixin = {
    componentDidMount: function() {
        this._unsubFunc = this.props.action.listen(this.onAction, this);
    },
    componentDidUnmount: function() {
        if (this._unsubFunc) {
            this._unsubFunc():
        }
    }
};

... and use it in your PrettyButtonDialog:

var PrettyButtonDialog = {
    mixins: [PrettyButtonDialogMixin],
    render: function() { /* snip snip */ }
};

Hope this helps!

@spoike spoike added the question label Nov 12, 2014

@spoike spoike closed this Nov 13, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment