Skip to content

Commit

Permalink
fix: missing set removing state
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Mar 7, 2024
1 parent 81fe265 commit cb81942
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
56 changes: 32 additions & 24 deletions src/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function memoExternal<F extends AsyncFn>(
} else {
try {
// Waiting or Removing
if (cur.state === State.Removing) {
while (cur.state === State.Removing) {
await new Promise<void>((res) => {
if (!cur.removingCallbacks) {
cur.removingCallbacks = new Set();
Expand Down Expand Up @@ -90,33 +90,41 @@ export function memoExternal<F extends AsyncFn>(
const cur = walkOrBreak<F, any[]>(root, path);

if (cur) {
if (cur.state === State.Waiting) {
await new Promise((res) => {
if (!cur.callbacks) {
cur.callbacks = new Set();
}
// Ignore error
cur.callbacks!.add({ res, rej: () => {} });
});
} else if (cur.state === State.Removing) {
await new Promise<void>((res) => {
if (!cur.removingCallbacks) {
cur.removingCallbacks = new Set();
}
// Ignore error
cur.removingCallbacks!.add({ res, rej: () => {} });
});
while (cur.state === State.Waiting || cur.state === State.Removing) {
if (cur.state === State.Waiting) {
await new Promise((res) => {
if (!cur.callbacks) {
cur.callbacks = new Set();
}
// Ignore error
cur.callbacks!.add({ res, rej: () => {} });
});
} else if (cur.state === State.Removing) {
await new Promise<void>((res) => {
if (!cur.removingCallbacks) {
cur.removingCallbacks = new Set();
}
// Ignore error
cur.removingCallbacks!.add({ res, rej: () => {} });
});
}
}

await options.external.remove
.bind(memoFunc)(args as Parameters<F>)
.catch(options.external?.error ?? (() => undefined));
try {
cur.state = State.Removing;

await options.external.remove
.bind(memoFunc)(args as Parameters<F>)
.catch(options.external?.error ?? (() => undefined));

// Resolve other waiting callbacks
for (const callback of cur.removingCallbacks ?? []) {
callback.res();
// Resolve other waiting callbacks
for (const callback of cur.removingCallbacks ?? []) {
callback.res();
}
cur.removingCallbacks = undefined;
} finally {
cur.state = State.Empty;
}
cur.removingCallbacks = undefined;
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/memo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe('memo external', () => {
func(),
func()
]);
expect(tasks2).toStrictEqual([undefined, 1, 1, 1, undefined, 1, 1]);
expect(tasks2).toStrictEqual([undefined, 1, 2, 3, undefined, 4, 5]);
});
});

Expand Down

0 comments on commit cb81942

Please sign in to comment.