mobx-state-tree icon indicating copy to clipboard operation
mobx-state-tree copied to clipboard

custom action names

Open Mokhalil opened this issue 7 years ago • 9 comments

I want to set a name for the actions in order to be able to track it in the developer tool console. Something like this in mobx

@action("My text goes here") function myaction()

is that possible in mobx-state-tree?

Mokhalil avatar Apr 01 '18 15:04 Mokhalil

There's a handy onAction method for that. You do not need any additional name - the function that you decorate already has one. So onAction will report that.

jayarjo avatar Apr 02 '18 09:04 jayarjo

@Mokhalil assuming you're talking about the mobx dev tools then currently no - @mweststrate i guess we could add the fn.name to the action or do you see a reason not to do that?

robinfehr avatar Apr 02 '18 11:04 robinfehr

So in mst tab we already can display action names because we always have named actions. But I don't now how to pass names to it in extension.

Amareis avatar Oct 07 '18 11:10 Amareis

In this handler we can add onAction and add action name in log item info, I think, then dispaly it in frontend. In that way we don't even need redux dev tools for logging actions.

Amareis avatar Oct 07 '18 11:10 Amareis

When I call the action delay1, I got some unamed action in the changes tab, what's wrong with this?

unamed action

This is my code:

somemodel
.actions((self) => {
    const actions = {
      runInAction(fn: () => void) {
        return fn();
      },
      add1() {
        self.storeNumber++; // use self to get/set properties
      },
      add2() {
        this.add1(); // use this to call methods
        this.add1();
      },
      delay1: flow(function* delay1(ms: number, fn: () => void) {
        self.loading = true;
        self.loadingMS = 0;
        actions.add1();
        try {
          yield sleep(ms * 1000);
          self.loadingMS = ms;
          fn();
        } catch (e) {
          console.error(e);
        } finally {
          self.loading = false;
        }
      }),
    };
    return actions;
  })

qinhuaihe avatar Oct 19 '18 02:10 qinhuaihe

i can reproduce the same with the mobx spy method. all actions defined in the models are name: "<unnamed action>"

maerzhase avatar Mar 19 '19 13:03 maerzhase

Hi, I am having similar issue. I want to add custom prefix to actions names in order to be able to track them in middleware but if I do it in a usual way

.actions(self => ({
   [addPrefix('fetchUser')]: flow(...) 

   const addPrefix = (name: string) => `${REQUEST_ACTION_PREFIX}.${name}`;

I will lose typescript support as it doesn't work with string interpolations

It seems I need something like const actionInvoker = createActionInvoker(self as any, action2.name || name, boundAction)

Avizura avatar Mar 17 '20 21:03 Avizura

issue moved to : https://github.com/coolsoftwaretyler/mst-middlewares/issues/12

zuhorer avatar Sep 19 '23 18:09 zuhorer

^ Whoops, this issue got caught up in a migration because it matched for middleware and is:open, but we are going to keep it here. Sorry for the runaround!

coolsoftwaretyler avatar Sep 21 '23 23:09 coolsoftwaretyler