Skip to content

Commit 013e2e2

Browse files
committed
wip remove $capture$ from qrl
1 parent 338ac31 commit 013e2e2

File tree

17 files changed

+87
-114
lines changed

17 files changed

+87
-114
lines changed

packages/docs/src/routes/api/qwik/api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@
808808
}
809809
],
810810
"kind": "Function",
811-
"content": "Create an inlined QRL. This is mostly useful on the server side for serialization.\n\n\n```typescript\ninlinedQrl: <T>(symbol: T | null, symbolName: string, lexicalScopeCapture?: any[]) => QRL<T>\n```\n\n\n<table><thead><tr><th>\n\nParameter\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\nsymbol\n\n\n</td><td>\n\nT \\| null\n\n\n</td><td>\n\nThe object/function to register, or `null` to retrieve a previously registered one by hash\n\n\n</td></tr>\n<tr><td>\n\nsymbolName\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\nThe name of the symbol.\n\n\n</td></tr>\n<tr><td>\n\nlexicalScopeCapture\n\n\n</td><td>\n\nany\\[\\]\n\n\n</td><td>\n\n_(Optional)_ A set of lexically scoped variables to capture.\n\n\n</td></tr>\n</tbody></table>\n\n**Returns:**\n\n[QRL](#qrl)<!-- -->&lt;T&gt;",
811+
"content": "Create an inlined QRL. This is mostly useful on the server side for serialization.\n\n\n```typescript\ninlinedQrl: <T>(symbol: T | null, symbolName: string, lexicalScopeCapture?: any[] | null) => QRL<T>\n```\n\n\n<table><thead><tr><th>\n\nParameter\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\nsymbol\n\n\n</td><td>\n\nT \\| null\n\n\n</td><td>\n\nThe object/function to register, or `null` to retrieve a previously registered one by hash\n\n\n</td></tr>\n<tr><td>\n\nsymbolName\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\nThe name of the symbol.\n\n\n</td></tr>\n<tr><td>\n\nlexicalScopeCapture\n\n\n</td><td>\n\nany\\[\\] \\| null\n\n\n</td><td>\n\n_(Optional)_ A set of lexically scoped variables to capture.\n\n\n</td></tr>\n</tbody></table>\n\n**Returns:**\n\n[QRL](#qrl)<!-- -->&lt;T&gt;",
812812
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/shared/qrl/qrl.ts",
813813
"mdFile": "core.inlinedqrl.md"
814814
},

