mobx-state-tree
mobx-state-tree copied to clipboard
custom action names
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?
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.
@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?
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.
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.
When I call the action delay1, I got some unamed action in the changes tab, what's wrong with this?

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;
})
i can reproduce the same with the mobx spy method. all actions defined in the models are name: "<unnamed action>"
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)
issue moved to : https://github.com/coolsoftwaretyler/mst-middlewares/issues/12
^ 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!