Skip to content

Commit

Permalink
Fix runInOwner
Browse files Browse the repository at this point in the history
  • Loading branch information
Exelord committed Feb 2, 2023
1 parent f926708 commit 675040e
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
Owner,
getOwner,
runWithOwner,
useContext,
createRoot,
onCleanup,
} from "solid-js";
import { Owner } from "solid-js/types/reactive/signal";
import { ServiceRegistryContext } from "./context";

export interface Service extends Record<any, any> {}
Expand All @@ -16,23 +16,28 @@ export interface RegistryConfig {
expose?: ServiceInitializer<any>[] | boolean;
}

function runInOwner<T>(owner: Owner, fn: () => T) {
function runInOwner<T>(owner: Owner, fn: () => T): T {
let error;
let hasErrored = false;

const result = createRoot((dispose) => {
try {
runWithOwner(owner, () => onCleanup(dispose));
return fn();
} catch (e) {
dispose();
hasErrored = true;
error = e;
return;
}
}, owner);

if (hasErrored) throw error; // We need to throw error here in order to stop further execution of parent function
const result = runWithOwner(owner, () => {
let disposer: () => void;
onCleanup(() => disposer?.());

return createRoot((dispose) => {
disposer = dispose;
try {
return fn();
} catch (e) {
dispose();
hasErrored = true;
error = e;
return;
}
}, owner);
})!;

if (hasErrored) throw error;

return result;
}
Expand Down

0 comments on commit 675040e

Please sign in to comment.