Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-actions-stay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

Prevent stale bound action cleanup from unregistering a newer action with the same URL.
5 changes: 4 additions & 1 deletion src/data/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ function toAction<T extends Array<any>, U, V = T>(
fn[invokeSymbol] = invoke;
if (!isServer) {
actions.set(url, fn as unknown as Action<T, U, V>);
getOwner() && onCleanup(() => actions.delete(url));
getOwner() &&
onCleanup(() => {
if (actions.get(url) === fn) actions.delete(url);
});
}
return fn as unknown as Action<T, U, V>;
}
Expand Down
25 changes: 25 additions & 0 deletions test/data/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,31 @@ describe("action", () => {
expect(actions.get(testAction.url)).toBe(testAction);
});

test("should not unregister a replacement action with the same URL", () => {
const testAction = action(async (value: string) => value, "replacement-test");
let disposeFirst!: () => void;
let disposeSecond!: () => void;

const first = createRoot(dispose => {
disposeFirst = dispose;
return testAction.with("value");
});
const second = createRoot(dispose => {
disposeSecond = dispose;
return testAction.with("value");
});

expect(first.url).toBe(second.url);
expect(actions.get(second.url)).toBe(second);

disposeFirst();

expect(actions.get(second.url)).toBe(second);

disposeSecond();
expect(actions.has(second.url)).toBe(false);
});

test("should support `.with` method for currying arguments", () => {
const baseAction = action(async (prefix: string, data: string) => {
return `${prefix}: ${data}`;
Expand Down
Loading