xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
554 stars 25 forks source link

async function undo redo #551

Open wtianye opened 1 month ago

wtianye commented 1 month ago

How to make the updateCounts operation complete in a group.Neither of the following attempts worked

@model("MyApp/Counter")
class Counter extends Model({ count: prop(0) }) {
  @modelAction
  update(n: number) {
    this.count = n;
  }
}

export async function updateCounts(counter: Counter) {
  counter.update(111);
  await f();
  counter.update(222);
}

export function f() {
  return new Promise((resolve) => {
    resolve(0);
  });
}

test("1", async () => {
  const counter = new Counter({});
  const undoManager = undoMiddleware(counter);
  const group = undoManager.createGroup();
  await group.continue(async () => {
    await updateCounts(counter);
  });
});

test("2", async () => {
  const counter = new Counter({});
  const undoManager = undoMiddleware(counter);
  await undoManager.withGroupFlow("withGroupFlow", function* () {
    yield* _await(updateCounts(counter));
  });
});