-
I understand that flows don't batch, but what purpose do they serve exactly? What is the difference between the following: class FlowStore {
constructor() { makeAutoObservable(this); }
count = 0
*increment() {
this.count++;
yield this.incrementLater();
}
async incrementLater() {
await delay(100);
this.count++;
}
} class NoFlowStore {
constructor() { makeAutoObservable(this); }
count = 0
async increment() {
this.count++;
await this.incrementLater();
}
async incrementLater() {
await delay(100);
this.count++;
}
} To me, they look like they both accomplish the same goal, but there must be some difference under the hood? What are the benefits of putting things through a flow? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Accepted answer:Original answer:*increment() {
this.count++;
yield this.incrementLater();
} is equivalent to: async increment() {
runInAction(() => {
this.count++;
})
await this.incrementLater();
} Besides mobx complaining about mutating observable outside async increment() {
this.count++;
this.foo++;
this.bar++;
await this.incrementLater();
} This would result in 4 individual invocations of observers (instead of 2 if using |
Beta Was this translation helpful? Give feedback.
Accepted answer:
#3171 (reply in thread)
Original answer:
is equivalent to:
Besides mobx complaining about mutating observable outside
action
the practical implications are only noticable if you mutate multiple observables at a time:This would result in 4 individual invocations of observers (instead of 2 if using
flow
).