Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace async useComputed$ #11

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
16 changes: 12 additions & 4 deletions packages/qwik-image/src/lib/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
useComputed$,
useContext,
useContextProvider,
useSignal,
useTask$,
} from '@builder.io/qwik';

export const DEFAULT_RESOLUTIONS = [3840, 1920, 1280, 960, 640];
Expand Down Expand Up @@ -211,6 +213,7 @@ export const getBreakpoints = ({
* @alpha
*/
export const Image = component$<ImageProps>((props) => {
const srcSetSig = useSignal<string>();
const state = useContext(ImageContext);
const { resolutions, imageTransformer$, ...imageAttributes } = {
...state,
Expand All @@ -226,9 +229,14 @@ export const Image = component$<ImageProps>((props) => {
...getStyles(props),
}));
const sizes = useComputed$(() => getSizes(props));
const srcSet = useComputed$(() => {
const { src, width, height, aspectRatio, layout } = props;
return getSrcSet({
useTask$(async ({ track }) => {
const src = track(() => props.src);
const width = track(() => props.width);
const height = track(() => props.height);
const aspectRatio = track(() => props.aspectRatio);
const layout = track(() => props.layout);

srcSetSig.value = await getSrcSet({
Comment on lines +232 to +239
Copy link
Member

@wmertens wmertens Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, you can run any sync code you like inside track and track multiple signals at once.

you could so something like await getSrcSet(track(() => props))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I just realized that you need to reference the props themselves, so it would be more like await getSrcSet(track(() => ({src: props.src, width: props.width, ...}) or you could destructure in the component top and just list the props in track.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, would you like to create a PR for that?

src,
width,
height,
Expand Down Expand Up @@ -256,7 +264,7 @@ export const Image = component$<ImageProps>((props) => {
style={style.value}
width={width.value}
height={height.value}
srcset={srcSet.value}
srcset={srcSetSig.value}
sizes={sizes.value}
/>
);
Expand Down