Skip to content

Commit

Permalink
Fully isolate job from Solid's owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Exelord committed Apr 2, 2023
1 parent b4d917e commit 96c29f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
31 changes: 14 additions & 17 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { createRoot, runWithOwner } from "solid-js";
import { runWithOwner } from "solid-js";

export function isolate<T>(fn: () => T): T {
let error;
let hasErrored = false;
const owner = null as any;
let error: unknown;
let hasError = false;

const result = runWithOwner(owner, () => {
return createRoot((dispose) => {
try {
return fn();
} catch (e) {
hasErrored = true;
error = e;
return;
} finally {
dispose();
}
});
const result = runWithOwner(null, () => {
try {
return fn();
} catch (e) {
hasError = true;
error = e;
throw e;
}
})!;

if (hasErrored) throw error;
if (hasError) {
throw error;
}

return result;
}
5 changes: 1 addition & 4 deletions tests/browser/task.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { test, expect } from "@playwright/experimental-ct-solid";
import { TodoList } from "../fixtures/todo-list";

test.only("adds only one todo even when clicked twice", async ({
mount,
page,
}) => {
test("adds only one todo even when clicked twice", async ({ mount, page }) => {
await mount(<TodoList />);

const addTodo = page.getByRole("button", { name: "Add todo" });
Expand Down
13 changes: 12 additions & 1 deletion tests/vitest/job.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { work, timeout } from "../../src/work";
import { createJob, JobMode } from "../../src/job";
import { describe, test, expect } from "vitest";
import { createRoot } from "solid-js";
import { createRoot, getOwner } from "solid-js";

describe("job", () => {
describe("#perform", () => {
Expand Down Expand Up @@ -119,4 +119,15 @@ describe("job", () => {
expect(job.isPending).toBe(false);
});
});

test("runs without owner", async () => {
await createRoot(async () => {
const job = createJob(async () => {
expect(getOwner()).toBe(null);
});

expect(getOwner()).not.toBe(null);
await job.perform();
});
});
});

0 comments on commit 96c29f0

Please sign in to comment.