packages/docs/src/routes/api/qwik/index.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ Create an inlined QRL. This is mostly useful on the server side for serializatio
17131713
inlinedQrl: <T>(
17141714
symbol: T | null,
17151715
symbolName: string,
1716-
lexicalScopeCapture?: any[],
1716+
lexicalScopeCapture?: any[] | null,
17171717
) => QRL<T>;
17181718
```
17191719
@@ -1762,7 +1762,7 @@ lexicalScopeCapture
17621762
17631763
</td><td>
17641764
1765-
any[]
1765+
any[] \| null
17661766
17671767
</td><td>
17681768

packages/qwik/src/core/qwik.core.api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,13 @@ export const _IMMUTABLE: unique symbol;
394394
export const implicit$FirstArg: <FIRST, REST extends any[], RET>(fn: (qrl: QRL<FIRST>, ...rest: REST) => RET) => ((qrl: FIRST, ...rest: REST) => RET);
395395

396396
// @public
397-
export const inlinedQrl: <T>(symbol: T | null, symbolName: string, lexicalScopeCapture?: any[]) => QRL<T>;
397+
export const inlinedQrl: <T>(symbol: T | null, symbolName: string, lexicalScopeCapture?: any[] | null) => QRL<T>;
398398

399399
// Warning: (ae-forgotten-export) The symbol "QRLDev" needs to be exported by the entry point index.d.ts
400400
// Warning: (ae-internal-missing-underscore) The name "inlinedQrlDEV" should be prefixed with an underscore because the declaration is marked as @internal
401401
//
402402
// @internal (undocumented)
403-
export const inlinedQrlDEV: <T = any>(symbol: T, symbolName: string, opts: QRLDev, lexicalScopeCapture?: any[]) => QRL<T>;
403+
export const inlinedQrlDEV: <T = any>(symbol: T, symbolName: string, opts: QRLDev, lexicalScopeCapture?: any[] | null) => QRL<T>;
404404

405405
export { isBrowser }
406406

@@ -570,10 +570,10 @@ export type NativeUIEvent = UIEvent;
570570
export type NativeWheelEvent = WheelEvent;
571571

572572
// @internal (undocumented)
573-
export const _noopQrl: <T>(symbolName: string, lexicalScopeCapture?: any[]) => QRL<T>;
573+
export const _noopQrl: <T>(symbolName: string, lexicalScopeCapture?: any[] | null) => QRL<T>;
574574

575575
// @internal (undocumented)
576-
export const _noopQrlDEV: <T>(symbolName: string, opts: QRLDev, lexicalScopeCapture?: any[]) => QRL<T>;
576+
export const _noopQrlDEV: <T>(symbolName: string, opts: QRLDev, lexicalScopeCapture?: any[] | null) => QRL<T>;
577577

578578
// @public
579579
export type NoSerialize<T> = (T & {

packages/qwik/src/core/shared/jsx/jsx-node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ const handleBindProp = (props: Props, prop: string) => {
158158
if (value) {
159159
if (prop === BIND_CHECKED) {
160160
props.checked = value;
161-
props['on:input'] = createQRL(null, '_chk', _chk, null, null, [value]);
161+
props['on:input'] = createQRL(null, '_chk', _chk, null, [value]);
162162
} else {
163163
props.value = value;
164-
props['on:input'] = createQRL(null, '_val', _val, null, null, [value]);
164+
props['on:input'] = createQRL(null, '_val', _val, null, [value]);
165165
}
166166
return true;
167167
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export type QRLInternalMethods<TYPE> = {
3535
readonly $symbol$: string;
3636
readonly $hash$: string;
3737

38-
$capture$: string[] | null;
3938
$captureRef$: unknown[] | null;
4039
dev: QRLDev | null;
4140

@@ -65,7 +64,6 @@ export const createQRL = <TYPE>(
6564
symbol: string,
6665
symbolRef: null | ValueOrPromise<TYPE>,
6766
symbolFn: null | (() => Promise<Record<string, TYPE>>),
68-
capture: null | Readonly<number[]>,
6967
captureRef: Readonly<unknown[]> | null
7068
): QRLInternal<TYPE> => {
7169
if (qDev && qSerialize) {
@@ -128,7 +126,7 @@ export const createQRL = <TYPE>(
128126

129127
// Wrap functions to provide their lexical scope
130128
const wrapFn = (fn: TYPE): TYPE => {
131-
if (typeof fn !== 'function' || (!capture?.length && !captureRef?.length)) {
129+
if (typeof fn !== 'function' || !captureRef?.length) {
132130
return fn;
133131
}
134132
return function (this: unknown, ...args: QrlArgs<TYPE>) {
@@ -236,7 +234,6 @@ export const createQRL = <TYPE>(
236234
$hash$: hash,
237235
getFn: bindFnToContext,
238236

239-
$capture$: capture,
240237
$captureRef$: captureRef,
241238
dev: null,
242239
resolved: undefined,

packages/qwik/src/core/shared/qrl/qrl.public.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export const $ = <T>(expression: T): QRL<T> => {
254254
);
255255
}
256256

257-
return createQRL<T>(null, 's' + runtimeSymbolId++, expression, null, null, null);
257+
return createQRL<T>(null, 's' + runtimeSymbolId++, expression, null, null);
258258
};
259259
/** @private Use To avoid optimizer replacement */
260260
export const dollar = $;
@@ -303,7 +303,7 @@ export const sync$ = <T extends Function>(fn: T): SyncQRL<T> => {
303303
fn = new Function('return ' + fn.toString())() as any;
304304
}
305305

306-
return createQRL<T>('', SYNC_QRL, fn, null, null, null) as any;
306+
return createQRL<T>('', SYNC_QRL, fn, null, null) as any;
307307
};
308308

309309
/**
@@ -324,5 +324,5 @@ export const _qrlSync = function <TYPE extends Function>(
324324
serializedFn = fn.toString();
325325
}
326326
(fn as any).serialized = serializedFn;
327-
return createQRL<TYPE>('', SYNC_QRL, fn, null, null, null) as any;
327+
return createQRL<TYPE>('', SYNC_QRL, fn, null, null) as any;
328328
};

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const qrl = <T = any>(
7575
}
7676

7777
// Unwrap subscribers
78-
return createQRL<T>(chunk, symbol, null, symbolFn, null, lexicalScopeCapture);
78+
return createQRL<T>(chunk, symbol, null, symbolFn, lexicalScopeCapture);
7979
};
8080

8181
/**
@@ -90,25 +90,25 @@ export const qrl = <T = any>(
9090
export const inlinedQrl = <T>(
9191
symbol: T | null,
9292
symbolName: string,
93-
lexicalScopeCapture: any[] = EMPTY_ARRAY
93+
lexicalScopeCapture: any[] | null = null
9494
): QRL<T> => {
9595
// Unwrap subscribers
96-
return createQRL<T>(null, symbolName, symbol, null, null, lexicalScopeCapture);
96+
return createQRL<T>(null, symbolName, symbol, null, lexicalScopeCapture);
9797
};
9898

9999
/** @internal */
100100
export const _noopQrl = <T>(
101101
symbolName: string,
102-
lexicalScopeCapture: any[] = EMPTY_ARRAY
102+
lexicalScopeCapture: any[] | null = null
103103
): QRL<T> => {
104-
return createQRL<T>(null, symbolName, null, null, null, lexicalScopeCapture);
104+
return createQRL<T>(null, symbolName, null, null, lexicalScopeCapture);
105105
};
106106

107107
/** @internal */
108108
export const _noopQrlDEV = <T>(
109109
symbolName: string,
110110
opts: QRLDev,
111-
lexicalScopeCapture: any[] = EMPTY_ARRAY
111+
lexicalScopeCapture: any[] | null = null
112112
): QRL<T> => {
113113
const newQrl = _noopQrl(symbolName, lexicalScopeCapture) as QRLInternal<T>;
114114
newQrl.dev = opts;
@@ -120,7 +120,7 @@ export const qrlDEV = <T = any>(
120120
chunkOrFn: string | (() => Promise<any>),
121121
symbol: string,
122122
opts: QRLDev,
123-
lexicalScopeCapture: any[] = EMPTY_ARRAY
123+
lexicalScopeCapture?: any[]
124124
): QRL<T> => {
125125
const newQrl = qrl(chunkOrFn, symbol, lexicalScopeCapture, 1) as QRLInternal<T>;
126126
newQrl.dev = opts;
@@ -132,7 +132,7 @@ export const inlinedQrlDEV = <T = any>(
132132
symbol: T,
133133
symbolName: string,
134134
opts: QRLDev,
135-
lexicalScopeCapture: any[] = EMPTY_ARRAY
135+
lexicalScopeCapture: any[] | null = null
136136
): QRL<T> => {
137137
const qrl = inlinedQrl(symbol, symbolName, lexicalScopeCapture) as QRLInternal<T>;
138138
qrl.dev = opts;

packages/qwik/src/core/shared/qrl/qrl.unit.ts

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,25 @@ describe('serialization', () => {
6161
matchProps(parseQRL('./chunk#s1'), {
6262
$chunk$: './chunk',
6363
$symbol$: 's1',
64-
$capture$: null,
6564
});
6665
matchProps(parseQRL('./chunk#s1[1 2]'), {
6766
$chunk$: './chunk',
6867
$symbol$: 's1',
69-
$capture$: [1, 2],
7068
});
7169
matchProps(parseQRL('./chunk#s1[1 2]'), {
7270
$chunk$: './chunk',
7371
$symbol$: 's1',
74-
$capture$: [1, 2],
7572
});
7673
matchProps(parseQRL('./chunk#s1[1 2]'), {
7774
$chunk$: './chunk',
7875
$symbol$: 's1',
79-
$capture$: [1, 2],
8076
});
8177
matchProps(parseQRL('./chunk[1 2]'), {
8278
$chunk$: './chunk',
83-
$capture$: [1, 2],
8479
});
8580
matchProps(parseQRL('./path#symbol[2]'), {
8681
$chunk$: './path',
8782
$symbol$: 'symbol',
88-
$capture$: [2],
8983
});
9084
matchProps(
9185
parseQRL(
@@ -94,7 +88,6 @@ describe('serialization', () => {
9488
{
9589
$chunk$: '/src/path%2d/foo_symbol.js?_qrl_parent=/home/user/project/src/path/foo.js',
9690
$symbol$: 'symbol',
97-
$capture$: [2],
9891
}
9992
);
10093
});
@@ -109,36 +102,22 @@ describe('serialization', () => {
109102
new WeakMap<any, any>()
110103
);
111104
assert.equal(
112-
qrlToString(serializationContext, createQRL('./chunk', '', null, null, null, null)),
105+
qrlToString(serializationContext, createQRL('./chunk', '', null, null, null)),
113106
'chunk#'
114107
);
115108
assert.equal(
116-
qrlToString(serializationContext, createQRL('./c', 's1', null, null, null, null)),
109+
qrlToString(serializationContext, createQRL('./c', 's1', null, null, null)),
117110
'c#s1'
118111
);
112+
assert.equal(qrlToString(serializationContext, createQRL('./c', 's1', null, null, [])), 'c#s1');
119113
assert.equal(
120-
qrlToString(serializationContext, createQRL('./c', 's1', null, null, [], null)),
121-
'c#s1'
122-
);
123-
assert.equal(
124-
qrlToString(
125-
serializationContext,
126-
createQRL(
127-
'./c',
128-
's1',
129-
null,
130-
null,
131-
// should be ignored
132-
[1, '2'] as any,
133-
[{}, {}]
134-
)
135-
),
114+
qrlToString(serializationContext, createQRL('./c', 's1', null, null, [{}, {}])),
136115
'c#s1[0 1]'
137116
);
138117
assert.equal(
139118
qrlToString(
140119
serializationContext,
141-
createQRL('src/routes/[...index]/a+b/c?foo', 's1', null, null, null, [{}, {}])
120+
createQRL('src/routes/[...index]/a+b/c?foo', 's1', null, null, [{}, {}])
142121
),
143122
'src/routes/[...index]/a+b/c?foo#s1[2 3]'
144123
);
@@ -176,19 +155,19 @@ describe('serialization', () => {
176155

177156
describe('createQRL', () => {
178157
test('should create QRL', () => {
179-
const q = createQRL('chunk', 'symbol', 'resolved', null, null, null);
158+
const q = createQRL('chunk', 'symbol', 'resolved', null, null);
180159
matchProps(q, {
181160
$chunk$: 'chunk',
182161
$symbol$: 'symbol',
183162
resolved: 'resolved',
184163
});
185164
});
186165
test('should have .resolved: given scalar', async () => {
187-
const q = createQRL('chunk', 'symbol', 'resolved', null, null, null);
166+
const q = createQRL('chunk', 'symbol', 'resolved', null, null);
188167
assert.equal(q.resolved, 'resolved');
189168
});
190169
test('should have .resolved: given promise for scalar', async () => {
191-
const q = createQRL('chunk', 'symbol', Promise.resolve('resolved'), null, null, null);
170+
const q = createQRL('chunk', 'symbol', Promise.resolve('resolved'), null, null);
192171
assert.equal(q.resolved, undefined);
193172
assert.equal(await q.resolve(), 'resolved');
194173
assert.equal(q.resolved, 'resolved');
@@ -199,7 +178,6 @@ describe('createQRL', () => {
199178
'symbol',
200179
null,
201180
() => Promise.resolve({ symbol: 'resolved' }),
202-
null,
203181
null
204182
);
205183
assert.equal(q.resolved, undefined);
@@ -209,31 +187,31 @@ describe('createQRL', () => {
209187

210188
const fn = () => 'hi';
211189
test('should have .resolved: given function without captures', async () => {
212-
const q = createQRL('chunk', 'symbol', fn, null, null, null);
190+
const q = createQRL('chunk', 'symbol', fn, null, null);
213191
assert.equal(q.resolved, fn);
214192
});
215193
test('should have .resolved: given promise for function without captures', async () => {
216-
const q = createQRL('chunk', 'symbol', Promise.resolve(fn), null, null, null);
194+
const q = createQRL('chunk', 'symbol', Promise.resolve(fn), null, null);
217195
assert.equal(q.resolved, undefined);
218196
assert.equal(await q.resolve(), fn);
219197
assert.equal(q.resolved, fn);
220198
});
221199
test('should have .resolved: promise for function without captures', async () => {
222-
const q = createQRL('chunk', 'symbol', null, () => Promise.resolve({ symbol: fn }), null, null);
200+
const q = createQRL('chunk', 'symbol', null, () => Promise.resolve({ symbol: fn }), null);
223201
assert.equal(q.resolved, undefined);
224202
assert.equal(await q.resolve(), fn);
225203
assert.equal(q.resolved, fn);
226204
});
227205

228206
const capFn = () => useLexicalScope();
229207
test('should have .resolved: given function with captures', async () => {
230-
const q = createQRL('chunk', 'symbol', capFn, null, null, ['hi']);
208+
const q = createQRL('chunk', 'symbol', capFn, null, ['hi']);
231209
assert.isDefined(q.resolved);
232210
assert.notEqual(q.resolved, capFn);
233211
assert.deepEqual(q.resolved!(), ['hi']);
234212
});
235213
test('should have .resolved: given promise for function with captures', async () => {
236-
const q = createQRL('chunk', 'symbol', Promise.resolve(capFn), null, null, ['hi']);
214+
const q = createQRL('chunk', 'symbol', Promise.resolve(capFn), null, ['hi']);
237215
assert.equal(q.resolved, undefined);
238216
assert.deepEqual(await q(), ['hi']);
239217
assert.notEqual(q.resolved, capFn);
@@ -245,7 +223,6 @@ describe('createQRL', () => {
245223
'symbol',
246224
null,
247225
() => Promise.resolve({ symbol: capFn }),
248-
null,
249226
['hi']
250227
);
251228
assert.equal(q.resolved, undefined);

packages/qwik/src/core/shared/scheduler-rules.unit.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function createMockTask(
6060
}
6161

6262
function createMockQRL(symbol: string): QRL {
63-
return createQRL(null, symbol, null, null, null, null) as QRL;
63+
return createQRL(null, symbol, null, null, null) as QRL;
6464
}
6565

6666
describe('findBlockingChore', () => {

packages/qwik/src/core/shared/serdes/allocate.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,22 @@ export const allocate = (container: DeserializeContainer, typeId: number, value:
5151
case TypeIds.QRL:
5252
case TypeIds.PreloadQRL: {
5353
if (typeof value === 'string') {
54-
const data = value.split(' ').map(Number);
55-
const chunk = container.$getObjectById$(data[0]) as string;
56-
const symbol = container.$getObjectById$(data[1]) as string;
57-
const captureIds = data.length > 2 ? data.slice(2) : null;
58-
return createQRLWithBackChannel(chunk, symbol, captureIds);
54+
const firstSpace = value.indexOf(' ');
55+
const chunkIdx = Number(value.slice(0, firstSpace));
56+
const secondSpace = value.indexOf(' ', firstSpace + 1);
57+
const symbolIdx = Number(
58+
secondSpace === -1
59+
? value.slice(firstSpace + 1)
60+
: value.slice(firstSpace + 1, secondSpace)
61+
);
62+
const chunk = container.$getObjectById$(chunkIdx) as string;
63+
const symbol = container.$getObjectById$(symbolIdx) as string;
64+
const qrl = createQRLWithBackChannel(chunk, symbol);
65+
if (secondSpace !== -1) {
66+
// temporarily store the captured references for inflate
67+
qrl.$captureRef$ = value.slice(secondSpace + 1) as unknown as unknown[];
68+
}
69+
return qrl;
5970
} else {
6071
return createQRLWithBackChannel('', String(value));
6172
}

0 commit comments

Comments
 (0)