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

[MobX 4] Action decorator being non-configurable breaks further decoration #1477

Closed
JabX opened this issue Apr 4, 2018 · 8 comments
Closed

Comments

@JabX
Copy link

JabX commented Apr 4, 2018

Hello,

I've been successfully using @action decorators along side a global @autobind decorator on the class (from core-decorators) for a while, but since I upgraded to MobX 4 it just stopped working altogether.

The @autobind decorator I use breaks here, because it tries to redefine the property defined by @action there, only because MobX (explicitely) defines the property as non configurable.

Is there a particuliar reason why this behavior was chosen ? I've read on other issues that all decorator stuff was put on hold until the spec finalizes, but the fix looks simple (unless I'm missing something) and it's a pretty major regression for me.

I don't see any other alternative than either:

  • request a fix on the other library to stop trying to redefine non configurable properties (forcing me to replace all my @actions by @action.bounds)
  • remove every @autobind class decorator and put them on each method instead, along with @action.bound where applicable
  • go back to MobX 3

And I'd like to avoid going to such lengths.

Thanks for your time.

@mweststrate
Copy link
Member

mweststrate commented Apr 4, 2018 via email

@JabX
Copy link
Author

JabX commented Apr 4, 2018

Well, @action.bound is supposed to be used for actions, which may not always represent all methods of a class, and a global @autobind decorator on a class represents far less noise to me than decorating each and every method (or adding .bound on all actions).

In any case, everybody is free to choose the binding method that works best for him/her, but mine doesn't work anymore in MobX 4 because of a tiny configurable: false, which might probably be unnecessary. If there's a valid reason for it then I guess I'll have to adapt, but if not I'd rather like for it to be changed.

@Strate
Copy link
Contributor

Strate commented Apr 9, 2018

@mweststrate unfortunatelly this is huge break for upgrading from mobx3 to mobx4 :(

UPD also this is not caught by typescript, so this become a real pain :(

@mweststrate
Copy link
Member

mweststrate commented Apr 9, 2018 via email

@Strate
Copy link
Contributor

Strate commented Apr 9, 2018

Temporary fixed as making middleware module, which exports custom action and action.bound functions, which calls to original mobx function and removes from there configurable property. Middleware module applied to whole project by webpack's NormalModuleReplacementPlugin:

fixMobx.ts:

export * from "mobx"

// TODO Remove after resolve of https://github.com/mobxjs/mobx/issues/1477

import {action as mobxAction} from "mobx"

export const action: typeof mobxAction = function(...args: any[]): any {
    const res = mobxAction.apply(this, args);
    if (typeof res === "object" && "configurable" in res && res.configurable === false) {
        delete res.configurable
    }
    return res;
} as typeof mobxAction

action.bound = function(...args: any[]): any {
    const res = mobxAction.apply(this, args);
    if (typeof res === "object" && "configurable" in res && res.configurable === false) {
        delete res.configurable
    }
    return res;
}

webpack.config.js:

new webpack.NormalModuleReplacementPlugin(/^mobx$/, function(resource) {
    // TODO Remove after resolve of https://github.com/mobxjs/mobx/issues/1477
    // Don't forget to adjust path to fixMobx.ts to your project
    if (resource.contextInfo.issuer !== path.join(srcDir, "web", "lib", "fixMobx.ts")) {
        resource.request = path.join(srcDir, "web", "lib", "fixMobx.ts")
    }
})

@hector
Copy link
Contributor

hector commented Apr 10, 2018

I am looking forward for this fix. I'm holding upgrade to version 4 until then

@rkuykendall
Copy link

Just more data for the team: I use class-autobind-decorator on my classes. Currently this is incompatible with mobx 4.

@mweststrate
Copy link
Member

Fixed and released as 4.2.0. Actions are now always configurable and writable.

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

5 participants