Skip to content

Commit

Permalink
refactor: redo ShowFreeze logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-ext committed Dec 8, 2023
1 parent 896d9e3 commit 2d4903b
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions packages/solid-freeze/lib/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,23 @@ export interface ShowFreezeProps {
}

export const ShowFreeze = (props: ShowFreezeProps) => {
const promise = createMemo((prev: Deferred | undefined) => {
if (props.when) {
if (prev) {
return prev;
}

let _resolve: Deferred['r'];
let promise = new Promise((resolve) => (_resolve = resolve)) as Deferred;

promise.r = _resolve!;
return promise;
} else if (prev) {
prev.r(undefined);
}
});

// Hard-stuck to `true` if `props.when` is true
const show = createMemo((prev: boolean) => {
if (prev || props.when) {
return prev;
return true;
}

return prev;
}, false);

const [suspend] = createResource(promise, identity);

return (
<Suspense
fallback={props.fallback}
// @ts-expect-error
children={[suspend, () => show() && props.children]}
/>
);
return Freeze({
get freeze() {
return show() && !props.when;
},
get children() {
if (show()) {
return props.children;
}
},
});
};

0 comments on commit 2d4903b

Please sign in to comment.