Skip to content

Commit 5f3f68a

Browse files
authored
Merge pull request #7686 from QwikDev/v2-memoizee-qrl-imports
perf(qrl): remove $lazyResolve$
2 parents c9a93cc + 7d8b949 commit 5f3f68a

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

.changeset/soft-insects-see.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@qwik.dev/core': patch
3+
---
4+
5+
:zap: QRL segments now memoize imports, removing some Promises during render

packages/qwik/src/core/shared/qrl/qrl-class.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type QRLInternalMethods<TYPE> = {
4141

4242
resolved: undefined | TYPE;
4343

44-
resolve(): Promise<TYPE>;
44+
resolve(containerEl?: Element): Promise<TYPE>;
4545
getSymbol(): string;
4646
getHash(): string;
4747
getCaptured(): unknown[] | null;
@@ -54,7 +54,6 @@ export type QRLInternalMethods<TYPE> = {
5454
unknown;
5555

5656
$setContainer$(containerEl: Element | undefined): Element | undefined;
57-
$resolveLazy$(containerEl?: Element): ValueOrPromise<TYPE>;
5857
};
5958

6059
export type QRLInternal<TYPE = unknown> = QRL<TYPE> & QRLInternalMethods<TYPE>;
@@ -128,10 +127,6 @@ export const createQRL = <TYPE>(
128127
return bound;
129128
}
130129

131-
const resolveLazy = (containerEl?: Element): ValueOrPromise<TYPE> => {
132-
return symbolRef !== null ? symbolRef : resolve(containerEl);
133-
};
134-
135130
// Wrap functions to provide their lexical scope
136131
const wrapFn = (fn: TYPE): TYPE => {
137132
if (typeof fn !== 'function' || (!capture?.length && !captureRef?.length)) {
@@ -205,7 +200,7 @@ export const createQRL = <TYPE>(
205200
const imported = getPlatform().importSymbol(_containerEl, chunk, symbol);
206201
symbolRef = maybeThen(imported, (ref) => (qrl.resolved = wrapFn((symbolRef = ref))));
207202
}
208-
if (typeof symbolRef === 'object' && isPromise(symbolRef)) {
203+
if (isPromise(symbolRef)) {
209204
symbolRef.then(
210205
() => emitUsedSymbol(symbol, ctx?.$element$, start),
211206
(err) => {
@@ -235,7 +230,6 @@ export const createQRL = <TYPE>(
235230
getHash: () => hash,
236231
getCaptured: () => captureRef,
237232
resolve,
238-
$resolveLazy$: resolveLazy,
239233
$setContainer$: setContainer,
240234
$chunk$: chunk,
241235
$symbol$: symbol,

packages/qwik/src/core/use/use-styles.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { implicit$FirstArg } from '../shared/qrl/implicit_dollar';
33
import { getScopedStyles } from '../shared/utils/scoped-stylesheet';
44
import { useSequentialScope } from './use-sequential-scope';
55
import { assertQrl } from '../shared/qrl/qrl-utils';
6-
import { isPromise } from '../shared/utils/promises';
76
import { ComponentStylesPrefixContent } from '../shared/utils/markers';
87
import { styleKey } from '../shared/utils/styles';
98

@@ -96,14 +95,14 @@ const _useStyles = (
9695
const host = iCtx.$hostElement$;
9796
set(styleId);
9897

99-
const value = styleQrl.$resolveLazy$(iCtx.$element$);
100-
if (isPromise(value)) {
101-
value.then((val) =>
102-
iCtx.$container$.$appendStyle$(transform(val, styleId), styleId, host, scoped)
103-
);
104-
throw value;
98+
if (styleQrl.resolved) {
99+
iCtx.$container$.$appendStyle$(transform(styleQrl.resolved, styleId), styleId, host, scoped);
105100
} else {
106-
iCtx.$container$.$appendStyle$(transform(value, styleId), styleId, host, scoped);
101+
throw styleQrl
102+
.resolve()
103+
.then((val) =>
104+
iCtx.$container$.$appendStyle$(transform(val, styleId), styleId, host, scoped)
105+
);
107106
}
108107

109108
return styleId;

packages/qwik/src/core/use/use-visible-task.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EventHandler } from '../shared/jsx/types/jsx-qwik-attributes';
22
import { isServerPlatform } from '../shared/platform/platform';
3-
import { createQRL } from '../shared/qrl/qrl-class';
3+
import { createQRL, type QRLInternal } from '../shared/qrl/qrl-class';
44
import { assertQrl } from '../shared/qrl/qrl-utils';
55
import type { QRL } from '../shared/qrl/qrl.public';
66
import { ChoreType } from '../shared/util-chore-type';
@@ -42,7 +42,7 @@ export const useVisibleTaskQrl = (qrl: QRL<TaskFn>, opts?: OnVisibleTaskOptions)
4242
set(task);
4343
useRunTask(task, eagerness);
4444
if (!isServerPlatform()) {
45-
qrl.$resolveLazy$(iCtx.$element$);
45+
(qrl as QRLInternal).resolve(iCtx.$element$);
4646
iCtx.$container$.$scheduler$(ChoreType.VISIBLE, task);
4747
}
4848
};

0 commit comments

Comments
 (0)